请问如何使文本框获得焦点时触发一个事件?private void txtYear_TextChanged(object sender, EventArgs e)
{}是当文本框内容发生改变时触发事件。文本框获得焦点时触发一个事件该怎么写呢?万分感谢!

解决方案 »

  1.   

      private void textBox1_Enter(object sender, EventArgs e)
            {
                this.Text = "ok";
            }
      

  2.   

    我想让 当 Tab使焦点落到某一个文本框上时,文本框的内容被全部选中,这样要修改起来会方便一些。我定义事件
    private void textBox1_Enter(object sender, EventArgs e) 

       this.textBox1.Select();
       this.textBox1.SelectAll();
    }好像没起到效果。请问这个问题该如何解决?多谢!
      

  3.   


    试试……
    this.textBox1.SelectedStart = 0;
    this.textBox.SelectedLength =this.textBox1.text.Length;
      

  4.   

    private void textBox1_Enter(object sender, EventArgs e) 

      this.textBox1.SelectAll(); 

    就这样可以了。我试过。
      

  5.   

     
    自己写一个文本框类,处理window消息EN_SETFOCUS,EN_KILLFOCUS试一试。
      

  6.   

    文本有一個GotFocused事件
    private void textBox_gotFocused(object sender,eventArgs e)
    {
       if(ChangeHandel!=null)
         ChangeHandel(sender,e);      //獲得焦點時,触發這個事件
    }private event eventHandle ChangeHandel;
      

  7.   

         private bool m_SelectALL = false;
            void textBox1_Leave(object sender, EventArgs e)
            {
                m_SelectALL = false;
            }
            private void textBox1_Enter(object sender, EventArgs e)
            {
                this.textBox1.SelectAll();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                this.textBox1.Enter += new EventHandler(textBox1_Enter);
                this.textBox1.Click += new EventHandler(textBox1_Click);
                this.textBox1.Leave += new EventHandler(textBox1_Leave);
            }
            void textBox1_Click(object sender, EventArgs e)
            {
                if (!m_SelectALL)
                {
                    this.textBox1.SelectAll();
                    m_SelectALL = true;
                }            
            }
      

  8.   

    首先,请确认textBox1的TabStop属性是否为True;
    然后,请确认textBox1的Enter事件是正否确的和函数
    private void textBox1_Enter(object sender, EventArgs e)关联
    (可以搜索当前工程中是否有类似this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter);的代码)。
    如果以上两点都正确,并且函数
    private void textBox1_Enter(object sender, EventArgs e) 

      this.textBox1.SelectAll(); 

    是上面这样的实现方法,那么lz的需求是可以实现的。
      

  9.   


    实现了。多谢!!!我想问最后一个问题, “(可以搜索当前工程中是否有类似this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter);的代码)。”这句话只能自己敲吗,有没有捷径可以走啊?万分感谢!
      

  10.   

    this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter);这句话只能自己写吗?不能自动生成?
      

  11.   

    不是很了解lz的意思是什么,是在开发环境中搜索的时候?
    吼吼,我想Ctrl+c和Ctrl+v也可以实现吧!
      

  12.   

    噢,这个啊,如果你使用的是VS2005或者VS2008这些代码是可以自动生成的。