-
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!
-
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.
-
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
Page generated in 1,713,517,616.10803 seconds with 10 queries