Click to See Complete Forum and Search --> : Cursor Positioning in a Text Control.


Rob McVicar
May 30th, 2006, 10:17 AM
I have a textbox control in a Form. I'm using it as a history page of what is typed in an IM type app. How can I keep the focus (view) at the end of the text control instead of it automatically always going to the beginning?

I want to do this to be able to view the last items entered first.

Any help would be greatly appreciated.

Thanks.

Shuja Ali
May 30th, 2006, 10:28 AM
textBox1.SelectionStart = textBox1.Text.Length; I guess you are concatenating the strings everytime a new IM is recieved or sent. The append process to the textbox can be made more proper in order to reduce time. I would do the append process like this //set the cursor at the end of the text
textBox1.SelectionStart = textBox1.Text.Length;
//add the text here
textBox1.SelectedText = "text that needs to appended to the textbox";

Rob McVicar
May 31st, 2006, 08:07 AM
Thanks for the help. I'll give that a try. Sorry I did not get back sooner, e-mail probs at work!

Rob McVicar
May 31st, 2006, 10:48 AM
Does this method also work for a richtextbox?

Thanks.

Shuja Ali
May 31st, 2006, 10:50 AM
Does this method also work for a richtextbox?

Thanks.
It should, you should try it out yourself.

Rob McVicar
May 31st, 2006, 11:00 AM
Thanks it does not seem to be working, but maybe I messed something up.

Thanks again!

Shuja Ali
May 31st, 2006, 11:08 AM
Can you post the code? It works fine with me.

Rob McVicar
May 31st, 2006, 11:11 AM
Here it is...

displayTextBox.SelectionColor = System.Drawing.Color.MediumAquamarine;
displayTextBox.SelectionStart = displayTextBox.Text.Length;
displayTextBox.SelectedText = "\r\nSent: " + packet;

Thanks!

Shuja Ali
May 31st, 2006, 11:21 AM
Works perfectly on my system. :thumb: Does it show any error?

And you should change of the selection only after setting the selectionstart.

Rob McVicar
May 31st, 2006, 11:30 AM
No error here. Just does not work right. It is not showing the bottom of the richtextbox like it was with just the straight textbox. I changed to the richtextbox to be able to easily change the font color, which I will put after the selection start.

Rob McVicar
May 31st, 2006, 01:02 PM
FYI. The code works file with the regular text box, however, it does not work with a richtextbox.

Shuja Ali
June 1st, 2006, 03:28 AM
It exactly does what it is supposed to do. Append text to the richtextbox and position the cursor at the end of the richTextBox.