1

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?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Connor Deckers
  • 2,447
  • 4
  • 26
  • 45

1 Answers1

3

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;
        }
    }
}
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Tom
  • 1,330
  • 11
  • 21
  • Good news: It works. Bad news: When iterating through almost 500 objects, it locks up for about a half minute. Is there any faster way of doing this? Or, perhaps, some messed up way of doing it in a `BackgroundWorker`? – Connor Deckers Jun 14 '12 at 02:24
  • Edit- I don't think it's iterating through so many objects that slows it down, I think it's actually changing the `Visibility` that's affecting it. Is there any way to make this smoother? – Connor Deckers Jun 14 '12 at 03:08
  • Right, that is an issue. Are you actually displaying all 500 objects at once? or would it make sense to limit the amount displayed? – Tom Jun 14 '12 at 13:48
  • Try this using a search delegate in a separate thread. This will free up the GUI and will offload the heavy work onto the delegate. http://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the – Tom Jun 14 '12 at 14:53