Using the Click event inside an IF

Asked

Viewed 114 times

-1

Hello, I wonder if it is possible to use a Click event inside an IF. I will try to explain my situation. At the moment I have this:

foreach (Label lblCor in panel1.Controls)

    if (lblNum.Text == lblCor.Text)
    {
        lblCor.Click += new EventHandler(lblCor_Click);
        lblCor.BackColor = Color.Orange;
    }

    private void lblCor_Click(object sender, EventArgs e)
    {

    }

I am trying to make it so that when lblNum.text equals lblCor.Text, and click lblCor, it will change color. It will be possible to do this?

Thank you

2 answers

1


After creating the events, do the comparison there.

private void lblCor_Click(object sender, EventArgs e)
{
    var lbl = (Label)sender;

    if (lbl.Text == lblNum.Text)
    {
        lbl.BackColor = Color.Orange;
    }
}
  • The problem is that I have different panels and each of these panels has 27 Labels. And this way I can’t use this for all Labels

  • 1

    If you have different panels you have to foreach (which adds the event) to each one of them... or find another way to create the event for all the Slides...or even manually via you_form.designer.Cs

-2

Try this verification within the method lblCor_Click. This way you wouldn’t need to put the launch of the event inside the loop.

Ex:

private void lblCor_Click(object sender, EventArgs e)
{
        if (lblNum.Text == lblCor.Text)
        {
             lblCor.Click += new EventHandler(lblCor_Click);
             lblCor.BackColor = Color.Orange;
        }
}

This way when the user performs the click event there will be the desired comparison and color change.

  • 1

    Did you notice that lblCor is a list item?

  • It’s not working out as I’d hoped, but thank you

Browser other questions tagged

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