Solved

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

Posted on 2006-11-06
6
211 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
Comment Utility
Is this a button on a windows form or on an asp.net webpage ?
0
 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
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
Comment Utility
This is a button on a windows form please
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
see above
0
 
LVL 2

Accepted Solution

by:
cybercrew earned 250 total points
Comment Utility
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
Comment Utility
Thanks to everyone
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Aspnet xml 2 36
c# string 6 39
C# Winform store and retrieve array of bytes to Properties,Settings.Default.... 7 30
ConsoleSql 1 12
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…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

10 Experts available now in Live!

Get 1:1 Help Now