Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Scrolling in a RichTexBox

Scrolling in a RichTexBox

Scheduled Pinned Locked Moved C#
helpcsharpdatabasesysadminlounge
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jpeg
    wrote on last edited by
    #1

    I'm trying to develop my first C# app. I chose to develop a chat software because I wanted to use the sockets' classes. I've develop a chat server and a chat client. In the last one, I have to display the incoming messages in a RichTextBox control. The problem is that it doesn't automatically scroll to the end of this control when I append a text. The piece of code is : int index=dialogTextBox.TextLength; dialogTextBox.AppendText(message+"\r\n"); dialogTextBox.Select(index, message.Length); dialogTextBox.SelectionColor=myColor; (NB : its append a new line using the 'myColor' color) I tried to use the ScrollToCaret but it doesn't work. Please help me ! jpeg

    S 1 Reply Last reply
    0
    • J jpeg

      I'm trying to develop my first C# app. I chose to develop a chat software because I wanted to use the sockets' classes. I've develop a chat server and a chat client. In the last one, I have to display the incoming messages in a RichTextBox control. The problem is that it doesn't automatically scroll to the end of this control when I append a text. The piece of code is : int index=dialogTextBox.TextLength; dialogTextBox.AppendText(message+"\r\n"); dialogTextBox.Select(index, message.Length); dialogTextBox.SelectionColor=myColor; (NB : its append a new line using the 'myColor' color) I tried to use the ScrollToCaret but it doesn't work. Please help me ! jpeg

      S Offline
      S Offline
      Steven Behnke
      wrote on last edited by
      #2

      All you will need to do is... int index=dialogTextBox.TextLength; dialogTextBox.AppendText(message+"\r\n"); // Inserted here dialogTextBox.Focus(); dialogTextBox.Select(index, message.Length); dialogTextBox.SelectionColor=myColor; // Focus your entry box back again here. Or you can write an On Text Changed handler, like I had to that will focus the box, scroll to the caret, then focus back to the entry. You will have to do this if you are adding text to the box from a thread that you created to read from the socket like I did. Doh! -Steven

      J 1 Reply Last reply
      0
      • S Steven Behnke

        All you will need to do is... int index=dialogTextBox.TextLength; dialogTextBox.AppendText(message+"\r\n"); // Inserted here dialogTextBox.Focus(); dialogTextBox.Select(index, message.Length); dialogTextBox.SelectionColor=myColor; // Focus your entry box back again here. Or you can write an On Text Changed handler, like I had to that will focus the box, scroll to the caret, then focus back to the entry. You will have to do this if you are adding text to the box from a thread that you created to read from the socket like I did. Doh! -Steven

        J Offline
        J Offline
        jpeg
        wrote on last edited by
        #3

        Thanks for your help. As you said in your answer, my updating functions were called from a thread (a thread which is waiting for messages from the server). So I added a handling method for my dialogTextBox's TextChange Event to use these updating functions instead of calling them in the thread and it works ! Therefore this problem is quite strange, don't you think ? Why can you add a text to a textbox and can't scroll to the end ?

        S 1 Reply Last reply
        0
        • J jpeg

          Thanks for your help. As you said in your answer, my updating functions were called from a thread (a thread which is waiting for messages from the server). So I added a handling method for my dialogTextBox's TextChange Event to use these updating functions instead of calling them in the thread and it works ! Therefore this problem is quite strange, don't you think ? Why can you add a text to a textbox and can't scroll to the end ?

          S Offline
          S Offline
          Steven Behnke
          wrote on last edited by
          #4

          It is a little strange. I had even tried passing the parent object as a part of a setup function, and then calling a method on that object to focus the object, but still no go. For some reason it appears that .Focus() doesn't function inside of a subthread.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups