Jur*_*kis 8 c# checkedlistbox winforms
是否可以添加checkedListBox
项目的值和标题
checkedListBox1.Items.Insert(0,"title");
Run Code Online (Sandbox Code Playgroud)
如何添加还有价值?
checkedListBox1.Items.Insert(0, new ListBoxItem("text", "value"));
Run Code Online (Sandbox Code Playgroud)
尝试设置 DisplayMember 和 ValueMember 属性。然后你可以像这样传递一个匿名对象:
checkedListBox1.DisplayMember = "Text";
checkedListBox1.ValueMember = "Value";
checkedListBox1.Items.Insert(0, new { Text= "text", Value = "value"})
Run Code Online (Sandbox Code Playgroud)
编辑:
要在下面回答您的问题,您可以为您的项目创建一个类,如下所示:
public class MyListBoxItem
{
public string Text { get; set; }
public string Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后像这样添加它们:
checkedListBox1.Items.Insert(0, new MyListBoxItem { Text = "text", Value = "value" });
Run Code Online (Sandbox Code Playgroud)
然后你可以得到这样的值:
(checkedListBox1.Items[0] as MyListBoxItem).Value
Run Code Online (Sandbox Code Playgroud)