Scrolling in a RichTexBox
-
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
-
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
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
-
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
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 ?
-
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 ?
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.