C# 如何动态填充CheckedListBox?

C# 如何动态填充CheckedListBox?,c#,winforms,dynamic,checkedlistbox,C#,Winforms,Dynamic,Checkedlistbox,我想根据传递到表单构造函数中的项(在本例中为列表)填充CheckedListBox 我的主要代码是: foreach (int platypus in listPlatypi) { userFriendlyPlatypusName = ExpandFromPlatypusID(platypus); // I want to store a verbose string in an Item of the CheckedListBox, something like: //

我想根据传递到表单构造函数中的项(在本例中为列表
)填充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);
}

您在寻找吗?

答案取决于您在列出的框架代码之外所做的工作。重要的是在以后处理列表项时代码需要什么信息

CheckedListBox
的工作原理与
ListBox
类似。显示的文本是每个项目的
.ToString()
结果

如果字符串有效,则添加显示名称文本

如果需要为每个项目存储更多信息,请在类中添加一个
ToString()
重写,并
.add()
完整项目

如果这不是一个选项,请创建一个小的显示包装:

public class PlatypusDisplayWrapper {
   public Platypus {get; set;}
   public override string ToString() { 
       return this.Platypus.Name;
   }
}

哦,天哪,为什么我昨天不明白这一点现在令人难以置信和尴尬。不要尴尬:我5分钟前也做了同样的事情。谢谢@keyr的回答,谢谢你的提问。