尝试更改标签的边框颜色

Ste*_*bob 25 .net vb.net custom-controls winforms

我在VB,VS2008工作,winforms.我有一些标签要创建,我正在使用BorderStyle = FixedSingle.

有没有办法改变这个边框的颜色?它总是默认为黑色.

ora*_*dov 33

如果您不想创建自定义控件,可以尝试以下操作:

连接到Label的绘画活动.

void label1_Paint(object sender, PaintEventArgs e)
{
    ControlPaint.DrawBorder(e.Graphics, label1.DisplayRectangle, Color.Blue, ButtonBorderStyle.Solid);
}
Run Code Online (Sandbox Code Playgroud)

Andrej Tozon这里拍摄


Ste*_*bob 12

我结合了robin.ellis和orandov的解决方案,得到了对我来说最好的结果.我创建了一个继承Label对象然后覆盖OnPaint事件的自定义控件.

Public Class nomLabel
   Inherits Label

  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
      MyBase.OnPaint(e)

      ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, myColor, ButtonBorderStyle.Solid)
   End Sub

End Class
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助!


rie*_*819 8

我也遇到了这个问题并最终使用了一种解决方法.

创建一个自定义控件,该控件由包含在面板中的标签组成.

然后,您可以使用面板创建边框并将其颜色更改为您想要的任何颜色.

我发现在你的应用程序中包装所有控件都是一个好主意(虽然有点耗时),因为当找到你需要一个自定义属性,或者更改为你所有类型的控件时,你可以只更改基本控件和整个应用程序更改.