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

Reply
 
Thread Tools Search this Thread
  #1  
Old 07-08-2009
Member
 
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.

Thanks!
Reply With Quote
  #2  
Old 07-08-2009
Member
 
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:

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

      this.lstSelected.Items.Clear();

      // Loop through the CheckedItems collection of the CheckedListBox

      // and add the items in the Selected ListBox

      foreach (string item in this.chkListPossibleValues.CheckedItems)
      {
         this.lstSelected.Items.Add(item.ToString());
      }

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

Code:
public Form1()
{
   //

   // Required for Windows Form Designer support

   //

   InitializeComponent();

   // Fill the CheckedListBox

   this.chkListPossibleValues.Items.Add("One");
   this.chkListPossibleValues.Items.Add("Two");
   this.chkListPossibleValues.Items.Add("Three");
   this.chkListPossibleValues.Items.Add("Four");
   this.chkListPossibleValues.Items.Add("Five");
   this.chkListPossibleValues.Items.Add("Six");
   this.chkListPossibleValues.Items.Add("Seven");
   this.chkListPossibleValues.Items.Add("Eight");
   this.chkListPossibleValues.Items.Add("Nine");
   this.chkListPossibleValues.Items.Add("Ten");
}
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
  #3  
Old 07-08-2009
Member
 
Join Date: Apr 2008
Posts: 22
Re: C# Listbox control

I think this is what you are looking for.

http://www.microbion.co.uk/developer...%20listing.pdf

I hope this helps you.
GunPoint
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags:



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.