【问题标题】:For each and TypeOf doesn't work (don't charge any text on RichTextBox) on VB.NET对于每个和 TypeOf 在 VB.NET 上不起作用(不在 RichTextBox 上收取任何文本费用)
【发布时间】:2013-07-03 09:31:00
【问题描述】:

我想为我表单的所有 RichTextBox 创建一个全局样式:

与:

Public Class RichTextLabel

Public Shared Sub AddTextWithFont(ByVal sText As String, ByVal oFont As Font)

    For Each cControl In frmMain.Controls
        If (TypeOf cControl Is RichTextBox) Then
            Dim index As Integer
            index = cControl.TextLength
            cControl.AppendText(sText)
            cControl.SelectionStart = index
            cControl.SelectionLength = cControl.TextLength - index
            cControl.SelectionFont = oFont
        End If
    Next
End Sub

Public Shared Sub AddTextWithColor(ByVal sText As String, ByVal oColor As Color)

    For Each cControl In frmMain.Controls
        If (TypeOf cControl Is RichTextBox) Then
            Dim index As Integer
            index = cControl.TextLength
            cControl.AppendText(sText)
            cControl.SelectionStart = index
            cControl.SelectionLength = cControl.TextLength - index
            cControl.SelectionColor = oColor
        End If
    Next
End Sub

结束类

还有:

    RichTextLabel.AddTextWithFont("Estado del Spammer: ", New Font("Microsoft Sans Serif", 8, FontStyle.Bold))
    RichTextLabel.AddTextWithColor(state, Color.Red)

我不知道它出了什么问题... :(

【问题讨论】:

  • 不起作用并不能告诉我们任何事情。你的意思是什么不起作用 - 它什么都不做,它是否抛出异常等等。
  • 为什么你认为有问题?当您尝试运行代码时会发生什么,这与您的预期有何不同?您收到任何错误消息吗?
  • 它什么也没做,所有的文字都是空白的。

标签: vb.net foreach styles controls typeof


【解决方案1】:

我解决了:

Public Class RichTextLabel

Public Shared Sub AddTextWithFont(ByVal sText As String, ByVal oFont As Font, ByVal rtb As RichTextBox)

    Dim index As Integer
    index = rtb.TextLength
    rtb.AppendText(sText)
    rtb.SelectionStart = index
    rtb.SelectionLength = rtb.TextLength - index
    rtb.SelectionFont = oFont

End Sub

Public Shared Sub AddTextWithColor(ByVal sText As String, ByVal oColor As Color, ByVal rtb As RichTextBox)

    Dim index As Integer
    index = rtb.TextLength
    rtb.AppendText(sText)
    rtb.SelectionStart = index
    rtb.SelectionLength = rtb.TextLength - index
    rtb.SelectionColor = oColor
End Sub
End Class

还有:

RichTextLabel.AddTextWithFont("Estado del Spammer: ", New Font("Microsoft Sans Serif", 8, FontStyle.Bold), RichTextBox1)
RichTextLabel.AddTextWithColor(state, Color.Red, RichTextBox1)

【讨论】:

    【解决方案2】:

    这似乎有效:

        For Each cControl As Control In frmMain.Controls
            If (TypeOf cControl Is RichTextBox) Then
                Dim rtb As RichTextBox = CType(cControl, RichTextBox)
    
                Dim index As Integer
                index = rtb.TextLength
                rtb.AppendText(sText)
                rtb.SelectionStart = index
                rtb.SelectionLength = rtb.TextLength - index
                rtb.SelectionFont = oFont
            End If
        Next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多