Demonstration of working code:
Yourquestionisverysimpleanditisveryeasytodevelopasolutionforthisthatyouneed.
I'llgiveyouabase,thealgorithmpracticallydone,you'llneedtoadaptsomethingsandgiveanimprovementaswell.Therearealotthatcanbeimproved,thelimitisyourimagination.
Well,let'stakethesteps.
About"preparation"
Create a panel to place these buttons for games. If you use FlowLayoutPanel
the elements will always be repositioned from left to right (see the second image of the next item). This will make it easier to find them and will also allow you to separate your screen into different pieces. In my example, the panel is named mainPanel
.
Set (on all buttons) how each of them will be identified as a given game. In the example, I used the property Tag
to set this and left the Text
property for display only (note that some buttons show short names, but the search works for the actual game name).
Thedashedpartisthepanel.Inthenextimage,thepropertiestab,lookatthevaluesofthepropertiesText
andTag
ofthelastbuttonofthefirstrow.
TheinterestingthingaboutusingtheTag
propertyistobeabletousetheactualgamenametodothesearch,buttoshowthebuttononlyitsabbreviation.Forexample:
Definewhenthesearchwilltakeplace.Intheexample,IdefinedthatthesearchwouldoccurwhenevertheusertypedsomethingintheTextBox
fetch,usingtheevent TextChanged
.
About the code
At form initialization (in the constructor, after the InitializeComponents()
method), you must create a list with all the buttons that exist inside the main panel. This will allow you to remove the buttons from the panel without losing them, because of course it will be necessary to put them there again.
Now comes the part that does the work, the implementation of the TextChanged
event. The algorithm is simple and naive.
A filter is made in the _todosBotoes
variable (the one that was created at form
initialization) using Linq, this filter looks for buttons whose Tag
property is a text and contains the text that was typed by the user. I still gave a crafted one and caused the search to be done by the property Text
, only if < Tag
property% is null
or can not be converted to string
.
All controls on the main panel are removed.
The controls returned by the filter are added in the main panel.
Notice that I used a method called ContainsIgnoreCase()
. This method does not exist in class string
, is an extension method created based on the algorithm of this response and serves so that the comparison treats tiny and upper-case characters as the same.
Follow the code
public partial class Form1 : Form
{
private readonly Button[] _todosBotoes;
public Form1()
{
InitializeComponent();
// Passo 1
_todosBotoes = mainPanel.Controls.OfType<Button>().ToArray();
}
private void txtBusca_TextChanged(object sender, EventArgs e)
{
// Passo 2.1
var controles = _todosBotoes.Where(bt => (bt.Tag as string ?? bt.Text).ContainsIgnoreCase(txtBusca.Text))
.ToArray();
// Passo 2.2
mainPanel.Controls.Clear();
// Passo 2.3
mainPanel.Controls.AddRange(controles);
}
}
Method code ContainsIgnoreCase()
public static bool ContainsIgnoreCase(this string source, string search)
{
return source.IndexOf(search, StringComparison.CurrentCultureIgnoreCase) >= 0;
}