我想根据传递给表单构造函数的项(在本例中是一个列表<int>)填充一个CheckedListBox。
我的框架代码是:
foreach (int platypus in listPlatypi)
{
userFriendlyPlatypusName = ExpandFromPlatypusID(platypus);
// I want to store a verbose string in an Item of the CheckedListBox, something like:
// Item item = new Item(userFriendlyPlatypusName); // what data type should "Item" be?
CheckedListBox1.Add(item);
}发布于 2012-07-20 00:41:14
发布于 2012-07-20 00:52:52
答案取决于您在所列出的框架代码之外所做的工作。重要的是您的代码在以后对列表项执行操作时需要哪些信息。
CheckedListBox的工作方式与ListBox类似。显示的文本是每个项目的.ToString()的结果。
如果字符串有效,则添加显示名称文本。
如果您需要为每个项目存储更多信息,请向您的类添加一个ToString()重写并对整个项目执行.Add()操作。
如果这不是一个选项,那么创建一个小的显示包装:
public class PlatypusDisplayWrapper {
public Platypus {get; set;}
public override string ToString() {
return this.Platypus.Name;
}
}https://stackoverflow.com/questions/11564676
复制相似问题