Solved

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

Posted on 2006-11-06
6
217 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Suggested Courses

734 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