开发者

difficulty inserting a name to an inserted object of a checkedlistbox

I am abit new in C# and i am trying to insert an object to a CheckedListBox, so this inserted item will have a title inside the checked list (my object contains a string field inside it which I want to be displayed in the CheckedListBox). for example this is my class:

 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)
        { 
         开发者_C百科    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;}}

my class also contains a tostring overrider inside her (not what i want to display)

thanks in advance for your help.


Override ToString() in your class, the class that the object is an instance of.

Edit:

You don't want to display the contents of ToString(). You want to display the LayerName, don't you? Perhaps you should display the values with Databinding instead. Then you can set DisplayMember to your new LAYERNAME property.


I believe this is what you are trying to achieve:

checkedListBox1.Items.Add(yourObject.stringField);


((MyObjectType)checkedListBox1.Items(index)).Name = "whatever"

You will have to know the index of the object you want to change.

You'll just have to override the ToString method in your class so that it returns this Name property value.

public overrides string ToString() {
    return Name;
}

It will then display its name when added to your CheckedListbox.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜