tags:

views:

72

answers:

3

I have create the class HoverButton which derives from Form.Button. Here I override the OnMouseEnter/OnMouseLeave events.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DSLiteWizardLib
{
    class HoverButton : Button
    {
        #region Constructor
        public HoverButton()
        {
            InitializeComponent();
            bMouseHover = false;
        }
        #endregion

        #region Methods
        private void OnMouseEnter(object sender, System.EventArgs e)
        {
            bMouseHover = true;
        }
        private void OnMouseLeave(object sender, System.EventArgs e)
        {
            bMouseHover = false;
        }
        private void InitializeComponent()
        {
            this.MouseEnter += new System.EventHandler(this.OnMouseEnter);
            this.MouseLeave += new System.EventHandler(this.OnMouseLeave);
        }
    }
}

Eventually I want to pass an image for the hover state, pressed state, etc.

How can I get the button that is placed on my Form to use my HoverButton class instead of the standard Form.Button class?

+2  A: 

If you're using a seperate assembly to store your control, you could right click the control toolbar and add an item for your assembly. You could then drag and drop your control just like any of the other builtin control.

If you're looking for something a little less elegant, you could go into the Designer.cs file and change the type of the button from Button to HoverButton there.

Justin Niessner
A: 

If you look in your toolbox in your Form Designer you should see the HoverButton that you created. (After a successful build of course.) You then drag it onto your form just like a regular button.

Paul Sasik
A: 

How to add and create a Usercontrol in Visual Studio 2008

NOTE: For the control to show in the toolbox automatically, make sure to set this option in VS:

Tools > Options > Windows Forms Designer > General : AutoToolboxPopulate

Philip Wallace