Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create an alert effect on a button by cycling between two colors randomly.

What I have tried:

private void timer1_Tick(object sender, EventArgs e)
       {
           Random rand = new Random();
           int A = rand.Next(256);
           int R = rand.Next(256);
           int G = rand.Next(256);
           int B = rand.Next(256);
           NotificationBtn.BackColor = Color.FromArgb(A, R, G, B);
       }


But this cycles between a lot of colors i need only two colors.
Posted
Updated 29-Nov-19 16:02pm
Comments
j snooze 29-Nov-19 17:24pm    
Why not create 2 random colors, store them in an array with 2 elements, and flip between the 2? or make color variables set each one to a random color and them flip between those 2 variables?
RickHamton 29-Nov-19 17:31pm    
but how would i incorporate the timer to specify when the color changes to the other.

1 solution

int timeleft = 2;
        private void timer1_Tick(object sender, EventArgs e)
        {
            Color color1 = Color.Red; 
            Color color2 = Color.WhiteSmoke; 

            if (timeleft > 0)
            {
                NotificationBtn.BackColor = color1;
                timeleft = timeleft - 1;
            }
            else
            if(timeleft == 0)
            {
                NotificationBtn.BackColor = color2;
                timeleft = timeleft + 2;
            }
        }
 
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