Caret in textbox

P

P

Is there a way to get the caret placed at the end of the text in a textbox
when it recieves focus. What I want to do is trap a certain key and insert a
charachter to a textbox and focus it. But the caret appears at the beginning
of the string. Not quite what I was looking for.

Advice?

<P>
 
P

Peter B

this.TextBox1.SelectionStart = this.TextBox1.Text.Length;

Should do it for you.

If you want to insert a character at the position of the caret this should
work:

int pos = TextBox1.SelectionStart;
TextBox1.Text = tBox.Text.Insert( pos, myChar );
TextBox1.SelectionStart = pos + 1;

/ Peter
 
G

Guest

You can place the carret wherever you want. To place it at the end use: this.textBox1.SelectionStart=this.textBox1.Text.Length; (you could write the following code in OnFocus event)
 
P

Pete Vickers [eMVP]

Hi,
try...
txtMyText.Focus
txtMyTest.SelectionStart = txtMyTest.TextLength

HTH

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top