2
My question is the following, I am developing a program (I am beginner) in C#. The part I wanted to improve on is the following: I’m wanting to create different events in a for structure. For example:
public frmSelecaoDeCartas()
{
InitializeComponent();
// Declara arrays contendo os Botões
Button[] btn = { button2, button3, button4 };
// Inicia uma estrutura de repetição para gerar os eventos
for (int i = 0; i < btn.Length; i++)
{
// Cria o evento do Button de índice I com o nome de btnNum (Num = 0 a 4)
btn[i].Click += btnNum_Click;
// Evento com o código (Problema nessa parte, quero trocar a palavra Num por
// números de acordo com a mudança da índice i (i++)
void btnNum_Click(object sender, EventArgs e)
{
MessageBox.Show(CartasInformacao[i], "Informações",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
Would look like this:
btn[i].Click += btnNum_Click;
void btn1_Click(object sender, EventArgs e) { }
void btn2_Click(object sender, EventArgs e) { }
// E assim vai...
Is that possible? If yes, help me? Thank you!
Instead of posting a print of your code, post the code in full, so it looks better to analyze it.
– gato
But that’s the code, the rest is just logic and code of the program itself... Nothing to do with the code I printed, and what I printed shows all my doubts. But here’s a bigger print: http://prntscr.com/gtt3as
– Lucas Bittencourt
@Lucasnaja but putting the code snippet here makes it easier for us to help you, even easier to copy and paste to text
– Luiz Santos
When you add to one button it works?
– Luiz Santos
Yeah, thanks for the tip. The code goes: https://pastebin.com/v382kTPF Currently there are 3 buttons, when I run the code, the three are with the same code, because they are in the same event (btn_Click) For example, I click the button 2 and the same message appears by clicking the button 3 I wanted to create an event for each button using the for, not the same event for each button
– Lucas Bittencourt