label forecolour

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
is it possible to have a label that has a word show in two different colors? such as the label displays the word "forum" with "for" in red and "um" in blue

if not what would be a good approach to accomplishing this? (making a new label control or using another object)

any suggestion would be awsome
 
Hi,
Not sure how much this will use full but try this.........

Private Sub labInfo_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles labInfo.Paint

' Create font and brush.

Dim drawFont As New Font("Arial", 10)

Dim drawBrush As New SolidBrush(Color.Blue)

' Create rectangle for drawing.

Dim x As Single = 1.0F

Dim y As Single = 2.0F

Dim width As Single = e.Graphics.MeasureString("Ritesh", drawFont).Width

Dim height As Single = labUm.Height

Dim drawRect As New RectangleF(x, y, width, height)

Dim blackPen As New Pen(Color.Black)

e.Graphics.DrawString("Ritesh", drawFont, drawBrush, drawRect)

drawBrush.Color = Color.Red

e.Graphics.DrawString("Jain", drawFont, drawBrush, width, y)

End Sub


I hope this will help u............................

Regards,
Ritesh Jain
 
actually that is a big help, i'm gonna use it to make a custom control, see the thing is that what i really need is two forecolour properties and then a 1st forecolour length (number of chars that get the first color, the rest are in the second colour) that can be changed at run time i'll post the control when i get around to actually making it
 
for those who care, here's that control i was talking about, it works exactly like a normal label as well as having two forecolours

basically i added a 2nd forecolor property and a ForeColor1Length property which means that however long you want the 1st forecolor to be is set here, the rest of the text property is displayed in the 2nd color

this attachment is the solution so y'alls can modify it as ya want, i dont care
btw if the font of the label is set to bold the last character of forecolor1 overlaps the first character of forecolor2 i didnt bother adding the stuff to correct it if bold is on or not

have fun
 
Back
Top