移到文本框中的文本末尾

时间:2012-09-15 17:50:19

标签: c# winforms

我希望文本框始终关注。所以我决定添加LostFocus处理程序

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

但是当我按下表单上的按钮然后再次开始写入文本框时 - 它开始在文本框中的当前文本之前添加符号。对于该示例,如果我在文本框abcd中有文本,然后按下表单上的按钮并再次开始写1234我在文本框文本1234abcd中。

如何解决此问题?

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

只需使用这样的select方法:

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