Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add value to checkedListBox items

Is it possible to add to checkedListBox item also value and title

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

How to add also value?

like image 948
Jurijs Visockis Avatar asked Dec 30 '15 17:12

Jurijs Visockis


2 Answers

Try setting the DisplayMember and ValueMember properties. Then you can pass an anonymous object like so:

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

Edit:

To answer your question below, you can create a class for your item like so:

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

And then add them like this:

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

And then you can get the value like this:

(checkedListBox1.Items[0] as MyListBoxItem).Value
like image 56
TrueEddie Avatar answered Oct 19 '22 20:10

TrueEddie


checkedListBox1.Items.Insert(0, new ListBoxItem("text", "value"));
like image 22
Shaher Jamal Eddin Avatar answered Oct 19 '22 20:10

Shaher Jamal Eddin