Click here to Skip to main content
15,888,090 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what i am trying to do is
1)Whenever i move my mouse over button it's color should be Blue.
2)when my mouse leaves the button it's color should become normal.
3)Whenever i move my mouse over button it's color should be Blue
4)Then when i click that button its color should be changed to Red.
5)after button click when my mouse leaves the button its color should be red itself.
6)next time when i move my mouse over the button it's color should become blue.
7)Then when my mouse leaves the button its color should be red itself

Here i got all the 6 steps and now i need to get the 7th step.. how do i do it.

Thank you in advance.

What I have tried:

for 1,2,3,5 and 6 steps
 private void btnDateTime_MouseHover(object sender, EventArgs e)
{
if (btnDateTime.BackColor != Color.Empty && btnDateTime.BackColor != Color.Red)
            {
                btnDateTime.BackColor = Color.Blue;
            }
            
            if (btnDateTime.BackColor == Color.Red)
            {
                btnDateTime.BackColor = Color.Blue;
            }
}


for 4th step
private void btnDateTime_Click(object sender, EventArgs e)
       {
           btnHi.Visible = true;
           if(btnDateTime.BackColor == Color.Blue)
           {
               btnDateTime.BackColor = Color.Red;
           }
           else
           {
               btnDateTime.BackColor = default(Color);
           }
       }

for 7th step what should i do??
Posted
Updated 20-Apr-17 22:15pm
v2

1 solution

try

bool isclicked = false;
       private void button1_Click(object sender, EventArgs e)
       {
           (sender as Button).BackColor = System.Drawing.Color.Red;
           isclicked = true;
       }

       private void button1_MouseHover(object sender, EventArgs e)
       {
           (sender as Button).BackColor = System.Drawing.Color.Blue;
       }

       private void button1_MouseLeave(object sender, EventArgs e)
       {
           System.Drawing.Color defaultColor = System.Drawing.SystemColors.Control;
           (sender as Button).BackColor = isclicked ? System.Drawing.Color.Red: defaultColor

       }
 
Share this answer
 
Comments
Member 13097185 21-Apr-17 4:31am    
Thank you it is great code
Karthik_Mahalingam 21-Apr-17 4:33am    
welcome
Member 13097185 21-Apr-17 4:38am    
Thank you it is great code.But after that 7th step on button click how to get back to normal color?
Karthik_Mahalingam 21-Apr-17 5:01am    
what is after 7th?
on what basis
Member 13097185 21-Apr-17 5:12am    
(sender as Button).BackColor = System.Drawing.Color.Red;
isclicked = true;

after this step i need to get back to default color on button click

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