难以将名称插入到选中列表框的插入对象中

发布于 2024-10-04 00:21:41 字数 1674 浏览 8 评论 0原文

我对 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 重写器(不是我想显示的),

提前感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相思故 2024-10-11 00:21:41

在您的类(该对象是其实例的类)中重写 ToString()。

编辑:

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

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.

昵称有卵用 2024-10-11 00:21:41

我相信这就是您想要实现的目标:

checkedListBox1.Items.Add(yourObject.stringField);

I believe this is what you are trying to achieve:

checkedListBox1.Items.Add(yourObject.stringField);
情感失落者 2024-10-11 00:21:41

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

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

您只需必须覆盖 ToString 类中的方法,以便它返回此 Name 属性值。

public overrides string ToString() {
    return Name;
}

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文