1

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?

1
  • 1
    FYI - You are not overriding here, you are subscribing to the events. Use the override keyword on OnMouseLeave and OnMouseEnter to override them correctly. Commented Nov 12, 2009 at 20:37

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.