A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
ListBox and CheckedListBox
Here we'll cover the aspects of C#'s list boxes and checked list boxes (ListBox and CheckedListBox controls).
On Saturday, May 8th 2004 at 07:41 PM
By Andrew Pociu (View Profile)
(Rated 4.4 with 125 votes)
Using the ListBox
We'll start with a 'Windows Application' project in Microsoft Visual C# .NET. Name it 'ListBoxes'.
To your newly created form add from the Toolbox a ListBox named 'itemList', a TextBox named itemName and three buttons named 'cmdAdd', 'cmdRem' and 'cmdClr'.
Your form should now look similar to this one:
All the action is started by the last 3 buttons we have added, of course.
New items can be added to the list by the programmer by selecting the list box and using the 'Items' property.
A window pops up, where you can enter new items to be displayed in the list box. Each line represents a new item.
Now arrives the important part: coding. We'll use the Click event of the 'cmdAdd' button to add new items to the list. Of course, the name of the item will be the text in the 'itemName' text box.
Double click the 'cmdAdd' button to get into the cmdAdd_Click event directly and use the following code:
Using the CheckedListBox
Similar to its base class, ListBox but allows items to be checked using the checkbox next to each item, this way you can select multiple items.
Create a new project named 'CheckedListBoxes' and add to it a CheckedListBox and a ListBox. Name them 'checkedList' and 'selectedList'. Now add some items to checkedList (you can add new items just like you do when using a simple ListBox - from Properties choose Items, in the window that pops up every line represents a new item).
After you add some items to 'checkedList' your form should look similar to this:
We want every item that is checked in 'checkedList' to be added to the 'selectedList'.
This is easy to code by using the 'ItemCheck' event of our CheckedListBox.
With our CheckedListBox ('checkedList') selected go to the Events section and scroll until you find the ItemCheck event.
Double click the empty box next to ItemCheck. Microsoft Visual C# will automatically add the 'checkedList_ItemCheck' next to ItemCheck and the Event window will now look like in the picture above. Also Visual Studio will bring you to the part where you need to code:
First we check to see the NewValue of the changed item represented by the 'e' argument. If its CheckState is Checked we can add it to the list by using the selectedList.Items.Add() method we learned earlier about.
We also need to convert the SelectedItem using ToString().
Else, if the item was unchecked we remove it in a manner similar to the one we used to add it, just that now we use the Remove() method.
The items are added to the ListBox in the order in which you select them.
If you have any questions comment below or post in the forums.
Stay tunned with Geekpedia.com for more upcoming tutorials .
|Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList|
Rate this tutorial
There are no related tutorials.
Related Source Code
There is no related source code.
C# Job Search