26

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

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

Stewbob
  • 16,759
  • 9
  • 63
  • 107

3 Answers3

35

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 here by Andrej Tozon

orandov
  • 3,256
  • 3
  • 32
  • 32
13

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.

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!

Stewbob
  • 16,759
  • 9
  • 63
  • 107
  • how to use this? `myLabel.BorderStyle = nomLabel` ? – AdorableVB Dec 04 '13 at 09:20
  • @AdorableVB, nomLabel is a custom control, not a border style. You would use nomLabel instead of the standard Label control. – Stewbob Dec 04 '13 at 11:44
  • yup, got it. created mine yesterday, but, the border is fixed. I want that `ButtonBorderStyle.Solid` when mouse hovered and `.None` when mouse Leave. code does not work, and if I use custom control, it does not apply the code either. – AdorableVB Dec 05 '13 at 00:50
8

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.

rie819
  • 1,249
  • 12
  • 19