【发布时间】:2010-11-23 04:14:07
【问题描述】:
我是 C# 的新手,我正在尝试将一个对象插入到 CheckedListBox,
所以这个插入的项目将在选中列表中有一个标题(我的对象包含一个字符串字段,我想在 CheckedListBox 中显示它)。
例如这是我的课:
public class InfoLayer
{
private string LayerName;
private List<GeoInfo> GeoInfos;
public InfoLayer()
{
LayerName = "New Empty Layer";
GeoInfos = new List<GeoInfo>();
}
public InfoLayer(string LayerName)
{
this.LayerName = LayerName;
GeoInfos = new List<GeoInfo>();
}
public InfoLayer(string LayerName,List<GeoInfo> GeoInfosToClone):this(LayerName)
{
foreach (GeoInfo item in GeoInfosToClone)
{
GeoInfos.Add((GeoInfo)((ICloneable)item).Clone());
}
}
public GeoInfo SearchElement(long id)
{
foreach (GeoInfo info in GeoInfos) // foreach loop running on the list
{
if (info.INFOID == id)
return info; // return the item if we found it
}
return null;
}
public GeoInfo SearchElement(string name)
{
foreach (GeoInfo info in GeoInfos)
{
if (info.INFONAME.CompareTo(name)==0)
return info;
}
return null;
}
public override string ToString()
{
string toReturn = "";
for (int i = 0; i < GeoInfos.Count; i++) // for loop running on the list
{
toReturn += String.Format("{0}\n",GeoInfos[i].ToString()); // piping another geoinfo
}
return toReturn;
}
public string LAYERNAME{get{return LayerName;}}
我的班级还包含一个 tostring 覆盖器(不是我想要显示的)
提前感谢您的帮助。
【问题讨论】:
-
你能给我们看一些你尝试和上课的代码吗?
-
您希望您的新行如何显示在 CheckedListBox 中?
-
层的名称我可以为它创建一个道具...我的课程包含一个字符串名称字段我将把道具添加到问题中
-
您的 ToString() 方法将返回一个多行字符串。这是你的意图吗?
-
确实是其他用途(tostring)
标签: c# winforms checkedlistbox