Results 1 to 2 of 2

Thread: RTB Color change in "chatlog"

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2012
    Posts
    51

    RTB Color change in "chatlog"

    Relearning VB.net, decided to make a silly chatbot. I want the user input and bot output different colors in the RTB. I'll post what I've been trying to make work but I'm struggling because I don't understand the .Selection very well.

    Code:
            OutputRTB.Select(OutputRTB.TextLength, 1)
            OutputRTB.SelectionColor = Color.Green
            OutputRTB.Text += vbNewLine & UsernameWhole & ": " & InputTxtBox.Text & vbNewLine
    
    
            OutputRTB.SelectionColor = Color.Blue
            OutputRTB.Select(OutputRTB.TextLength, 1)
            Dim InputStringDing As String = InputTxtBox.Text.ToLower
            Select Case True
                Case InputStringDing.Contains("hello")
                    OutputRTB.Text += "Bot: " & "Hello there"
                Case InputStringDing.Contains("hi")
                    OutputRTB.Text += "Bot: " & "Hi there"
                Case Else
                    OutputRTB.Text += "No recognizable response. Sending to database."
            End Select
    (Pastebin link to same code as above)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: RTB Color change in "chatlog"

    Don't add to the Text property. Call AppendText instead. I just tried this and it appears to do exactly what you want:
    vb.net Code:
    1. Private Sub AppendUserText(text As String)
    2.     AppendText(text, Color.Red)
    3. End Sub
    4.  
    5. Private Sub AppendBotText(text As String)
    6.     AppendText(text, Color.Blue)
    7. End Sub
    8.  
    9. Private Sub AppendText(text As String, colour As Color)
    10.     With RichTextBox1
    11.         .SelectionStart = .TextLength
    12.         .SelectionColor = colour
    13.         .AppendText(text & Environment.NewLine)
    14.     End With
    15. End Sub
    If you set the selection formatting at the end of the text and then append, the appended text will take on that formatting, so there's no need to select existing text and format it unless you want to change it later.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width