2015-11-07 70 views
-2

我想爲richtextbox中的所有字符串着色。例如,如果一個特定的字符串的前綴是如何根據字符串的前綴爲RichTextBox的文本着色?

  1. Received:那麼它應該是藍色
  2. Send:那麼它應該是紅色
  3. Info:那麼它應該是綠色

*The way I output the text in the RichTextBox is by ascending order, it means that all the newest messages will be outputted at the top of the RichTextBox, the old ones will go down.

屏幕截圖:

enter image description here

代碼:

Public Class Form1 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

     Dim msg_info As String = Nothing 

     If RB_Info.Checked = True Then 
      msg_info = "Info: " 
     End If 
     If RB_Send.Checked = True Then 
      msg_info = "Send: " 
     End If 
     If RB_Received.Checked = True Then 
      msg_info = "Received: " 
     End If 

     RichTextBox1.Text = RichTextBox1.Text.Insert(0, msg_info & TextBox1.Text & ControlChars.NewLine) 
    End Sub 
End Class 

編輯:我試圖將這種子,但它改變了所有項目的ListBox中的顏色​​

Sub HighlightPhrase(box As RichTextBox, phrase As String, color As Color) 
     Dim pos As Integer = box.SelectionStart 
     Dim s As String = box.Text 
     Dim ix As Integer = 0 
     While True 
      Dim jx As Integer = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase) 
      If jx < 0 Then 
       Exit While 
      End If 
      box.SelectionStart = jx 
      box.SelectionLength = phrase.Length 
      box.SelectionColor = color 
      ix = jx + 1 
     End While 
     box.SelectionStart = pos 
     box.SelectionLength = 0 
    End Sub 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     Dim msg_info As String = Nothing 

     If RB_Info.Checked = True Then 
      msg_info = "Info: " 
      HighlightPhrase(RichTextBox1, "Info", Color.Blue) 
     End If 
     If RB_Send.Checked = True Then 
      msg_info = "Send: " 
      HighlightPhrase(RichTextBox1, "Send", Color.Green) 
     End If 
     If RB_Received.Checked = True Then 
      msg_info = "Received: " 
      HighlightPhrase(RichTextBox1, "Received", Color.Red) 
     End If 
     RichTextBox1.Text = RichTextBox1.Text.Insert(0, msg_info & TextBox1.Text & ControlChars.NewLine) 
    End Sub 

回答

1

我我已經編輯了我的解決方案,所以新行出現在RichTextBox的頂部。

我認爲我的解決方案很難看,因爲基本上它會在添加新行後重新繪製每行的顏色。想象一下,如果RichTextBox有成千上萬行。我認爲你可以通過限制RTB中有多少行,例如100行來減少這種情況。使用一個ListView

你可能想看看ListView控件

Public Class Form1 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

     Dim msg_info As String = Nothing 

     If RB_Info.Checked = True Then 
      msg_info = "Info: " 
     End If 
     If RB_Send.Checked = True Then 
      msg_info = "Send: " 
     End If 
     If RB_Received.Checked = True Then 
      msg_info = "Received: " 
     End If 

     RichTextBox1.Text = RichTextBox1.Text.Insert(0, msg_info & TextBox1.Text & ControlChars.NewLine) 
     ChangeColor() 

    End Sub 

    Private Sub ChangeColor() 

     Dim lines = RichTextBox1.Text.Split(vbLf) 
     Dim startPos As Integer, endPos As Integer = -1 
     Dim myColor As Color 

     For i = 0 To lines.Length - 2 ' minus 2 because the last one is empty string 
      startPos = endPos + 1 
      endPos = startPos + lines(i).Length 
      RichTextBox1.Select(startPos, endPos) 

      If lines(i).StartsWith("Info: ") Then myColor = Color.Red 
      If lines(i).StartsWith("Send: ") Then myColor = Color.Blue 
      If lines(i).StartsWith("Received: ") Then myColor = Color.Green 

      RichTextBox1.SelectionColor = myColor 
     Next 

    End Sub 

End Class 

替代解決方案。如果你不想在列表中寫任何東西,我建議你用ListView替換RichTextBox。在添加ListView後,將View屬性更改爲List

enter image description here

Public Class Form1 

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 

     Dim msg_info As String = Nothing 
     Dim myColor As Color 

     If RB_Info.Checked = True Then 
      msg_info = "Info: " 
      myColor = Color.Green 
     End If 
     If RB_Send.Checked = True Then 
      msg_info = "Send: " 
      myColor = Color.Red 
     End If 
     If RB_Received.Checked = True Then 
      msg_info = "Received: " 
      myColor = Color.Blue 
     End If 

     Dim li = New ListViewItem() 
     li.ForeColor = myColor 
     li.Text = msg_info & TextBox1.Text 
     ListView1.Items.Insert(0, li) 

    End Sub 

End Class 
+0

是的,這是偉大的。它唯一遺漏的就是RichTextBox應該在RichTextBox的上部輸出新文本。像這樣'RichTextBox1.Text = RichTextBox1.Text.Insert(0,msg_info&TextBox1.Text&ControlChars.NewLine)' –

+0

非常感謝!這真的有用! –