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
Solved

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

Posted on 2006-11-06
6
215 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
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.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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