Click here to Skip to main content
16,016,306 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm creating a Form project which must add a button dynamically and within an event it must change Form background color to red with first click, and then, with the second click to green.
Below is the code



public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int X = 100;
private int Y = 200;
Button btn = new Button();
private void Form1_Load(object sender, EventArgs e)
{
this.Controls.Add(btn);
btn.Location = new Point(X, Y);
btn.Text = "Click";
btn.Click += new EventHandler(Mouse_Click);
}


private void Mouse_Click(Object s, EventArgs e)
{
// how to do here?
}


}
Posted

Assuming you want the Button to continue switching colors with every Click, and that you do not need to preserve the original Form BackColor on start-up:
C#
Color firstColor = Color.Red;
Color secondColor = Color.Green

bool whichColor = true;

private void btn_Click(Object s, EventArgs e)
{
    BackColor = whichColor ? firstColor : secondColor;

    whichColor = ! whichColor;
}
Or, you could simplify this to:
C#
private void button1_Click(object sender, EventArgs e)
{
    BackColor = (whichColor = ! whichColor) ? firstColor : secondColor;
}
You could simplify even further if you are sure you don't need access to the created Button outside the scope/context in which it was created at run-time:
C#
private void SomeForm_Load(object sender, EventArgs e)
{
    // outside of the Load Event you will have no access
    // to the Button you create here !
    Button newButton = new Button();
    
    newButton.AutoSize = true;
    newButton.Location = new Point(400, 100);
    newButton.Text = "Click to Change Form Background Color";
    newButton.ForeColor = Color.White;
    
    // anonymous EventHandler defined with Lambda expression
    newButton.Click += (obj, eArgs) =>
    {
        BackColor = (BackColor == Color.Red) ? Color.Green: Color.Red;
    };
    
    Controls.Add(newButton);
}
Note the interesting fact that if the Control you create, as a child Control of a Form, does not have its BackColor, set when you create it at run-time, it will "inherit" the BackGround Color of the Form, and as the form BackColor is changed, the Button will change BackGround Color also: I have never seen this behavior "officially" described by Microsoft !

But, that behavior was exactly why I set the ForeColor property of the Button to white: so that it would be visible whether the Button BackColor was red, or green :)
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 28-Sep-13 10:21am    
Sure, a 5.
—SA
Add a variable which suggests the click times.
Then in the function filed, write like this:


int ClickTime=0;
private void Mouse_Click(Object s, EventArgs e)
{
if(ClickTime==0)
{
this.BackColor=Color.Red;
ClickTime++;
}
if(ClickTime==1)
{
this.BackColor=Color.Green;
ClickTime++;
}
if(ClickTime==2)
{
//add your own codes here
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900