-1

I am trying to make a button that will set the text of another button when clicked. But when I do this it requires a second click to do the second action, which is setting the buttons text.

    private void button2_Click(object sender, EventArgs e)
    {
        // Button has been clicked.
        if(a == 1){
            // The button has been clicked only once
            button2.Text = "Repeat: Off";
            button6.Text = "Shuffle: Off";
            loop = false;
            shuffle = false;
            a = 2;
        } else if(a == 2) {
            // The button has been clicked only twice :c
            button2.Text = "Repeat: On";
            button6.Text = "Shuffle: Off";
            loop = true;
            shuffle = false;
            a = 1;
        }
    }

I'm not sure how this happens.

ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
Jacob
  • 69
  • 14
  • Did you set a breakpoint and step through your method while watching how your variable values change? – O. R. Mapper Jun 10 '15 at 20:29
  • Possible duplicate of http://stackoverflow.com/questions/13486245/winforms-how-to-call-a-double-click-event-on-a-button – JGV Jun 10 '15 at 20:31
  • did you intialize A!=0? – TaW Jun 10 '15 at 20:32
  • 1
    @VimalanJayaGanesh: I think in this case here, the OP does *not* want to have to double-click to execute their action. – O. R. Mapper Jun 10 '15 at 20:35
  • You need to initialize both the Flaf (a) and the butons' Trxts in a way that they fit. As you haven't done that the first click only synchronizes. After that all further work well, right? Just change the intial value of a! – TaW Jun 10 '15 at 20:54

2 Answers2

0

Your first action (if condition) is true when a==1. Your second action (else if) is true when a==2.

You are setting value of a = 2 in your first action (if condition).

So when you click your button for the first time it sets the value of a to 2 and only the first action (if condition) gets executed. And when you click the same button again it now executes the second action (else if) as the first click now sets the value of a to 2.

rageit
  • 3,513
  • 1
  • 26
  • 38
  • We don't know the intial value of a. probably 0?? – TaW Jun 10 '15 at 20:36
  • @TaW, it can't be 0, otherwise it would never do anything at all. (its an else-if, not an else)... – Ron Beyer Jun 10 '15 at 20:39
  • Well, that's right :-) - otoh, the problem often is that the intial state of the flag and the states of other things (the Texts) don't fit.. – TaW Jun 10 '15 at 20:41
0

You are proxying the current state of your button with the variable a, which is not generally a good idea. You should instead just check the state of your button (or whatever properties it modifies). Have a look at this implementation, noting that I removed the Shuffle stuff because it was constant:

private void button2_Click(object sender, EventArgs e)
{
    loop = !loop;
    if (loop)
    {
        button2.Text = "Repeat: On";
    }
    else
    {
        button2.Text = "Repeat: Off";
    }
}
Brian Driscoll
  • 19,373
  • 3
  • 46
  • 65