Reputation: 145
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
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
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