Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 525
Azeem Khalid Avatar asked Dec 14 '13 08:12

Azeem Khalid


People also ask

Which control is used to control a textbox and list box?

The label control is used to display a text or a message to the user on the form. The label control is normally used along with other controls. Common examples is wherein a label is added along with the textbox control. The label gives an indication to the user on what is expected to fill up in the textbox.

What is the difference between ListBox and CheckBox?

It's identical to the ListBox control, but a check box appears in front of each item. The user can select any number of items by selecting the check boxes in front of them. As you know, you can also select multiple items from a ListBox control by pressing the Shift and Ctrl keys.


2 Answers

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

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

like image 152
Koryu Avatar answered Sep 20 '22 05:09

Koryu


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.

like image 43
Tom Heard Avatar answered Sep 20 '22 05:09

Tom Heard