web-dev-qa-db-ja.com

checkedListBoxアイテムに値を追加する方法

checkedListBoxアイテムに値とタイトルも追加できますか

checkedListBox1.Items.Insert(0,"title");    

どのように付加価値を付けますか?

8
Jurijs Visockis
checkedListBox1.Items.Insert(0, new ListBoxItem("text", "value"));
7

DisplayMemberプロパティとValueMemberプロパティを設定してみてください。次に、次のように匿名オブジェクトを渡すことができます。

checkedListBox1.DisplayMember = "Text";
checkedListBox1.ValueMember = "Value";
checkedListBox1.Items.Insert(0, new { Text= "text", Value = "value"})

編集

以下の質問に答えるには、次のようにアイテムのクラスを作成します。

public class MyListBoxItem
{
    public string Text { get; set; }
    public string Value { get; set; }
}

そして、次のように追加します。

checkedListBox1.Items.Insert(0, new MyListBoxItem { Text = "text", Value = "value" });

そして、あなたはこのような値を得ることができます:

(checkedListBox1.Items[0] as MyListBoxItem).Value
4
TrueEddie