Results 1 to 3 of 3

Thread: C# Listbox control

  1. #1
    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!

  2. #2
    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.

  3. #3
    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

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,719,546,072.88845 seconds with 17 queries