一个窗体上有两个textbox,一个是name,一个是ip。在新建时弹出窗体的时候默认焦点在name上,在编辑时name是只读的,焦点应该在ip上,如何才能实现呢, this.txtIP.Focus();
 this.txtIP.SelectAll();
这样做焦点还是在name上,因为name的tab顺序在ip的前面。

解决方案 »

  1.   

    this.txtIP.Focus();
     this.txtIP.SelectAll();
    不就好了么
      

  2.   

    txtIP是ID吧,建议用document . getElementById ("txtIP").focus();的规范写法试一下
      

  3.   

    试验了下,txtIP.tabindex设置下,大于txtName.tabindex。
      

  4.   

    this.txtIP.Focus();
     this.txtIP.SelectAll();这两句话之后是不是给txtName付过值,还有以上两句话,最好写在form_load事件里。
      

  5.   

    小于的话,txtname就不能获取焦点了,在新建的时候还得txtname获取焦点呢 
      

  6.   

    现在就是在form_load 事件里面,我调试过代码还执行了。
      

  7.   

    说实话 不知道你为什么没焦点  我用this.textBox1.Focus();   一试就成功
      

  8.   

    this.txtIP.Focus();是否你的textbox控件有其它事件?
      

  9.   

    我新建了一个按钮,在点击事件里面执行了 this.txtIP.Focus();
    this.txtIP.SelectAll();是可以获取焦点的,在 from_load 里执行了怎么不行,难道是 tab index的问题造成的吗
      

  10.   

    新建两个textbox---焦点自动会进入tabindex小得textbox--如果还有好多其他东西 然后在设置下焦点试试--
      

  11.   

    感觉是from_load中走别的方法,或有类似于form_active事件重设form了要么彻查form时都走什么了,要么在load时候判断下,编辑时候直接把name.TabStop 设置下 ?
      

  12.   

    this.txtName.Enabled = false;
      

  13.   

    form只有一个事件 就load 代码也很简单,  private void FormDeviceDetail_Load(object sender, EventArgs e)
            {
                if (isEdit)
                {
                    this.Text = Resource1.EditPhoneNumber;
                    this.txtname.ReadOnly = true;
                    this.txtPhoneNumber.Focus();
                    this.txtPhoneNumber.SelectAll();
                }
              
            }
      

  14.   

    最简单的办法:
        调整它们的tab顺序!
      

  15.   

    调整后解决了,   private void FormDeviceDetail_Load(object sender, EventArgs e)
            {
                if (isEdit)
                {
                    this.Text = Resource1.EditPhoneNumber;
                    this.txtname.ReadOnly = true;
                    this.txtPhoneNumber.TabIndex=0;
                    this.txtname.TabIndex = 1;
                }
          
            }