going to textbox

  • Thread starter Thread starter linuxpld
  • Start date Start date
L

linuxpld

Hi

How can I force my cursor to go to the end of the text after pushing a
button.

Using System.Windows.Forms I have something like this:


TextBox textBox;
Button button;

.....
button.Click += new System.EventHandler(myButton_Click);

protected void myButton_Click(object sender, System.EventArgs e) {
-- code to activate textBox and go to the end of the text - something
like mouse click on the end of the text.
}

thx for any answers.
linux coder
 
Hi Linux coder,
How can I force my cursor to go to the end of the text after pushing a
button.

Your key to success if the TextBox's SelectionStart property, with which you
can control the caret ("cursor") position. Combined with the SelectionLength
property, you can select any piece of text inside the textbox, or none at
all.

So, try this code for instance:

private void button1_Click(object sender, EventArgs e)
{
textBox1.SelectionLength = 0;
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.Focus();
}

Hope this answers your question.

(Note though that you are changing the default Windows functionality by
doing this, which in turn might raise some complaints from the users of your
application. Then again, there are many valid reasons to do exactly what you
want to do. Use proper judgement.)

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
You can achieve that using the following code snippet:

textBox.Focus();
SendKeys.Send("{END}");
 

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

Back
Top