2010-11-22 83 views
1

我是在C#中的高技术,我想插入一个对象到CheckedListBox, 所以这个插入的项目将有一个标题内的检查列表(我的对象包含一个我想在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置换器(不是我想要显示)

在此先感谢您的帮助。

+0

,你能告诉我们你的尝试和一些类的代码? – 2010-11-22 19:14:54

+0

您希望您的新行显示在CheckedListBox中? – 2010-11-22 19:56:41

+0

该层的名字我可以创建一个道具吧...我的类包含一个字符串名称字段,我将道具添加到问题 – 2010-11-22 19:58:04

回答

1

覆盖的ToString()在你的类,该对象是实例的类。

编辑:

你不想显示的ToString()内容。你想显示LayerName,不是吗?也许你应该用Databinding来显示值。然后您可以将DisplayMember设置为新的LAYERNAME属性。

0

我相信这是你想要达到的目的:

checkedListBox1.Items.Add(yourObject.stringField); 
0

((MyObjectType)checkedListBox1.Items(指数))名称= “无所谓”

你必须知道要更改的对象的索引。

您只需重写类中的ToString方法,以便返回此Name属性值。

public overrides string ToString() { 
    return Name; 
} 

然后,它会在添加到您的CheckedListbox时显示其名称。