Azeem Khalid
Azeem Khalid

Reputation: 145

Adding checkbox controls to list box

I'm new to .NET environment and just a student. I'm working on User Management. I want to assign multiple roles to one user. For this purpose I've created list box which contains a list of roles from database.

 lbRoles.Items.Add(readerRole["RoleName"].ToString());

I just need a check box with each item. Please suggest how to add a checkbox with each item. I did tried

lbRoles.Controls.Add(checkBox);
lbRoles.Items.Add(readerRole["RoleName"].ToString());

But it was not helpful. I did google but no result :(

Upvotes: 9

Views: 60262

Answers (2)

Koryu
Koryu

Reputation: 1381

There is the CheckedListBox class, its very simple and does exactly what you want. :)

Displays a ListBox in which a check box is displayed to the left of each item.

Upvotes: 15

Tom Heard
Tom Heard

Reputation: 1278

Instead of using a ListBox, use a ListView instead and set ListView.Checkboxes to true.

This will place a CheckBox next to each item in the ListView, and your users can select specific items in the ListView by clicking the checkboxes, then get the selected items using ListView.SelectedItems.

Upvotes: 8

Related Questions