.net 尝试更改标签的边框颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/987668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:51:28  来源:igfitidea点击:

Trying to change the Border Color of a label

.netvb.netwinformscustom-controls

提问by Stewbob

I'm working in VB, VS2008, winforms. I've got some labels to create, and I'm using the BorderStyle = FixedSingle.

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

Is there any way to change the color of this border? It is always defaulting to black.

有没有办法改变这个边框的颜色?它始终默认为黑色。

回答by orandov

If you don't want to create a custom control you can try this:

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

Hook up to the Label's paint event.

连接到标签的绘制事件。

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

Taken from hereby Andrej Tozon

来自这里安德烈Tozon

回答by Stewbob

I combined the solutions from robin.ellis and orandov to get a result that worked the best for me. I created a custom control that inherited the Label object and then overrode the OnPaint event.

我结合了 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

Thanks for the help!

谢谢您的帮助!

回答by rie819

I ran into this problem as well and ended up using a workaround.

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

Create a custom control which consists of a label wrapped in a panel.

创建一个自定义控件,其中包含一个包含在面板中的标签。

You can then use the panel to create your border and change it's color to whatever you want.

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

I've found that it's a good idea (although a little time consuming) to wrap all controls in your application anyways, because when it comes to finding out you need a custom property, or change to all of your controls of that type, you can just change the base control and your entire app changes.

我发现无论如何将所有控件包装在应用程序中是一个好主意(虽然有点耗时),因为当发现需要自定义属性或更改为该类型的所有控件时,您只需更改基本控件即可更改整个应用程序。