tags:

views:

109

answers:

2

I have an instance of System.Windows.Forms.CheckedListBox which displays a list of tick boxes and I have some other System.Windows.Forms objects in my application. I would like to, depending on what the user selects using the other System.Windows.Forms items, show or hide different items in the System.Windows.Form.CheckedListBox. How could I achieve this?

Note: this is for a windows desktop application, not a webpage.

A: 

Hi Richard,

I added a checked list box control to a window form. This is a very basic example. I also added a button. When the button is clicked it removed the first item. Here is the code that is in my button's event handler to remove the first item:

if (checkedListBox1.Items.Count > 0)
{
   checkedListBox1.Items.RemoveAt(0);
}

You can modify this code to suit your needs.

Brendan Vogt
Ok I'll try this soon.I'm not 100% that familiar with how things work with rendered form elements...but if this is a desktop application, does css stuff like 'display: none' work?
Richard Maguire
@Richard. Yes it would. Put the code into a web page to give it a test in your web browser.
Brendan Vogt
That's the prob though, it's not a webpage, it's a desktop application. I know that css code works for webpage elements, but does it work for desktop applications?
Richard Maguire
CSS won't work in a windows application. Give me a moment then I will get some code posted to you :)
Brendan Vogt
Please see my update to my original answer.
Brendan Vogt
Please accept as answer if this has helped you in any way. It also increases your acceptance rate.
Brendan Vogt
+2  A: 

There is no easy way to hide an item in a CheckedListBox, you have to remove it, like Brendan Vogt showed you.

An alternative is to take advantage of data binding. It is not supposed to work for CheckedListBox, the documentation of the DataSource property says:

This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.
Gets or sets the data source for the control. This property is not relevant for this class.

However, I used it in the past, and it works fine. So if you assign a DataView as the DataSource for the list, you can filter its items using the RowFilter property

DataView view = new DataView(productsDataTable);
checkedListBox.DataSource = view;
checkedListBox.DisplayMember = "Name";
...

// Hide discontinued products
view.RowFilter = "Discontinued = False";
Thomas Levesque
Awesome! Thank you :)
Richard Maguire