1

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)

Seazoux
  • 621
  • 1
  • 5
  • 28

3 Answers3

2

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
competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • I was working on something like this but you have been quicker. I got stuck on the part of how to show the true ForeColor despite being disabled (otherwise you see the cursor indication to write a new line, which does not appear in a label). I recall that some time ago I found a solution for that but don't quite remember now. – varocarbas Jul 04 '13 at 19:43
0

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.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
0

Well, I have a solution if isn't possible, but it breaks the rules of the title, I have to create other label. ;(

Seazoux
  • 621
  • 1
  • 5
  • 28