Doubt about looking for buttons

Asked

Viewed 44 times

0

The idea is:

  1. In the field of "Fetch Game...", I look for the buttons, which are already named with their respective games: btnArcheAge, btnDiablo3, btnWoW, etc. The tags are also already named with the real name of each game.

  2. When I search for "Archeage" for example, the rest of the buttons disappear and only remains it to be clickable.

Obs: The buttons were placed on a FlowLayoutPanel


I need a little push with the code, I don’t know where to start. This is the photo I have of the old post they made for me.

inserir a descrição da imagem aqui

Program and code

inserir a descrição da imagem aqui

public partial class Form1 : Form
{
    private readonly Button[] _todosBotoes;
    public Form1()
    {
        InitializeComponent();
        //Passo 1
        _todosBotoes = mainPanel.Controls.OfType<Button>().ToArray();
    }

    private void pictureBox3_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void bunifuImageButton1_Click(object sender, EventArgs e)
    {

        if(panel1.Width == 350)
        {    

            panel1.Visible = true;
            panel1.Width = 65;

        }

    }

    private void btnVoltar_Click(object sender, EventArgs e)
    {
        if(panel1.Width == 65)
        {

            panel1.Visible = true;
            panel1.Width = 350;


        }

    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {

        if (panel1.Width == 350)
        {
            btnVoltar.Hide();

        }
        else
        {

            btnVoltar.Show();
        }

    }

    public static bool ContainsIgnoreCase(this string source, string search)
    {
        return source.IndexOf(search, StringComparison.CurrentCultureIgnoreCase) >= 0;
    }


    private void txtBuscarJogo_TextChanged(object sender, EventArgs e)
    {
        //Passo 2.1
        var controles = _todosBotoes.Where(btnArcheAge => (btnArcheAge.Tag as String ?? btnArcheAge.Text).ContainsIgnoreCase(txtBuscarJogo.Text)).ToArray();
        //Passo 2.2
        mainPanel.Controls.Clear();
        //Passo 2.3
        mainPanel.Controls.AddRange(controles);
    }

    private void lblAmigos_Click(object sender, EventArgs e)
    {

    }


}

}

  • What is your difficulty?

  • Just like calling a specific button and the others disappear, as soon as I erase what is written all come back. Just like it is in 1° gif. I don’t know how to call a button, being what is typed in the textbox.

  • What you’ll have to do is put inside the txtBuscarJogo_TextChanged functions with if and string contains, then put to the buttons that do not contain disappear and the ones that contain change position.

1 answer

0


Well, I made all the code for you in the other post, just missed you understanding a little part.

As I said in one of the items

Define when the search will take place. In the example, I set that the search would occur whenever the user typed something in the search Textbox, using the event TextChanged.

You already have the code that does the search (in the other post), now just need to put it inside the event TextChanged of TextBox. By the names in your code, it must be txtBuscarJogo_TextChanged.

  • Where do I put this public Static bool Containsignorecase(this string source, string search) { Return source.Indexof(search, Stringcomparison.Currentcultureignorecase) >= 0; }

  • Wherever you want.

  • http://prntscr.com/f5l08m. What I’m doing wrong?

  • I don’t know, the print doesn’t show the error.

  • I edited the code passed by the current one above. The error is this :http://prntscr.com/f5l1gr

  • Create the extension method ContainsIgnoreCase in a static class (exactly as the error tells).

  • This way is right.

  • Haha hard to use this, but it would be very helpful. I’ll try more here...

  • But it opens another question with the error of now. But it isolates this error, right. Shows the extension code, the code you use the method and the error message. Much simpler to help you like this =)

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.