Answered by:
[HELP] Label possible change color and back color?

Question
-
Hi all, my code is:
Label17.Text = "Position: GK" & vbNewLine & "Overall Rating: 93"
I get this:
Is possible make something in code to change the forecolor in GK and the 93 color to red like this?
Thanks all.
Friday, June 12, 2015 12:58 AM
Answers
-
Hi,
It's impossible.
Each Label has one BackColor, cannot have more than one.
So, a simple solution is that you place four labels:
1. for "Position:", 2. for "GK" with Yellow BackColor, 3. for "Overall Rating:", and 4. "93" with Red ForeColor
code for changing BackColor or ForeColor is like this.
2. lblPosision.BackColor = Color.Yellow
4. lblRating.ForeColor = Color.Red
Best regards,- Edited by Ashidacchi Friday, June 12, 2015 4:33 AM
- Proposed as answer by Mr. Monkeyboy Sunday, June 14, 2015 4:06 AM
- Marked as answer by Youjun Tang Friday, June 19, 2015 2:58 AM
Friday, June 12, 2015 3:27 AM -
You can always draw your own with the label's paint event.
Option Strict On Public Class Form4 Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load Label1.AutoSize = False Label1.Size = New Size(140, 40) Label1.Font = New Font("Arial", 12) End Sub Private Sub Label1_Paint(sender As Object, e As PaintEventArgs) Handles Label1.Paint e.Graphics.Clear(Color.White) Dim t1 As String = " Position:" Dim t2 As String = "GK" Dim s1, s2 As New SizeF s1 = e.Graphics.MeasureString(t1, Label1.Font) s2 = e.Graphics.MeasureString(t2, Label1.Font) e.Graphics.DrawString(t1, Label1.Font, Brushes.Black, 0, 0) e.Graphics.FillRectangle(Brushes.Yellow, s1.Width, 0, s2.Width, s2.Height) e.Graphics.DrawString(t2, Label1.Font, Brushes.Black, s1.Width, 0) t1 = "Overall Rating:" t2 = "93" s1 = e.Graphics.MeasureString(t1, Label1.Font) e.Graphics.DrawString(t1, Label1.Font, Brushes.Black, 0, s2.Height) e.Graphics.DrawString(t2, New Font("Arial", 10, FontStyle.Bold), Brushes.Red, s1.Width, s2.Height + 2) End Sub End Class
- Marked as answer by Youjun Tang Friday, June 19, 2015 2:58 AM
Friday, June 12, 2015 4:46 AM
All replies
-
Hi,
It's impossible.
Each Label has one BackColor, cannot have more than one.
So, a simple solution is that you place four labels:
1. for "Position:", 2. for "GK" with Yellow BackColor, 3. for "Overall Rating:", and 4. "93" with Red ForeColor
code for changing BackColor or ForeColor is like this.
2. lblPosision.BackColor = Color.Yellow
4. lblRating.ForeColor = Color.Red
Best regards,- Edited by Ashidacchi Friday, June 12, 2015 4:33 AM
- Proposed as answer by Mr. Monkeyboy Sunday, June 14, 2015 4:06 AM
- Marked as answer by Youjun Tang Friday, June 19, 2015 2:58 AM
Friday, June 12, 2015 3:27 AM -
Ashidacchi's reply would probably be your simplest solution.
You can also see if in this thread How to add shadow effect to Label in Win Forms Application? the links for IronRazerz Custom Label class may have some capability you would want.
Trying to use a Labels paint event to paint text into the Label rather than setting Text into the Label would be a drawn out process in order to resize the Label based on the Text to use in it and then drawing the text with different colors and providing a rectangle of a certain color over drawn by some characters of another color.
So maybe this will work for you. You can use a RichTextBox which allows selected text to have its Backcolor and Color altered (Color being the ForeColor of the selected text). The mouse move event is used to focus another control rather than a RichTextBox so the user can't select text in an RTB.
But the text isn't centered like in a Label. I don't remember if an RTB has a property for that and forgot to look.
By splitting text on the Carriage Return from vbCrLf the Line Feed is in the measured string which makes the measured string twice as high as it would be otherwise. Else this code in the Button1 Click event
"RichTextBox1.Height = SomeSize.Height + 10"
would have to be this code
"RichTextBox1.Height = (SomeSize.Height * 2) + 10"
Option Strict On Public Class Form1 Dim AStringToUse As String = "Position: GK" & vbCrLf & "Overall Rating: 93" Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2))) RichTextBox1.ReadOnly = True RichTextBox1.BackColor = Color.White RichTextBox1.WordWrap = False Using g As Graphics = Graphics.FromHwnd(RichTextBox1.Handle) Dim SomeSize As Size = g.MeasureString("Waiting", RichTextBox1.Font).ToSize RichTextBox1.Width = SomeSize.Width + 8 RichTextBox1.Height = SomeSize.Height + 10 End Using RichTextBox1.Text = "Waiting" End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim SplitAStringToUse() As String = AStringToUse.Split(ChrW(13)) ' Split on Carriage Return from vbCrLf Using g As Graphics = Graphics.FromHwnd(RichTextBox1.Handle) Dim SomeSize As Size = g.MeasureString(SplitAStringToUse(1), RichTextBox1.Font).ToSize RichTextBox1.Width = SomeSize.Width + 8 RichTextBox1.Height = SomeSize.Height + 10 End Using RichTextBox1.Text = AStringToUse RichTextBox1.SelectionStart = RichTextBox1.Text.IndexOf(":"c) + 2 RichTextBox1.SelectionLength = RichTextBox1.Lines(0).Length - RichTextBox1.Text.IndexOf(":"c) - 1 RichTextBox1.SelectionBackColor = Color.Yellow RichTextBox1.SelectionColor = Color.Lime RichTextBox1.SelectionStart = RichTextBox1.Text.LastIndexOf(":"c) + 2 RichTextBox1.SelectionLength = RichTextBox1.Lines(0).Length + RichTextBox1.Lines(1).Length - RichTextBox1.Text.LastIndexOf(":"c) + 1 RichTextBox1.SelectionColor = Color.OrangeRed RichTextBox1.SelectionStart = 0 RichTextBox1.SelectionLength = 0 End Sub Private Sub RichTextBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseMove Button1.Focus() End Sub End Class
La vida loca
- Edited by Mr. Monkeyboy Friday, June 12, 2015 4:24 AM
Friday, June 12, 2015 4:13 AM -
You can always draw your own with the label's paint event.
Option Strict On Public Class Form4 Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load Label1.AutoSize = False Label1.Size = New Size(140, 40) Label1.Font = New Font("Arial", 12) End Sub Private Sub Label1_Paint(sender As Object, e As PaintEventArgs) Handles Label1.Paint e.Graphics.Clear(Color.White) Dim t1 As String = " Position:" Dim t2 As String = "GK" Dim s1, s2 As New SizeF s1 = e.Graphics.MeasureString(t1, Label1.Font) s2 = e.Graphics.MeasureString(t2, Label1.Font) e.Graphics.DrawString(t1, Label1.Font, Brushes.Black, 0, 0) e.Graphics.FillRectangle(Brushes.Yellow, s1.Width, 0, s2.Width, s2.Height) e.Graphics.DrawString(t2, Label1.Font, Brushes.Black, s1.Width, 0) t1 = "Overall Rating:" t2 = "93" s1 = e.Graphics.MeasureString(t1, Label1.Font) e.Graphics.DrawString(t1, Label1.Font, Brushes.Black, 0, s2.Height) e.Graphics.DrawString(t2, New Font("Arial", 10, FontStyle.Bold), Brushes.Red, s1.Width, s2.Height + 2) End Sub End Class
- Marked as answer by Youjun Tang Friday, June 19, 2015 2:58 AM
Friday, June 12, 2015 4:46 AM -
You can always draw your own with the label's paint event.
Option Strict On Public Class Form4 Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load Label1.AutoSize = False Label1.Size = New Size(140, 40) Label1.Font = New Font("Arial", 12) End Sub Private Sub Label1_Paint(sender As Object, e As PaintEventArgs) Handles Label1.Paint e.Graphics.Clear(Color.White) Dim t1 As String = " Position:" Dim t2 As String = "GK" Dim s1, s2 As New SizeF s1 = e.Graphics.MeasureString(t1, Label1.Font) s2 = e.Graphics.MeasureString(t2, Label1.Font) e.Graphics.DrawString(t1, Label1.Font, Brushes.Black, 0, 0) e.Graphics.FillRectangle(Brushes.Yellow, s1.Width, 0, s2.Width, s2.Height) e.Graphics.DrawString(t2, Label1.Font, Brushes.Black, s1.Width, 0) t1 = "Overall Rating:" t2 = "93" s1 = e.Graphics.MeasureString(t1, Label1.Font) e.Graphics.DrawString(t1, Label1.Font, Brushes.Black, 0, s2.Height) e.Graphics.DrawString(t2, New Font("Arial", 10, FontStyle.Bold), Brushes.Red, s1.Width, s2.Height + 2) End Sub End Class
You make it look too easy! Nice!
La vida loca
Friday, June 12, 2015 5:01 AM -
Not in a Label. If you want to use more than one font, you can use a RichTextBox instead.Friday, June 12, 2015 10:21 AM
-
You could try something like this also.
Option Strict On Public Class Form1 Dim RTB1StringToUse As String = "Position: GK" & vbCrLf & "Overall Rating: 93" Dim RTB2StringToUse As String = "Position: AJ" & vbCrLf & "Overall Rating: 77" Dim RTB3StringToUse As String = "Position: TG" & vbCrLf & "Overall Rating: 97" Dim RTB4StringToUse As String = "Position: LL" & vbCrLf & "Overall Rating: 23" Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2))) RichTextBox1.ReadOnly = True RichTextBox1.BackColor = Color.White RichTextBox1.WordWrap = False RichTextBox2.ReadOnly = True RichTextBox2.BackColor = Color.White RichTextBox2.WordWrap = False RichTextBox3.ReadOnly = True RichTextBox3.BackColor = Color.White RichTextBox3.WordWrap = False RichTextBox4.ReadOnly = True RichTextBox4.BackColor = Color.White RichTextBox4.WordWrap = False Using g As Graphics = Graphics.FromHwnd(RichTextBox1.Handle) Dim SomeSize As Size = g.MeasureString("Waiting", RichTextBox1.Font).ToSize RichTextBox1.Width = SomeSize.Width + 8 RichTextBox1.Height = SomeSize.Height + 10 RichTextBox2.Width = SomeSize.Width + 8 RichTextBox3.Height = SomeSize.Height + 10 RichTextBox3.Width = SomeSize.Width + 8 RichTextBox3.Height = SomeSize.Height + 10 RichTextBox4.Width = SomeSize.Width + 8 RichTextBox4.Height = SomeSize.Height + 10 End Using RichTextBox1.Text = "Waiting" RichTextBox2.Text = "Waiting" RichTextBox3.Text = "Waiting" RichTextBox4.Text = "Waiting" End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click SetRTBsText(RTB1StringToUse, RichTextBox1) SetRTBsText(RTB2StringToUse, RichTextBox2) SetRTBsText(RTB3StringToUse, RichTextBox3) SetRTBsText(RTB4StringToUse, RichTextBox4) End Sub Private Sub RichTextBoxs_MouseMove(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseMove, RichTextBox2.MouseMove, RichTextBox3.MouseMove, RichTextBox4.MouseMove Button1.Focus() End Sub Private Sub SetRTBsText(ByRef Info As String, ByRef RTB As RichTextBox) Dim SplitInfo() As String = Info.Split(ChrW(13)) ' Split on Carriage Return from vbCrLf Using g As Graphics = Graphics.FromHwnd(RTB.Handle) Dim SomeSize As Size = g.MeasureString(SplitInfo(1), RTB.Font).ToSize RTB.Width = SomeSize.Width + 8 RTB.Height = SomeSize.Height + 10 End Using RTB.Text = Info RTB.SelectionStart = RTB.Text.IndexOf(":"c) + 2 RTB.SelectionLength = RTB.Lines(0).Length - RTB.Text.IndexOf(":"c) - 1 RTB.SelectionBackColor = Color.Yellow RTB.SelectionColor = Color.Lime RTB.SelectionStart = RTB.Text.LastIndexOf(":"c) + 2 RTB.SelectionLength = RTB.Lines(0).Length + RTB.Lines(1).Length - RTB.Text.LastIndexOf(":"c) + 1 RTB.SelectionColor = Color.OrangeRed RTB.SelectionStart = 0 RTB.SelectionLength = 0 End Sub End Class
La vida loca
Friday, June 12, 2015 4:39 PM -
WOW :O
Yes the best solution is create more than 1 label....
Thanks all.
Friday, June 12, 2015 5:09 PM -
WOW :O
Yes the best solution is create more than 1 label....
Thanks all.
Then please propose the post, if one answered your question (Ashidacchi's I believe), as the answer to end this thread. :)La vida loca
Friday, June 12, 2015 5:26 PM