locked
to set border color of a label RRS feed

  • Question

  • hi all,

    is there any way to set the border color of a label control in c#, windows based application?

    plz help .....

    thanks in advance

    divya

     

    Wednesday, March 14, 2007 3:15 PM

Answers

  • Hi,

    you could do some custom drawing on your label if you hook into label's Paint event:

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

    If you need this kind of border on a number of labels across your application, create your custom label by deriving from Label control and implement border painting there.

    Andrej

    Wednesday, March 14, 2007 5:40 PM

All replies

  • Hi,

    Are labels shown up with border in the UI? They have left,top and width ,height properties.

    But no border will be shown in the UI.

    Have u seen any label with border ?

    If so,it may be either other control(with modified style ,edit box or button or picture box) or any custom control.

     

    Thanx,

    Ch.T.Gopi Kumar.

    Wednesday, March 14, 2007 3:36 PM
  • No, there is no property to change black border color when BorderStyle is FixedSingle which probably is some windows theme color. What you can do is to create a usercontrol inherited from Label and override OnPaint and add this code:
    using (Pen p = new Pen(Color.Red))
    {
        g.DrawRectangle(p, ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width - 1, ClientRectangle.Height - 1);
    }
    Wednesday, March 14, 2007 3:51 PM
  • Hi,

    Then it becomes custom control,not standard label.Am i right?

     

    Thanx,

    Ch.T.Gopi Kumar.

    Wednesday, March 14, 2007 3:55 PM
  •  TilakGopi wrote:

    Hi,

    Then it becomes custom control,not standard label.Am i right?

     

    Thanx,

    Ch.T.Gopi Kumar.



    Problem is not that he can't set Label to show border, because that is posible and is used also. It is to change that standard black color to some other color. My answer was that there is no property to change that and a way how is posible.
    Wednesday, March 14, 2007 4:15 PM
  • Hi,

    you could do some custom drawing on your label if you hook into label's Paint event:

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

    If you need this kind of border on a number of labels across your application, create your custom label by deriving from Label control and implement border painting there.

    Andrej

    Wednesday, March 14, 2007 5:40 PM
  • Hi,

     

    I wanted to do this and after reading these messages I came up with a simple work-around. 

    • Creat a second label and made it just a bit bigger than the main label. 
    • Set the background color of the second label that you wanted the color you want your border.
    • Place the main label on top of the 'Border color' label and center it to create your color border.
      • You might have to bring the main label to the front.

     This looked really nice and you can make the border and width you want.

     

    I hope someone can use this idea. Have fun.

     

    Connie

    Thursday, July 12, 2007 11:17 PM