Border Color in Label Controls

nakracreative

Well-known member
Joined
Aug 1, 2005
Messages
72
Location
India
Programming Experience
1-3
Is it possible to Have a border color in a label control in vb.net? If yes, then please explain it to me or suggest me some better idea of diaplaying aborder color in a label control. Anyone out there please help me out.
 
No is the short answer there.
 
you can make your own user control that inherits the windows.forms.label and then add a border complete with multiple colors and whatnot
 
All you have to do is to override onPaint event. However if you want to use such label in other projects as well then I suppose you should follow JuggaloBrotha's suggestion and make a user control


However, as I said you need to override OnPaint (say you want a label with red colour border:
i.e.
VB.NET:
 [/COLOR]
[FONT=Verdana][COLOR=blue][FONT='Courier New']Public[/FONT][/COLOR][FONT='Courier New'] [COLOR=blue]Class[/COLOR] MyLabel[/FONT][/FONT]
[FONT='Courier New'][FONT=Verdana]        [COLOR=blue]Inherits[/COLOR] Label[/FONT][/FONT]
[FONT='Courier New'][FONT=Verdana]        [COLOR=blue]Protected[/COLOR] [COLOR=blue]Overrides[/COLOR] [COLOR=blue]Sub[/COLOR] OnPaint([COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] PaintEventArgs)[/FONT][/FONT]
[FONT='Courier New'][FONT=Verdana]            [COLOR=blue]MyBase[/COLOR].OnPaint(e)[/FONT][/FONT]
[FONT='Courier New'][FONT=Verdana]            ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, Color.Red, 1, _[/FONT][/FONT]
[FONT='Courier New'][FONT=Verdana]            ButtonBorderStyle.Solid, Color.Red, 1, ButtonBorderStyle.Solid, Color.Red, 1, _[/FONT][/FONT]
[FONT='Courier New'][FONT=Verdana]            ButtonBorderStyle.Solid, Color.Red, 1, ButtonBorderStyle.Solid)[/FONT][/FONT]
[FONT='Courier New'][FONT=Verdana]        [COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT][/FONT]
[FONT=Verdana][FONT='Courier New']    [COLOR=blue]End[/COLOR] [COLOR=blue]Class[/COLOR][/FONT][COLOR=black]
[/FONT]

Then you must declare and instantiate the class myLabel … say you want to draw a label on load event:
VB.NET:
[B][/FONT][/COLOR][/B][/FONT]
[FONT=Verdana][COLOR=blue][FONT='Courier New']Dim[/FONT][/COLOR][FONT='Courier New'] myLabel [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] MyLabel[/FONT][/FONT]
[FONT='Courier New'][FONT=Verdana]myLabel.Location = [COLOR=blue]New[/COLOR] Point(20, 20)[/FONT][/FONT]
[FONT='Courier New'][FONT=Verdana]myLabel.Width = 150[/FONT][/FONT]
[FONT='Courier New'][FONT=Verdana]myLabel.Height = 20[/FONT][/FONT]
[FONT='Courier New'][FONT=Verdana]myLabel.TextAlign = ContentAlignment.MiddleCenter[/FONT][/FONT]
[FONT='Courier New'][FONT=Verdana]myLabel.Text = "Change border color"[/FONT][/FONT]
[FONT=Verdana][COLOR=blue][FONT='Courier New']Me[/FONT][/COLOR][FONT='Courier New'].Controls.Add(myLabel)[/FONT][/FONT]
[FONT='Courier New'][FONT=Verdana]


HTH
Regards ;)
 

Attachments

  • ChangeBorderColor.zip
    20.6 KB · Views: 99
to expand on kulrom's post

if you make the border-colored label control you'll want to add properties like:
BorderThickness
BorderColor
BorderEnabled
 
Back
Top