Question

I am Making a chat for my Game launcher, when you launch it up theres a button that Opens a Chat Forum with a richtextbox on it that it refreshes/timer checks when the text has changed in the txt file on the server has changed from textbox1 then it will reprint the text but thats not the problem, i have an admin panel for it, but i want to highlight lines where there is "!ADMINISTRATOR!" in it so it shows the user is an admin and it highlighted to show what he is saying. i've tried this

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

But that works, in a way, it does not always highlight it, while it will highlight sometimes, it also may highlight the text a just just put, but that i don't mind, the main problem is that it only highlights the First line with the text in it, so if the admin posts a message on it it will highlight <3 but then a user will chat and then admin posts another message, it only highlights the first line that contains that text. if you need any more info oh and for testing purposes im using a local file on my pc, ive tested the ftp it works, but just to save time im using a txt file this is the code i use for the timer

    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

if you can help thanks :) but for now i'm just trying new things. i guess it would kinda work a bit like syntax highlighting if you make it highlight a line instead of word.

Oh and if you wanted to know this is how i add the text to the chat / formatting

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
Was it helpful?

Solution

Use String.Replace() to inject the Rtf formatting where your target string appears.

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top