Nadav
Nadav

Reputation: 575

How to make the last added item to a checkedlistbox automatically selected

I'm using winforms and i'm pretty new to it,

can any one help me with:

How to make the last added item to a checkedlistbox automatically selected?

Every new item I add to the checkedlistbox, i want it to be selected.

Thank you!

Nadav

Upvotes: 0

Views: 853

Answers (3)

Ali Tarhini
Ali Tarhini

Reputation: 5358

In your code, add this line after you add an item:

checkedlistbox1.SelectedIndex = checkedlistbox1.Items.Count-1

Upvotes: 1

Bolu
Bolu

Reputation: 8786

work with ControlAdded event of your checkedListBox and use Items.count-1 to get the last item added.

Upvotes: 1

Ian
Ian

Reputation: 34489

Well you can do a number of things:

1) Set the checked state of the item when you add it to the CheckedListBox manually.

CheckedListBoxItem cbl = new CheckedListBoxItem ();
this.checkListBox.Items.Add(cbl);
clb.Checked = true;

2) Create a sub-class of the CheckedListBox and override the 'Add' method to check the item just before adding it.

3) Create a sub-class of the CheckedListBox that exposes an ItemAdded event, wire up to that event and then set the checked state of the item.

Upvotes: 2

Related Questions