首页 > 解决方案 > VB.Net - 更改面板的边框颜色

问题描述

我在堆栈溢出中搜索了类似“如何更改面板的边框颜色 vb.net”的内容,但没有找到结果,我删除了 vb.net,然后像这样输入,我找到了结果,但它仅适用于 C#,我没有t C# 好多了,也许我认为我可以翻译,但我只是认为翻译不会100% 准确,所以这就是我提出这个问题的原因。请帮助我如何在VB.Net中更改面板的边框颜色我已经在属性中设置了 BorderStyle FixedSingle 但我仍然无法更改面板的边框颜色。请帮助并告诉我如何更改面板的边框颜色,否则我们不能从属性中做到这一点,我们可以通过编码来做到这一点,然后至少请给我代码。

标签: vb.netcolorsborderpanel

解决方案


正如您已经提到的,这个问题有一个带有多个答案的c# 版本。

以下是答案的简短摘要:

可能性 1

最简单无代码的方式如下:

  • BackColorof设置Panel1为所需的边框颜色
  • Paddingof设置Panel1为所需的边框厚度(例如2;2;2;2
  • 创建一个Panel2内部Panel1并将Dock-property 设置为Fill
  • BackColorof设置Panel2为所需的背景颜色

警告:不能使用透明背景。

可能性 2

Paint在事件处理程序 内绘制一个边框。(从这个答案
翻译成 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 并在客户区绘制边框。 (从这个答案
翻译成 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

推荐阅读