C# 难以将名称插入checkedlistbox的插入对象
我是C语言的新手,我正在尝试将一个对象插入到C# 难以将名称插入checkedlistbox的插入对象,c#,winforms,checkedlistbox,C#,Winforms,Checkedlistbox,我是C语言的新手,我正在尝试将一个对象插入到CheckedListBox, 因此,这个插入的项目将在选中列表中有一个标题(我的对象中包含一个字符串字段,我希望在CheckedListBox中显示该字段)。 例如,这是我的班级: public class InfoLayer { private string LayerName; private List<GeoInfo> GeoInfos; public InfoLayer() { L
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;}}
公共类信息层
{
私有字符串层名称;
私人名单地理信息系统;
公共信息层()
{
LayerName=“新建空层”;
GeoInfos=新列表();
}
公共信息层(字符串层名称)
{
this.LayerName=LayerName;
GeoInfos=新列表();
}
公共信息层(字符串LayerName,列表GeoInfosToClone):此(LayerName)
{
foreach(GeoInfosToClone中的GeoInfo项)
{
添加((GeoInfo)((ICloneable)项).Clone());
}
}
公共地理信息搜索元素(长id)
{
foreach(GeoInfos中的GeoInfo)//在列表上运行的foreach循环
{
如果(info.INFOID==id)
return info;//如果找到了项目,请将其返回
}
返回null;
}
公共地理信息搜索元素(字符串名称)
{
foreach(地理信息系统中的地理信息)
{
if(info.INFONAME.CompareTo(name)==0)
退货信息;
}
返回null;
}
公共重写字符串ToString()
{
字符串toReturn=“”;
for(int i=0;i
我的类中还包含一个tostring重写器(不是我想要显示的)
提前感谢您的帮助。我相信这就是您想要实现的目标:
checkedListBox1.Items.Add(yourObject.stringField);
我相信这就是你想要实现的目标:
checkedListBox1.Items.Add(yourObject.stringField);
((MyObjectType)checkedListBox1.Items(index)).Name=“无论什么”
您必须知道要更改的对象的索引
您只需重写类中的方法,使其返回此Name
属性值
public overrides string ToString() {
return Name;
}
然后,当添加到您的CheckedListbox
((MyObjectType)checkedListBox1.Items(index)).name=“任意”
您必须知道要更改的对象的索引
您只需重写类中的方法,使其返回此Name
属性值
public overrides string ToString() {
return Name;
}
然后,当添加到类中的CheckedListbox
覆盖对象是其实例的类中时,它将显示其名称
编辑:
您不想显示ToString()
的内容。您想显示LayerName
,是吗?也许您应该改为使用数据绑定显示值。然后您可以将DisplayMember
设置为新的LayerName
属性。覆盖ToString()在类中,对象是其实例的类
编辑:
您不想显示
ToString()的内容
。您想显示LayerName
,不是吗?也许您应该改为使用数据绑定来显示值。然后您可以将DisplayMember
设置为新的LayerName
属性。不,我希望列表包含对象,并且复选框将对象的txt字段作为名称。这将有b如果是这样的话,那就很容易了。我希望列表中包含对象,复选框中有对象的txt字段作为名称。如果是这样的话,那就很容易了。你能给我们看看你的try和class的一些代码吗?你希望你的新行如何显示在你的CheckedListBox中?我可以为它创建一个道具的层的名称…我的类包含一个字符串名称字段。我将向问题添加prop。您的ToString()方法将返回一个多行字符串。这是您的意图吗?确实是用于其他用途(ToString)你能给我们看一下你的try and类的一些代码吗?你希望你的新行如何显示在你的CheckedListBox中?我可以为它创建一个道具的层的名称…我的类包含一个字符串名称字段,我将把道具添加到问题中。你的ToString()方法将返回一个多行字符串。这是你的意图吗?它确实用于其他用途我误解了这个问题,你的回答清楚了。=@Nadav:If-tostring()如果不起作用,那么在添加对象时,CheckedListBox中会显示哪些文本?在开始使用CheckedListBox上的数据绑定之前,请仔细阅读以下内容:+1我误解了这个问题,您的答案也很清楚。=)@Nadav:If-ToString()如果不起作用,那么在添加对象时,CheckedListBox中会显示什么文本?在开始对CheckedListBox使用数据绑定之前,请仔细阅读以下内容:我不想更改我的对象中的字段我希望CheckedListBox在插入的项旁边有我将要放置的项的名称,这对我来说会更清楚我的类已经有了tostring覆盖功能,但它不起作用我不想更改我的对象中的字段我希望checkedlistbox在插入的项旁边有我将要输入的项的名称。如果我可以说,在你的问题中提到这一点会更清楚y所以。=)编辑了我的答案。我的类已经有了tostring覆盖功能,但它不起作用