Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have created one button in c#.
i just want to change the background color of the button on every click at runtime.colors should change randomly i.e; not in some fixed sequence.
Thank you..
Posted
Comments
Reiss 10-Jan-12 3:53am    
What platform are you using wpf, asp.net, winform etc?
Suraj S Koneri 10-Jan-12 3:56am    
Can you specify which button's color needs to get change is that webcontrol or windows control?

If you are using ASP.NET button or Windows button then here is the code to change background color randomly.

C#
protected void btnClick_Click(object sender, EventArgs e)
    {
        Random obj = new Random();
        int r = obj.Next(255);
        int g = obj.Next(255);
        int b = obj.Next(255);
        btnClick.BackColor = System.Drawing.Color.FromArgb(r,g,b);
    }


i hope it helps.
 
Share this answer
 
Comments
RaisKazi 10-Jan-12 4:23am    
My 5.
protected void Button1_Click(object sender, EventArgs e)
{
object[] newobj = new object[10];
newobj[0] = System.Drawing.Color.Red;
newobj[1] = System.Drawing.Color.Black;
newobj[2] = System.Drawing.Color.Blue;
newobj[3] = System.Drawing.Color.Yellow;
newobj[4] = System.Drawing.Color.Gray;
newobj[5] = System.Drawing.Color.Green;
newobj[6] = System.Drawing.Color.Crimson;
newobj[7] = System.Drawing.Color.Purple;
newobj[8] = System.Drawing.Color.Gold;
newobj[9] = System.Drawing.Color.Silver;
Random ran = new Random();
int j = ran.Next(10);

Button1.BackColor =(System.Drawing.Color) newobj[j];
}
 
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