1

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

世界 最酷的标签。_ :D

(当然还有一些颜色)

有可能的?;)

(我不会只放文本变量)

4

3 回答 3

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
于 2013-07-04T19:36:13.180 回答
0

如果您正在这样做WPF,那将非常简单。在 中是可能的ASP.NET,但并不简单。据我所知,这是不可能的Windows Forms

于 2013-07-04T19:27:44.523 回答
0

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

于 2013-07-04T19:31:28.717 回答