2

I want to draw a border around a panel when the mouse is hovered on a particular button so that it will give a sort of highlight effect and on MouseLeave event the border will be gone. How to achieve this? I am new in Winform app development.

PS- I have drawn a rectangle in the panels paint event but i want to show the rectangle on mouse hover. I have tried Invalidate() but nothing happens.

K. Ajay
  • 357
  • 1
  • 4
  • 21
  • You can change [panel border color](https://stackoverflow.com/a/43419785/3110834) the same way that you can change [textbox border color](https://stackoverflow.com/a/43419785/3110834). – Reza Aghaei Aug 08 '18 at 12:27
  • To understand the difference between overriding `OnPaint` and handling `WM_NCPAINT`, see a scrollable panel. – Reza Aghaei Aug 08 '18 at 14:15

1 Answers1

2

please follow my answer closely, I've tested it locally, co it should be ok for you as well:

1) Add a new class to your project, name it MyPanel.cs
2) Replace default text of the class with:

[System.ComponentModel.DesignerCategory("Code")]
public class MyPanel : Panel
{
   public Pen MyPen;

    public MyPanel()
    {
        SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
        MyPen = Pens.Red;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        using (SolidBrush brush = new SolidBrush(BackColor))
            e.Graphics.FillRectangle(brush, ClientRectangle);
        e.Graphics.DrawRectangle(MyPen, 0, 0, ClientSize.Width - 1, ClientSize.Height - 1);
    }
}

This is inside a namespace of your application, don't forget about this

3) Go to Form1.cs [design]
4) Add a new button from the toolbox toolbox->button
5) Double click some free space inside the form to generate private void Form1_Load(object sender, EventArgs e)
6) Back in the designer, click the button once and in the properties box switch to events (small icon of a lighting bolt)
7) Double click MouseHover to generate private void button1_MouseHover(object sender, EventArgs e)

Now our setup is ready so let's put things into the action:

8) Instantiate MyPanel class right under public partial class Form1 : Form:

 MyPanel p = new MyPanel();

9) In private void Form1_Load(object sender, EventArgs e) we can setup this instance and add it to the form's controls:

        p.Location = new Point(10, 10);
        p.Size = new Size(100, 100);

        Controls.Add(p);

10) Last thing we need to do is to change border color on hover of button, so let's do that in private void button1_MouseHover(object sender, EventArgs e):

        p.MyPen = Pens.Yellow;
        p.Refresh();

And that's it :)

Ps: If you want to make the border gone then use MouseLeave event and change MyPen of the panel to Pens.Transparent

Matěj Štágl
  • 870
  • 1
  • 9
  • 27
  • This is working! However i have a panel having many labels, Think of it as i want to give that panel a border. How to wrap this panel around that ? is there any way ? – K. Ajay Aug 08 '18 at 09:07
  • @K.Ajay please accept and upvote my answer if possible – Matěj Štágl Aug 08 '18 at 10:10