Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Thread Tools Search this Thread
Old 07-08-2009
Join Date: Aug 2009
Posts: 1
C# Listbox control

I have created two list boxes on windows form and displayed the list of drives on one list box. What is the possible way if the user clicks on any drive type, listbox2 should be populated with the list of files under that selected drive from listbox1.

Reply With Quote
Old 07-08-2009
Join Date: Aug 2008
Posts: 50
Re: C# Listbox control

ListBox Example

Example with both a ListBox and a CheckedListBox. The user can check items in the CheckedListBox and then click a button which will move the checked items to the normal ListBox.

1. Open a new project in Visual Studio.NET called Lists. Add a ListBox, a CheckedListBox and a button to the form and change the names as shown in the picture below:

2. Change the Text property of the button to "Move".

3. Change the CheckOnClick property of the CheckedListBox to true.

Adding the Event Handlers

Now we are ready to add some code. When the user clicks the Move button, we want to find the items that are checked, and copy Those into the Selected List Box.

Double-click the button and enter this code:

private void btnMove_Click(object sender, System.EventArgs e)
   // Check if there are any checked items in the CheckedListBox

   if (this.chkListPossibleValues.CheckedItems.Count > 0)
      // Clear the ListBox we'll move the selections to


      // Loop through the CheckedItems collection of the CheckedListBox

      // and add the items in the Selected ListBox

      foreach (string item in this.chkListPossibleValues.CheckedItems)

      // Clear all the checks in the CheckedListBox

      for (int i = 0; i < this.chkListPossibleValues.Items.Count; i++)
         this.chkListPossibleValues.SetItemChecked(i, false);
We start by checking the Count property of the CheckedItems collection. This will be greater than zero if any items in the collection are checked. We then clear all items in the Selected list box, and loop through the CheckedItems collection, adding each item to the Selected list box. Finally, we remove all the checks in the CheckedListBox.

Now we just need something in the CheckedListBox to move. We could add the items while in design mode, by selecting the Items property in the property panel and adding the items there. Instead, we'll add the items through code. We do so in the constructor of our form:

public Form1()

   // Required for Windows Form Designer support



   // Fill the CheckedListBox

Just as you would do if you were to enter the values through the properties panel, you use the Items collection to add items at runtime.

Is this is what you are trying to do?

You can Show the list of files & folders in second Listbox on selection in the first checked listbox.
Reply With Quote
Old 07-08-2009
Join Date: Apr 2008
Posts: 22
Re: C# Listbox control

I think this is what you are looking for.


I hope this helps you.
Reply With Quote

  TechArena Community > Software > Software Development

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads for: "C# Listbox control"
Thread Thread Starter Forum Replies Last Post
How to create ListBox in VB.net? MarceloQuad Software Development 4 22-01-2010 08:44 PM
ListView or ListBox Heather5 Software Development 3 29-08-2009 12:10 AM
HTML Listbox ActiveX Control Chrisch Software Development 3 11-08-2009 03:04 PM
I have a problem with listbox StoaVio Software Development 2 25-10-2008 03:33 PM
Project VBA Form Control "ListBox" KaSaRa Microsoft Project 1 04-08-2007 03:57 AM

All times are GMT +5.5. The time now is 12:49 AM.