Solved

changing backcolor of button to red when users move their mouse over the button

Posted on 2006-11-06
6
213 Views
Last Modified: 2010-05-18
Hi,
I am using visual studio 2005 (C#). I want a situation where when users move their mouse over a button the backcolor changes to red. How can I achieve this please?


Sirdots.

0
Comment
Question by:Sirdots
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 17885815
Is this a button on a windows form or on an asp.net webpage ?
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17885839
Button button1  = new Button();
Color c = button1.BackColor;
button1.MouseEnter += new System.EventHandler(this.button1_MouseEnter);
button1.MouseLeave += new System.EventHandler(this.button1_MouseLeave);

private void button1_MouseEnter(object sender, System.EventArgs e) {
      this.button1.BackColor = Color.Red;
}

private void button1_MouseLeave(object sender, System.EventArgs e) {
      this.button1.BackColor = c;
}
0
 

Author Comment

by:Sirdots
ID: 17885847
This is a button on a windows form please
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:ozymandias
ID: 17885856
see above
0
 
LVL 2

Accepted Solution

by:
cybercrew earned 250 total points
ID: 17887607
1. Open Visual Studio 2005
2. Press CRTL-SHIFT-N
3. Under "Project Types" make sure C# is Selected.
4. Select the "Windows Application" Template
5. Under the name TextBox change "WindowsApplcation1.cs" to "ButtonColor.cs"
6. Make Sure "Create Directory for Solution" is checked.
7. Press the Ok Button.
8. Click on a Button so that is selected in your Toolbox then click your Windows Form
9.  Make sure the button on the form is selected and press CRTL-W-P to display the properties window
10. On top of the Properties Window there is a lightning bolt that represents Events for an object. Click it to see the events for the button.
11. The two events you need to use are MouseHover (or MouseEnter) and the MouseLeave Event. Double Click the empty space next to MouseHover.
12. You should have been taken to the code view and a new Event Handler is created. Enter the Code :   buton1.BackColor = Color.Red;
Your Code should look what I have below:

private void button1_MouseHover(object sender, EventArgs e)
        {
            button1.BackColor = Color.Red;
        }

13.  Press Shift-F7 So that you are back to Designer View
14.  Double-Click  on the empty space next to the MouseLeave event
15.  Back in code view under the newly created event for MouseLeave enter this code: button1.BackColor = SystemColors.ButtonFace;
Your Code should look like what I have below:
 private void button1_MouseLeave(object sender, EventArgs e)
        {
            button1.BackColor = SystemColors.ButtonFace;
        }

16. Press the F5 key to run your program and see if you get your desired results.
0
 

Author Comment

by:Sirdots
ID: 17898742
Thanks to everyone
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c# string handling 1 27
SQL Exceptions 3 39
Importing CSV file encoded in UTF-8 in SQL SERVER 2008 using BULK INSERT 5 31
Close form "before" open 3 27
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now