Adding checkbox controls to list box

57.5k views Asked by At

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 :(

2

There are 2 answers

2
Koryu On BEST ANSWER

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.

2
Tom Heard On

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.