C# Recover all dynamic buttons

Asked

Viewed 65 times

0

I have a project in C# and I’m having difficulties.

I need to retrieve the data from the dynamically generated buttons at the same time. In the project there are approximately 50 buttons to open different forms in a panel and when I click on a button I wanted them to change color and all the other 49 buttons to be transparent, more as they are generated dynamically I could not, Is it possible to make a simultaneous call to these buttons that should be transparent? When I use Sender I can only recover one button at a time. Someone can help?

1 answer

0

you can scroll through all the buttons of the form, using a foreach. I made an example that you can adapt to your situation;

Avoid posting code print, show the formatted code and preferably full (in the part that matters) so that we can reproduce the same code for a reply.

Following example:

for (int i =1; i <= 50; i++)
{
    Button b = new Button();
    b.Tag = i;
    b.Text = "Button " + i;
    b.Name = "btn" + i;
    flowLayoutPanel1.Controls.Add(b);
    b.Click += (ss, ee) => { 
    
        foreach (Button btn in flowLayoutPanel1.Controls.OfType<Button>())
        {
            if (btn == b)
                btn.BackColor = Color.Blue;
            else
                btn.BackColor = Color.Yellow;
        }
    
    };
}

Upshot:

inserir a descrição da imagem aqui

obs. The first button I used to call this code, and it’s outside the flowLayoutPanel, so it didn’t enter the loop.

  • Really it is better to put all the code here so I will edit the question to better understand my problem OK

  • I think I completely changed the question of the initial question, I do not know if I leave the answer here rs...

  • Truth Rovann is that it is the first time put here. You think it best to delete the question and start another with the two separate doubts?

  • gives a read on community materials how to ask, how not to ask, and https://pt.meta.stackoverflow.com/questions/8045/gui-survivor%C3%aancia-do-stack-overflow-em-portugu%C3%aas? cb=1

Browser other questions tagged

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