Reputation: 631
Well, I'm trying to do this in the same label:
This is the coolest label of the world. :D
(And some color of course)
It is possible? ;)
(I will put variables no only text)
Upvotes: 1
Views: 1634
Reputation: 44971
You can actually accomplish this using a RichTextBox and some code.
If you add a RichTextBox to your form and apply the following properties:
Me.RichTextBox1.BorderStyle = System.Windows.Forms.BorderStyle.None
Me.RichTextBox1.ReadOnly = True
Me.RichTextBox1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None
you can then use it as a label:
Private Sub ConfigureRichTextLabel()
Me.RichTextBox1.Text = ""
Call AddTextWithFont("This is the coolest ", New Font("Arial", 12, FontStyle.Bold))
Call AddTextWithColor("label in the world ", Color.Red)
End Sub
Private Sub AddTextWithFont(sText As String, oFont As Font)
Dim index As Integer
index = Me.RichTextBox1.TextLength
Me.RichTextBox1.AppendText(sText)
Me.RichTextBox1.SelectionStart = index
Me.RichTextBox1.SelectionLength = Me.RichTextBox1.TextLength - index
Me.RichTextBox1.SelectionFont = oFont
End Sub
Private Sub AddTextWithColor(sText As String, oColor As Color)
Dim index As Integer
index = Me.RichTextBox1.TextLength
Me.RichTextBox1.AppendText(sText)
Me.RichTextBox1.SelectionStart = index
Me.RichTextBox1.SelectionLength = Me.RichTextBox1.TextLength - index
Me.RichTextBox1.SelectionColor = oColor
End Sub
You could take this one step further by subclassing the RichTextBox as RichTextLabel, apply the properties by default, and add the methods directly to the subclassed control.
Public Class RichTextLabel
Inherits RichTextBox
Public Sub New()
Me.ReadOnly = True
Me.BorderStyle = Windows.Forms.BorderStyle.None
Me.ScrollBars = RichTextBoxScrollBars.None
End Sub
Private Sub AddTextWithFont(sText As String, oFont As Font)
Dim index As Integer
index = Me.TextLength
Me.AppendText(sText)
Me.SelectionStart = index
Me.SelectionLength = Me.TextLength - index
Me.SelectionFont = oFont
End Sub
Private Sub AddTextWithColor(sText As String, oColor As Color)
Dim index As Integer
index = Me.TextLength
Me.AppendText(sText)
Me.SelectionStart = index
Me.SelectionLength = Me.TextLength - index
Me.SelectionColor = oColor
End Sub
End Class
Upvotes: 2
Reputation: 631
Well, I have a solution if isn't possible, but it breaks the rules of the title, I have to create other label. ;(
Upvotes: 0
Reputation: 34834
If you were doing WPF
, then it would be incredibly simple. It is possible in ASP.NET
, but not simple. It is impossible, as far as I know, in Windows Forms
to do this.
Upvotes: 0