日期:2014-05-19  浏览次数:20998 次

超级郁闷,TextBox得到焦点,选中全部内容
郁闷,如题,网上有很多解答,比如textbox.selectAll()  
但是我试了,为什么不行?比如我有2个textbox   ,点其中一个的时候,选中全部内容,我用Enter   或者gotFocus,都不行..但是只有一个textBox,并且预先写好内容时,在load后是选中的,还有如果你用down,跳到那个控件的时候,也是能选中的,为什么?我要实现第一种情况,要怎么做?贴出我的实验代码:

  private   void   textBox2_Enter(object   sender,   EventArgs   e)
                {
                        textBox2.SelectAll();
                }

                private   void   textBox1_KeyDown(object   sender,   KeyEventArgs   e)
                {
                        if   (e.KeyCode   ==   Keys.Down)
                        {
                                textBox2.Focus();
                        }
                }

------解决方案--------------------
textBox.Focus();
textBox.SelectAll();
------解决方案--------------------
你设置textBox1和textBox2的MouseDown事件,比如两个事件分别为textBox1_MouseDown和textBox2_MouseDown,那么在代码中分别实现这两个事件,如下:
private void textBox2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
textBox2.SelectAll();
}

private void textBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
textBox1.SelectAll();
}
这样应该就可以实现了