I searched in stack overflow something like this "How to change Panel's border color vb.net" and no results found so, I removed the vb.net and just typed like that and I found results but it is for C# only and I don't C# that much better and maybe I thought I could translate but I just thought translating will not be 100% Accurate so, that's why I made this question. Please help me how do I change the Panel's border Color in VB.Net I've set the BorderStyle FixedSingle in properties but there is still nothing I can do to change the Panel's border color. Please help and tell me how to change the Panel's border color or we can't do it from properties and we can do it by coding then at least please give me the code.
Asked
Active
Viewed 6,122 times
-1
-
You don't change the border colour. If you set the `BorderStyle` to `FixedSingle` then you live with what you get. The alternative is to draw a border yourself using GDI+, probably calling `Graphics.DrawRectangle` in the `Paint` event handler or in the `OnPaint` method of a custom control. We don't just give you the code here at SO. It's up to you to do the research and make an attempt, then post a question if you encounter a specific issue. That you don't know how is a reason to find out how, not to give up and expect us to tell you how. If you try and fail, then we can talk again. – jmcilhinney Jul 07 '20 at 08:59
1 Answers
5
As you already mentioned, there's a c# version of this question with multiple answers.
Here's a short summary of the answers:
Possibility 1
The simplest and codeless way is as follows:
- Set the
BackColor
ofPanel1
to the desired bordercolor - Set the
Padding
ofPanel1
to the desired border-thickness (e.g.2;2;2;2
) - Create a
Panel2
insidePanel1
and set theDock
-property toFill
- Set the
BackColor
ofPanel2
to the desired background color
Caveat: Transparent background can not be used.
Possibility 2
Draw a Border inside the Paint
event-handler.
(Translated to VB.NET from this answer.)
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
ControlPaint.DrawBorder(e.Graphics, Panel1.ClientRectangle, Color.DarkBlue, ButtonBorderStyle.Solid)
End Sub
Possibility 3
Create your own Panel
-class and draw the border in the client area.
(Translated to VB.NET from this answer.)
<System.ComponentModel.DesignerCategory("Code")>
Public Class MyPanel
Inherits Panel
Public Sub New()
SetStyle(ControlStyles.UserPaint Or ControlStyles.ResizeRedraw Or ControlStyles.DoubleBuffer Or ControlStyles.AllPaintingInWmPaint, True)
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Using brush As SolidBrush = New SolidBrush(BackColor)
e.Graphics.FillRectangle(brush, ClientRectangle)
End Using
e.Graphics.DrawRectangle(Pens.Yellow, 0, 0, ClientSize.Width - 1, ClientSize.Height - 1)
End Sub
End Class

MatSnow
- 7,357
- 3
- 19
- 31