2010-11-22 42 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時顯示其名稱。