2013-07-05 17 views
0

好吧,我想建立一個全球性的風格對我的表格的所有RichTextBox的:對於每TypeOf運算不工作(不收取任何的RichTextBox文本)在VB.NET

有了:

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) 

我不知道它錯了... :(

+0

不工作並沒有告訴我們任何東西。你的意思是什麼都行不通 - 它沒有做任何事情,是否會拋出異常,等等。 – Tim

+0

你爲什麼認爲有什麼不對?當您嘗試運行代碼時會發生什麼,以及它與您期望的有什麼不同?你有任何錯誤信息? – Guffa

+0

它沒有做任何事情,所有的文字都是空白的。 – Seazoux

回答

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 
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)