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