Adding checkbox controls to list box - c#

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

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.

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.

Related

How to make a listbox in c# not duplicate items on button click?

Well, i'm using ASP.NET c# listbox. I also have a button, the idea is the following: whenever I click the button an item x has to be shown in the listbox. However, I click the button many times, and instead of replacing the x value in the list for the new value (pretend it's y), it shows the following:
x
y
The expected result would be:
y
I mean, i don't want values to be on top of eachother, just one at a time.
You should be having a code behind file where the selected items (selected by clicking the button) should be getting added to the listBox object.
When you run a loop to add items to this listBox object, use the Contains method to check if the listBox object already contains the current item. Add only if it does not contain.
At code behind :
Before that you need to create list and store all the listbox value. if listbox doesn't have value then keep list as null.
Every time you need to check whether the list contain the value or not if list contain value then you don't need to add, if not contain then you need to add.
in Jquery :
on button click first get all the listbox list item value and check that in that list whether that list contain the value or not which you want to add.
May be this is help full for you.

How to remove an item from a list box based on a text box value

I am writing a program that keeps track of employee check in and check out times. There are two list boxes, one for checked in employees and the other for checked out employees. My question is how do I remove the name from the check in list box and populate the check out list box when a employee inputs his/her user id in a text box?
I am not sure where to begin. I know how to populate a list box from a data reader but not from a text box and I don't know how to remove a specific item from a list box. Any suggestions on how to use list boxes in this manner?
ListBox has an Items property that you can add and remove elements from.
listBox.Items.Add("new item");
listBox.Items.Remove("old item");
If you are binding your ListBox directly to a DataReader you can't directly manipulate the list of Items - instead you would modify the underlying datasource and let the binding refresh the UI.
To search for an items in a list
if (listBox.Items.Contains(searchvalue)) {
listBox.Items.Remove(searchValue);
}
Please refer to following link for adding item into listbox:
http://msdn.microsoft.com/en-us/library/aa288403%28v=vs.71%29.aspx
You can use any desired event and then add text from textbox to listbox using following statement.
listBox1.Items.Add(((TextBox)sender).Text);
For removing item from listbox refer to the following:
C# removing items from listbox

How can I group items and add label to each group in CheckedListBox in C#

I am using CheckedListBox control to display categorized items in my application. I can add items to the list but cannot categorize them. To be more specific, I want to group items and add a category label to each group.
Currently I am using SqlDataReader to add items to the control.
Can anyone help me get around this, please?
Thanks in advance.
i don't think there is a inbuilt option to group items in CheckedListBox but you can try with ListView with CheckBoxes property set as true. ListView is supporting for grouping as well.
How to: Group Items in a Windows Forms ListView Control

listview c#: how to add items to columns other than the first

My question is an exact duplicate of this:
"To add items to column 1 in my listView control (Winform) I'm using listView1.Items.Add, this works fine but how do I add items to columns 2 and 3 etc? "
Lots of similar Q&A elsewhere, but none of them talk about how to add items using the WinForms interactive listView builder as opposed to coding it directly. I know how to code it, but since you can add items to the first column using the builder, I assume there must be a way to do it for the other columns.
Right-click on the ListView and select "Edit Items..." to get the ListViewItem Collection Editor.
Select a ListViewItem (or click Add to add one and then select it). In the properties pane find SubItems in the Data category. Click on the "..." button to open the ListViewSubItem Collection Editor and you can add sub-items, which appear in the columns after the first.
You need to set the Text property of your ListViewItems and ListViewSubItems if you want to see anything.
I think (not sure) that most simple way is to add array of strings, and the list view knows the column according to the array index.

Displaying a value in a populated combo box

Here is the situation:
I have populated a combo box with the names of divisions in my company and it is working fine. I go into edit mode and pull one record out of the data table so I bind all controls on the form with one record. I also populate this combo box with all divisions but want it to display the selected division. While I know how to display correct date in textbox controls, I do not know how to make combo box display only selected data. It displays the first record from the query which populates it. Any suggestions?
Thanks
Not clear if you are using ASP.NET or Windows Form. I am assuming Windows Form at the moment since it has an actual ComboBox control, while ASP.NET only has DropDownList (not counting the AJAX Control Toolkit).
ComboBox has a bunch of Selected... properties, i.e. SelectedIndex, SelectedItem, SelectedValue, SelectedText that you can manipulate (set) to show a certain item on the screen. So you can just do cbDivision.SelectedText = myRecord.Division (assuming Division in myRecord contains the same name as the one bound in the ComboBox.
for reference, see: this
I'm not sure I understood your question correctly. But I think you just want to display the selectedValue in the dropdown list populated from a list of values.
< asp:DropDownList DataTextField="SomeDecsription" DataValueField="SomeValue" ...... />
I apologise if this is not what you were asking for

Categories