Search code examples
c#winformscheckedlistbox

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)
        { 
             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.


Solution

  • 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.