Solved

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

Posted on 2006-11-06
6
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

689 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