lapots
lapots

Reputation: 13395

move to the end of the text in textbox

I wanted to have textbox always focused. So I decided to add LostFocus handler

this.textBox1.LostFocus += new System.EventHandler(delegate(object sender, System.EventArgs e) 
                                                   {
                                                      this.textBox1.Focus();
                                                   });

But when I press buttons on the form and then begin to write into textbox again - it starts to add symbols before current text in textbox. For the example if I've got text in textbox abcd and then press button on the form and start to write again 1234 I have in the textbox text 1234abcd.

How to resolve this problem?

Upvotes: 1

Views: 248

Answers (3)

Gregor Primar
Gregor Primar

Reputation: 6805

Just use select method like this:

    void textBox1_LostFocus(object sender, EventArgs e)
    {
        textBox1.Focus();
        textBox1.Select(textBox1.Text.Length, 0);
    }

Upvotes: 1

Gyan Chandra Srivastava
Gyan Chandra Srivastava

Reputation: 1380

this.textBox1.LostFocus += new System.EventHandler(delegate(object sender, System.EventArgs e) 
                                                   {
                                                      this.textBox1.Focus();
                                                       this.textBox1.AppendText("") ;
                                                   });

Upvotes: 1

driis
driis

Reputation: 164291

this.textBox1.Focus();
this.textBox1.Select(textBox1.Text.Length, 0)

Upvotes: 3

Related Questions