VB.Net - 更改面板的边框颜色
VB.Net - Change the Panel's Border color
我在堆栈 溢出 中搜索了类似“如何更改面板的边框颜色 vb.net” 的内容,但没有找到结果,所以我删除了 vb.net就这样输入,我找到了结果,但它仅适用于 C#,我对 C# 没有那么好,也许我认为我可以翻译,但我只是认为翻译不会 100% 准确 所以,这就是我提出这个问题的原因。请帮助我如何在 VB.Net 中更改面板的边框颜色 我已经在属性中设置了 BorderStyle FixedSingle 但仍然无法更改面板的边框颜色.请帮助并告诉我如何更改面板的边框颜色,或者我们不能通过属性来完成,我们可以通过编码来完成,至少请给我代码。
正如您已经提到的,c# version of this question 有多个答案。
以下是答案的简短摘要:
可能性 1
最简单无代码的方式如下:
- 将
Panel1
的 BackColor
设置为所需的边框颜色
- 将
Panel1
的 Padding
设置为所需的边框粗细(例如 2;2;2;2
)
- 在
Panel1
中创建一个 Panel2
并将 Dock
-属性 设置为 Fill
- 将
Panel2
的 BackColor
设置为所需的背景颜色
警告:不能使用透明背景。
可能性2
在 Paint
事件处理程序中绘制边框。
(从 this answer 翻译成 VB.NET。)
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
可能性3
创建您自己的 Panel
-class 并在客户区绘制边框。
(从 this answer 翻译成 VB.NET。)
<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
我在堆栈 溢出 中搜索了类似“如何更改面板的边框颜色 vb.net” 的内容,但没有找到结果,所以我删除了 vb.net就这样输入,我找到了结果,但它仅适用于 C#,我对 C# 没有那么好,也许我认为我可以翻译,但我只是认为翻译不会 100% 准确 所以,这就是我提出这个问题的原因。请帮助我如何在 VB.Net 中更改面板的边框颜色 我已经在属性中设置了 BorderStyle FixedSingle 但仍然无法更改面板的边框颜色.请帮助并告诉我如何更改面板的边框颜色,或者我们不能通过属性来完成,我们可以通过编码来完成,至少请给我代码。
正如您已经提到的,c# version of this question 有多个答案。
以下是答案的简短摘要:
可能性 1
最简单无代码的方式如下:
- 将
Panel1
的BackColor
设置为所需的边框颜色 - 将
Panel1
的Padding
设置为所需的边框粗细(例如2;2;2;2
) - 在
Panel1
中创建一个Panel2
并将Dock
-属性 设置为Fill
- 将
Panel2
的BackColor
设置为所需的背景颜色
警告:不能使用透明背景。
可能性2
在 Paint
事件处理程序中绘制边框。
(从 this answer 翻译成 VB.NET。)
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
可能性3
创建您自己的 Panel
-class 并在客户区绘制边框。
(从 this answer 翻译成 VB.NET。)
<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