1
Demonstration of code working:
Your question is very simple and it is very easy to develop a solution for that you need.
I’ll give you a basis, the algorithm practically ready, you’ll need to adapt some things and give an improved as well. There is much that can be improved, the limit is your imagination.
Well, let’s get to the steps.
On the "preparation"
Create a dashboard to place these game buttons. If you use a
FlowLayoutPanel
the elements will always be repositioned from left to right (see the second image of the next item). This will facilitate finding them and will also allow you to separate your screen into different pieces. In my example, the panel is calledmainPanel
.Set (on all buttons) how each of them will be identified as a particular game. In the example, I used the property
Tag
to define this and I left the propertyText
for viewing only (note that some buttons show abbreviated names, but the search works for the actual name of the game).The dashed part is the panel. In the image beside, the properties tab, attention to property values
Text
andTag
from the last button of the first line.The interesting thing about using the property
Tag
, is able to use the real name of the game to do the search, but show only its abbreviation on the button. For example:Define when the search will happen. In the example, I set that the search would occur whenever the user typed something in the
TextBox
search, using the eventTextChanged
.
About the code
- On form startup (in constructor, after method
InitializeComponents()
), you need to create a list of all the buttons that exist inside the main panel. This will allow you to remove the panel buttons without losing them because, of course, you will need to put them back there. Now comes the part that makes the work, the implementation of the event
TextChanged
. The algorithm is simple and naive.A filter is made on the variable
_todosBotoes
(the one that was created at theform
) using Linq, this filter searches for buttons whose propertyTag
be a text and contain the text that was typed by the user. I even gave a worked and made the search be done by the propertyText
, only if the content of the propertyTag
fornull
or cannot be converted tostring
.All main panel controls 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 the classstring
, is an extension method created on the basis of algorithm of this response and serves for comparison to treat lower and upper case characters as the same.
Follows 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;
}
What have you tried to do? It would be interesting for you to edit and add to the question.
– user28595
So.. I don’t know where to start...
– Dário Sabaini
But you understand what I want to do?
– Dário Sabaini
Yes, but without a starting point, the question can be considered broad. If you do not know where to start, it would be interesting to break into smaller doubts, try to create a layout organizing the buttons and go asking as you have difficulties.
– user28595