难以将名称插入到选中列表框的插入对象中
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的类(该对象是其实例的类)中重写 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 theLayerName
, don't you? Perhaps you should display the values with Databinding instead. Then you can setDisplayMember
to your newLAYERNAME
property.我相信这就是您想要实现的目标:
I believe this is what you are trying to achieve:
((MyObjectType)checkedListBox1.Items(index)).Name = "whatever"您必须知道要更改的对象的索引。您只需必须覆盖
ToString
类中的方法,以便它返回此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 thisName
property value.It will then display its name when added to your
CheckedListbox
.