Ronan Mcdonald
Ronan Mcdonald

Reputation: 13

How to make an event span over multiple forms

The project I am working on needs a couple buttons on multiple forms, instead of doing the code shown below I was hoping it could be made global.

This is only one part of the project, all the code does is enlarge the button picture when the user hovers over it.

I've tried looking at classes, tags and attributes. I know classes can be made to use across multiple forms but I cant find out if they work with events.

private void btnEnter_MouseEnter(object sender, EventArgs e)
{
   Button btn = (Button)sender;
   btn.Size = new Size(299, 102);
}

private void btnLeave_MouseLeave(object sender, EventArgs e)
{
   Button btn = (Button)sender;
   btn.Size = new Size(289, 92);
}

Upvotes: 0

Views: 92

Answers (1)

Handbag Crab
Handbag Crab

Reputation: 1538

You can create an inherited button. Add a new class then make sure you put : Button after the class name.

using System.Drawing;
using System.Windows.Forms;

namespace InheritedButton
{
    public class ExpandButton : Button
    {
        public Size EnterSize { get; set; }
        private Size _LeaveSize;
        public Size LeaveSize
        {
            get
            {
                return (_LeaveSize);
            }
            set
            {
                _LeaveSize = value;
                this.Size = LeaveSize;
            }
        }

        public ExpandButton() : base()
        {
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            this.Size = EnterSize;
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            this.Size = LeaveSize;
            base.OnMouseLeave(e);
        }
    }
}

Build your project and the new button will appear in the toolbox. Drop it onto a form/control and make sure you set the EnterSize and LeaveSize. EnterSize determines the size of the button when you mouse over and LeaveSize sets the initial size and sets the size of the button when you mouse out. You don't need to set the Size property, just set LeaveSize.

Any time you want to use the expanding/contracting button just use the inherited one instead.

Upvotes: 1

Related Questions