none
Change button back color after click, and change it back after 2nd click and so on RRS feed

  • Question

  • I've 1 Button.

    how to do
    1. after button clicked, the back color setup in Red
    2. 2nd button clicked, if back color is in red color, the button back color become as normal

    how the if else statement looks like?

    Me only have this,

    if (clickedButton.BackColor = Color.Red)
    {
    clickedButton.BackColor = System.Drawing.Color.Red;
    }
    else
    {

    }

    Wednesday, October 28, 2009 4:14 PM

Answers

  • Hi,

    Check this code and let me know. Hope it works for your requirement.


     int i = 0;
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (i == 0)
                {
                    this.BackColor = Color.Red;
                    i++;
                }
                else if(i == 1  && this.BackColor == Color.Red)
                {
                    button1.BackColor = SystemColors.Control;
                    i++;
                }
            }


    Regards,
    Malleswar
    • Marked as answer by Bonusnet Wednesday, October 28, 2009 4:49 PM
    Wednesday, October 28, 2009 4:36 PM

All replies

  • Hi,

    Check this code and let me know. Hope it works for your requirement.


     int i = 0;
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (i == 0)
                {
                    this.BackColor = Color.Red;
                    i++;
                }
                else if(i == 1  && this.BackColor == Color.Red)
                {
                    button1.BackColor = SystemColors.Control;
                    i++;
                }
            }


    Regards,
    Malleswar
    • Marked as answer by Bonusnet Wednesday, October 28, 2009 4:49 PM
    Wednesday, October 28, 2009 4:36 PM
  • i have a better solution, i believe.

    old thread but atleast i can help others  - 

    int i = 0;
            private void button1_Click(object sender, EventArgs e)
            {
                i++;
                switch(i)
                {
                    case 1:
                        this.BackColor = Color.Red;
                        break;
                    case 2:
                        button1.BackColor = SystemColors.Control;
                        i = 0;
                        break;
                }
            }

    Thursday, March 14, 2019 11:51 AM
  • uses   a loop
    Thursday, March 14, 2019 11:52 AM