【问题标题】:move to the end of the text in textbox移动到文本框中文本的末尾
【发布时间】:2012-09-15 17:50:19
【问题描述】:

我想让文本框始终保持焦点。所以我决定添加LostFocushandler

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

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

如何解决这个问题?

【问题讨论】:

    标签: c# winforms


    【解决方案1】:
    this.textBox1.Focus();
    this.textBox1.Select(textBox1.Text.Length, 0)
    

    【讨论】:

      【解决方案2】:
      this.textBox1.LostFocus += new System.EventHandler(delegate(object sender, System.EventArgs e) 
                                                         {
                                                            this.textBox1.Focus();
                                                             this.textBox1.AppendText("") ;
                                                         });
      

      【讨论】:

        【解决方案3】:

        只需像这样使用 select 方法:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-08-06
          • 2020-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-16
          • 2020-10-09
          相关资源
          最近更新 更多