对于每个和TypeOf在VB.NET上不起作用(不对RichTextBox上的任何文本收费)

时间:2013-07-05 10:02:47

标签: vb.net foreach styles controls typeof

我想为我的表单的所有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)

我不知道它的错误...... :(

2 个答案:

答案 0 :(得分:0)

这似乎有效:

    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

答案 1 :(得分:0)

我解决了:

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)