Reputation: 2507
I have a FlowLayoutPanel that I fill with a custom UserControl
, and I have a TextBox
at the top of the form that I would like to use to filter the results. Each UserControl
stores it's properties, but I wasn't sure how to filter using those properties.
For example, say my UserControl
contains something like this:
// snip..
public string Text { get; set; }
public string Description { get; set; }
//snip..
How would I then go about taking the entry from the TextBox
and comparing it against both [usercontrol].Text
and [usercontrol].Description
? It has to be searched inside the text, not just from the beginning.
Once I've filtered the appropriate results, I would like those to be the only ones visible. Do I have to flush them all and rebuild it with only the applicable ones, or can I just remove the ones that don't match the filter?
I know this may be a very noob question, I just don't know where to start with it. Any ideas?
Upvotes: 1
Views: 1719
Reputation: 1360
You could loop through all of the user controls on the TextBoxChanged
event and if it does not match your criteria, set the visibility to collapsed. It would look something like this:
private textBoxTextChanged(obj sender, EventArgs e)
{
foreach(UserControl uc in flowLayoutPanel.Children)
{
if(!uc.Text.Contains(textBox.Text) && !uc.Description.Contains(textBox.Text))
{
uc.Visibility = Visibility.Collapsed;
}
else
{
//Set Visible if it DOES match
uc.Visibility = Visibility.Visible;
}
}
}
Upvotes: 3