窗体form2中有三个文本框num1,num2,num3,焦点默认在num1上,运行程序后,想让焦点在num2上,代码如下,但不起作用。
namespace wlj1024
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            this.num2.Focus();
        }
               private void label1_Click(object sender, EventArgs e)
        {
            
        }
        
        private void button1_Click(object sender, EventArgs e)
        {            this.num3.Text = Convert.ToString(Convert.ToInt64(this.num1.Text) + Convert.ToInt64(this.num2.Text)); 
           
        }
                private void button2_Click_1(object sender, EventArgs e)
        {
            this.num1.Text = "";
            this.num2.Text = "";
            this.num3.Text = "";
        }        private void num2_TextChanged(object sender, EventArgs e)
        {        }        
        
    }
}

解决方案 »

  1.   

    Form2_Load 改为 Shown 事件。
      

  2.   

    this.num1.Focus= "";
    this.num2.Focus= "";
    this.num3.Focus= "";
    这个就是通过焦点的。
    关键看你怎么用
      

  3.   

    我个人认为“this.num2.Focus();”从理论上说得过去,但现实中,却往往又没效果,楼下的请讲讲。但通过“View”->"Tab order",以手动的方式,可以把焦点默认到指定控件。(即为0的那个)
      

  4.   

    设置 num2 tabIndex=0是可以了,private void Form2_Load(object sender, EventArgs e)
            {
                this.num2.TabIndex=0;
                this.num2.Focus();
            }
    为什么不行?
      

  5.   

    在界面设置时将num2的TabIndex属性设置为0就可以达到效果,或者是在窗体的Load时间时设置,如this.num2.TabIndex=0,就可以啦。
      

  6.   

    帮助里有这样一句话:
    注意: 
    Focus 是低级别方法,主要供自定义控件创作者使用。而应用程序程序员则应该对子控件使用 Select 方法或 ActiveControl 属性,或对窗体使用 Activate 方法。
     
      

  7.   

    在窗体的load事件中设置this.num2.TabIndex=0 不行呀
      

  8.   

    load 中用这个可以:num2.select();
    num2.focus()
      

  9.   

    num2.select();
    这个就行了吧!!
      

  10.   

    private void Form2_Load(object sender, EventArgs e)
            {
                this.num2.Select();
                this.num2.Focus();
                
                
            }
    这样写的,也不行
      

  11.   

    lz,你就不能把你的Form_Load事件改为Form_Shown事件么?
      

  12.   

                this.textBox2.Focus();
                this.textBox2.Select();
      

  13.   

    不知道你什么环境,还是说别的地方还有代码你没有贴出来。我测试用 Load 事件中 Select() 还是在 Shown 事件中 Focus() 都好用。
      

  14.   

    我用的是vs2005的,代码如下:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace wlj1024
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
            private void Form2_Shown(object sender, EventArgs e)
            {
                num2.Focus();
                
                
            }               private void label1_Click(object sender, EventArgs e)
            {
                
            }
            
            private void button1_Click(object sender, EventArgs e)
            {            this.num3.Text = Convert.ToString(Convert.ToInt64(this.num1.Text) + Convert.ToInt64(this.num2.Text)); 
               
            }
                    private void button2_Click_1(object sender, EventArgs e)
            {
                this.num1.Text = "";
                this.num2.Text = "";
                this.num3.Text = "";
            }        private void num2_TextChanged(object sender, EventArgs e)
            {        }        private void Form2_Load(object sender, EventArgs e)
            {        }                      
            
        }
    }
      

  15.   


    this.num2.Select();
    this.num2.Focus();
      

  16.   

     textBox2.TabIndex = 0;
      将第二个textBox2的TabIndex 设置为0
      先聚焦的就是textBox2
      

  17.   

    每个TextBox都有TabIndex属性,将num1的TabIndex设置成其它2,将num2的TabIdex设置成1,num3设置成3。在load时间中加
    this.num2.Select();
    this.num2.Focus();
    好像是不起作用的,只有设置TabIndex属性才行。
      

  18.   

    只有手动设置TabIndex=0才行,但这样才麻烦了,最好是写在代码里,灵活。望高手指教写在哪个事件里面,才能使焦点在第二个文本框里
      

  19.   

    this.num2.parent.select();
    this.num2.select();
    this.num2.focus();试试
      

  20.   

    this.num2.Show();
    this.num2.focus();
    可以试试