- 1 year ago
How to make a listbox display items from a checked list and radio buttons? - C# Windows Form
To make a ListBox
display items from a checked list and radio buttons in a C# Windows Form, you can use the following steps:
Create a
CheckedListBox
control to hold the items that will be displayed in theListBox
.Create a
RadioButton
control for each filter option that you want to provide. For example, if you want to filter the items in theCheckedListBox
based on their type, you can create two radio buttons for "Fruits" and "Vegetables".Add an event handler for each radio button that will filter the items in the
CheckedListBox
based on the selected filter.When a filter is selected, clear the
ListBox
and add the filtered items from theCheckedListBox
.
Here is some sample code to get you started:
csharpprivate void radioButton1_CheckedChanged(object sender, EventArgs e)
{
// Filter by "Fruits"
listBox1.Items.Clear();
foreach (string item in checkedListBox1.CheckedItems)
{
if (item.Contains("Fruit"))
{
listBox1.Items.Add(item);
}
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
// Filter by "Vegetables"
listBox1.Items.Clear();
foreach (string item in checkedListBox1.CheckedItems)
{
if (item.Contains("Vegetable"))
{
listBox1.Items.Add(item);
}
}
}
In this example, checkedListBox1
is the CheckedListBox
that holds the items to be displayed in the ListBox
, and listBox1
is the ListBox
that will display the filtered items. The event handlers for each radio button check the CheckedItems
property of the CheckedListBox
, filter the items based on the selected filter, and add them to the ListBox
.