1.如果仅仅对TextBox的Enter事件中,进行TextBox.SelectAll(),会发现没有效果。原因是,SelectAll()的确发生了,不过,在Enter之后,会触发其他事件,取消了这个效果。

2.正确的做法是,在Enter事件中,设置一个开关,然后再MouseUp事件中再做SelectAll()操作。

   例子:

   

private bool _state_textBox1_selectAll = false;//开关值

private void textBox1_Enter(object sender, EventArgs e)
{
    this._state_textBox1_selectAll = true;//当获取焦点后,设置开关
}

private void textBox1_MouseUp(object sender, MouseEventArgs e)
{
    if (this._state_textBox1_selectAll)//如果开关被设置,则进行SelectAll
    {
        this._state_textBox1_selectAll = false;//先清除开关,以免影响MouseUp
        this.textBox1.SelectAll();
    }
}

 

相关文章:

  • 2021-06-24
  • 2022-12-23
  • 2021-09-27
  • 2022-01-22
  • 2022-12-23
  • 2022-03-06
  • 2022-12-23
  • 2021-10-18
猜你喜欢
  • 2021-12-29
  • 2021-05-18
  • 2021-10-16
  • 2022-12-23
  • 2021-08-21
  • 2022-02-17
  • 2022-12-23
相关资源
相似解决方案