在VB.NET上将文本样式更改为同一标签的两倍或三倍

时间:2013-07-04 19:15:38

标签: vb.net colors styles label

好吧,我正在尝试在同一个标​​签上执行此操作:

标签 < / strong>即可。 :d

(当然还有一些颜色)

有可能吗? ;)

(我将变量不仅仅是文本)

3 个答案:

答案 0 :(得分:2)

您可以使用RichTextBox和一些代码实际完成此操作。

如果您向表单添加RichTextBox并应用以下属性:

    Me.RichTextBox1.BorderStyle = System.Windows.Forms.BorderStyle.None
    Me.RichTextBox1.ReadOnly = True
    Me.RichTextBox1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None

然后您可以将其用作标签:

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

通过将RichTextBox子类化为RichTextLabel,默认应用属性,并将方法直接添加到子类控件,可以更进一步。

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

答案 1 :(得分:0)

如果你正在做WPF,那将非常简单。这可能在ASP.NET,但不简单。据我所知,Windows Forms无法做到这一点。

答案 2 :(得分:0)

嗯,我有一个解决方案,如果不可能,但它打破了标题的规则,我必须创建其他标签。 ;(