How do I set the cursor to the right of a text box

  • Thread starter Thread starter Rockfrdcpl
  • Start date Start date
R

Rockfrdcpl

Hi
Does anyone know how to place the cursor in the far right of the textbox when
the text box is not empty upon getting the focus?
 
in textbox's gotfocus and click event, write code

textbox1.select(0, textbox1.textlength)
sendkeys.send(keys.right)

I think, that would work.

Rajesh Patel
 
Hi Rajesh,
in textbox's gotfocus and click event, write code

textbox1.select(0, textbox1.textlength)
sendkeys.send(keys.right)
SendKeys.Send has to be used as follows
SendKey.Send({RIGHT});

The same result can be achieved with only one line of code
textBox1.Select(textBox1.TextLength, 0);
However, they work only if the focus is set using the keyboard. If mouse is
used text box sets the cursor where the click occurred.

Anyway, I think the question is how to place the carret next to the right
border of the control not at the end of the text.
I can't think of any easy way to do that. The only way I can think of is to
use P/Invoke and send EM_CHARFROMPOS message.

If you use RichEditBox it has GetCharIndexFromPosition method, which can be
used to get the index of the last visible character. Once we have the index
we can use RichTextBox.Select method.

int index = richTextBox1.GetCharIndexFromPosition(new
Point(richTextBox1.ClientRectangle.Right, 1));
richTextBox1.Select(index + 1, 0);

Or something like this.

HTH
B\rgds
 
Back
Top