C# .NET - How to bind values with CheckedListBox in Windows application?

Asked By Eswaran Radhakrishnan on 20-Jan-09 04:10 AM

Hi all,

I am fetching the country list and I am adding the country names in the CheckedListBox one by one as below. Country list is a one of the list collection.

for(int i=0; i<countryList.Count;i++)

{ checkedListBoxCountry.Items.Add(countryList[i].ToString(); }

But, I want to take the countryId when the user choose any one of the country from the list.

How to do that?

Thanks

R. Eswaran.

Try this

ram kumar replied to Eswaran Radhakrishnan on 20-Jan-09 04:25 AM

Hi,

By default the CheckedListBox displays the default string for the items it contains. However, the CheckedListBox can display diverse types of objects and you may want to change the displayed string by specifying the DisplayMember property. If the specified property does not exist, or the value of the DisplayMember property is an empty string (""), the results of the object's ToString method are displayed instead.

If the new value of the DisplayMember cannot be set, the previous value is maintained.

internal System.Windows.Forms.CheckedListBox CheckedListBox1;

private void InitializeCheckedListBox()
{
    this.CheckedListBox1 = new CheckedListBox();
    this.CheckedListBox1.Location = new System.Drawing.Point(40, 90);
    this.CheckedListBox1.CheckOnClick = true;
    this.CheckedListBox1.Name = "CheckedListBox1";
    this.CheckedListBox1.Size = new System.Drawing.Size(120, 94);
    this.CheckedListBox1.TabIndex = 1;
    this.CheckedListBox1.SelectionMode = SelectionMode.One;
    this.CheckedListBox1.ThreeDCheckBoxes = true;

    foreach ( Control aControl in this.Controls )
    {
        this.CheckedListBox1.Items.Add(aControl, false);
    }

    this.CheckedListBox1.DisplayMember = "Name";
    this.CheckedListBox1.Items.Add(CheckedListBox1);
    this.Controls.Add(this.CheckedListBox1);
}

Rams

Reply

alice johnson replied to Eswaran Radhakrishnan on 20-Jan-09 11:25 AM
there are multiple ways you could do it. Here is one.
 
'CREATE AN ARRAY WITH AN ELEMENT FOR EACH CHECKED ITEM
        Dim CheckedIDs(CheckedListBox1.CheckedItems.Count - 1) As String
         'LOOP THE CHECKED ITEMS, PUTTING THE ID IN THE ARRAY
        For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
            CheckedIDs(i) = CheckedListBox1.CheckedItems.Item(i).ToString
            i += 1
        Next
         'SHOW THE VALUES, JOINED WITH A "," AS A STRING
        MessageBox.Show(String.Join(","c, CheckedIDs))

Reply

alice johnson replied to Eswaran Radhakrishnan on 20-Jan-09 11:29 AM

Try this sample:

Dim CheckedIndexes As New ArrayList

Dim CheckedIds As New ArrayList

Dim i As Integer

For Each item As String In CheckedListBox1.CheckedIndices

CheckedIndexes.Add(item)

Next

For i = 0 To CheckedIndexes.Count - 1

CheckedListBox1.SelectedItems.Clear()

CheckedListBox1.SelectedIndex = CheckedIndexes.Item(i)

CheckedIds.Add(CheckedListBox1.SelectedValue)

Next

CheckedListBox1.SelectedItems.Clear()