Click here to Skip to main content
13,705,153 members
Rate this:
 
Please Sign up or sign in to vote.
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 9-Jan-12 21:45pm
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?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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.
  Permalink  
Comments
RaisKazi 10-Jan-12 4:23am
   
My 5.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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];
}
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180920.1 | Last Updated 10 Jan 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100