2013-12-20 92 views
0

我正在爲我的遊戲啓動器進行聊天,當您啓動一個按鈕時,會打開一個包含richtextbox的聊天論壇,它將刷新/文本在服務器上的txt文件已經改變從文本框1改變,然後它會重新打印文本,但多數民衆贊成沒有問題,我有一個管理面板,但我想突出顯示行「!管理員!因此它顯示用戶是管理員,突出顯示他正在說的內容。我已經試過這在richtextbox中突出顯示包含「admin」的所有行

Dim index As Integer = Me.RichTextBox1.Find("!ADMINISTRATOR!") 
    If index <> -1 Then 
     Dim lineindex As Integer = Me.RichTextBox1.GetLineFromCharIndex(index) 
     Dim first As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex) 
     Dim last As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex + 1) 
     If last = -1 Then last = Me.RichTextBox1.TextLength 
     Me.RichTextBox1.Select(first, last - first) 
     Me.RichTextBox1.SelectionBackColor = Color.Yellow 
    End If 

但這樣的作品,在某種程度上,它並不總是高亮顯示,雖然它有時會突出,這也可以突出一個纔剛剛把文字,但我不主要的問題是它只突出顯示第一行的文字,所以如果管理員發佈消息,它會突出顯示< 3,但是然後用戶會聊天,然後管理員發佈另一條消息,它只會突出顯示包含該文本的第一行。如果你需要更多的信息哦,併爲了測試的目的,我使用本地文件在我的電腦上,我測試了它的工作原理,但只是爲了節省時間即時通訊使用txt文件,這是我用於計時器的代碼

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick 
    If (System.IO.File.ReadAllText(Get_Directory_Current() & "\FilesDB\Chat\Chat.txt") = RichTextBox1.Text) Then 

    Else 
     Try 
      RichTextBox1.Text = System.IO.File.ReadAllText(Get_Directory_Current() & "\FilesDB\Chat\Chat.txt") 
      RichTextBox1.SelectionStart = RichTextBox1.Text.Length 
      RichTextBox1.ScrollToCaret() 
      ColorChat() ' this is the color chat that kinda dont work 
     Catch ex As Exception 
     End Try 
    End If 
End Sub 

如果你可以幫助謝謝:)但現在我只是嘗試新的東西。我想它會有點像語法突出顯示,如果你讓它突出顯示一行而不是單詞。

哦,如果你想知道我這是怎麼的文本添加到聊天/格式化

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 
    RichTextBox1.AppendText("<[" & Get_Time() & " " & strDate & "] " & "!ADMINISTRATOR!" & " | " & Environment.UserName & "> " & TextBox3.Text & vbNewLine) 
End Sub 

回答

1

使用String.Replace()目標字符串出現在哪裏注入的RTF格式。

Dim stringToFind = "!ADMINISTRATOR!" 
Dim txt = Me.RichTextBox1.Text 
Dim sb = New System.Text.StringBuilder() 
sb.Append("{\rtf1\ansi\deff0 {\colortbl;\red0\green0\blue0;\red255\green0\blue0;}") 
sb.Append(txt.Replace(stringToFind, String.Format("\cf2{0}\cf1", stringToFind))) 
sb.Append("}") 
Me.RichTextBox1.Rtf = sb.ToString() 
相關問題