-1

I need a label which does something different with every click.

 private void open_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 5; i++)
        {
            builder = new StringBuilder(4);
            builder.Append(zahl1.Text);
            builder.Append(zahl2.Text);
            builder.Append(zahl3.Text);
            builder.Append(zahl4.Text);

            code = builder.ToString();

        }
        if( code== setCode)
        {
            openAndClose.BackColor = Color.DarkGreen;
            setNewCode.Visible = true;

        }
        else
        {

        }

    }

With the first click the BackColor gets green and the visible is true. And now it should go back in the start position if I click it again. That means BackColor should be red and the visible should be false. Can I do this wth a second Eventhandler?

openAndClose.Click += new EventHandler(open_Click);

Thanks

aha364636
  • 365
  • 5
  • 23
  • use boolean data type, first click set it to true, second click check if the var is true then load the previous colors – Saubar May 14 '15 at 10:49

3 Answers3

0

You should be able to get what you want by having a global field which denotes if the label has been clicked before hand or not.

In short, initially set your flag to false, do something like so:

EventHandler() 
{
     if(!flag) 
     {
          BackColour = Green
          Visible = true          
     }
     else
     {
          BackColour = Red
          Visible = false          
     }
     flag = !flag
}

Attaching multiple event handlers will simply invoke multiple event handlers each time.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • But It should first check if the code is right and if it's right it should change the color and if you cklick again it should get back to the start and the if should work again. – aha364636 May 14 '15 at 10:58
  • @aha364636: In that case, you will need to add these checks before hand and act accordinly. – npinti May 14 '15 at 11:01
0

I think you do not even need a extra boolean or the check. You can just check if visible then hide, if not visible make it appear

Revils
  • 1,478
  • 1
  • 14
  • 31
0

You could simply do the following:

if(code == setCode)
{
    openAndClose.BackColor = openAndClose.BackColor == Color.DarkGreen ? Color.Red : Color.DarkGreen;
    setNewCode.Visible = !setNewCode.Visible;
}

The first part toggles the color between green and red, and the second part toggles the visibility.

Jako Basson
  • 1,461
  • 12
  • 19
  • I always need to click the setNewCode twice before I am able to change it to green.Do you know if it is from your Code? – aha364636 May 14 '15 at 16:23
  • Is code == setCode on the first click? If not then that code wont run according to your example. So see if you can debug the code to see what the value is. – Jako Basson May 14 '15 at 17:47