小弟初涉C#,尝试创建了一个C# PROJECT,在form1_Load方法中添加了一些要初始化的变量
发现编译后,Load方法并没有被执行
        public Form1()
        {
            InitializeComponent();
            //DebugCheck.Text = "Now load!";
            //_threadCheck = new Thread(new ParameterizedThreadStart(doCheck));
            //_threadCheck.Start();
        }
        private void Form1_Load(object sender, System.EventArgs e)
        {
            DebugCheck.Text = "Now load!";
            _threadCheck = new Thread(new ParameterizedThreadStart(doCheck));
            _threadCheck.Start();
        }
代码片段如上,为了校验,我在FORM中添加了一个label,命名为DebugCheck,然后希望在加载时,初始化这个label,发现没有改变
请问这可能是为什么啊?这个方法不是在窗体加载时自动执行的吗?

解决方案 »

  1.   

    public Form1() 
            { 
                InitializeComponent(); 
                this.Load+=new EventHandler(Form1_Load);        
            } 
            private void Form1_Load(object sender, EventArgs e) 
            {
              DebugCheck.Text = "Now load!";   
            } 
      

  2.   

    OK了
    可以请教下,直接放在form1()中和放在load方法中,有区别吗?而且我看MSDN的资料static int x = 200;
    static int y = 200;private void Button1_Click(System.Object sender, 
        System.EventArgs e)
    {
        // Create a new Form1 and set its Visible property to true.
        Form1 form2 = new Form1();
        form2.Visible = true;    // Set the new form's desktop location so it  
        // appears below and to the right of the current form.
        form2.SetDesktopLocation(x, y);
        x += 30;
        y += 30;    // Keep the current form active by calling the Activate
        // method.
        this.Activate();
        this.Button1.Enabled = false;
    }// Updates the label text to reflect the current values of x 
    // and y, which was were incremented in the Button1 control's 
    // click event.
    private void Form1_Activated(object sender, System.EventArgs e)
    {
        Label1.Text = "x: "+x+" y: "+y;
        Label2.Text = "Number of forms currently open: "+count;
    }static int count = 0;private void Form1_Closed(object sender, System.EventArgs e)
    {
        count -= 1;
    }private void Form1_Load(object sender, System.EventArgs e)
    {
        count += 1;
    }他并没有在Form1中直接添加这个方法嘛
      

  3.   

    这个应该是线程在作怪,Form1先于Form1_Load执行。放在Form1_Load里,Form1_Load方法执行完了,可能线程还没有执行
      

  4.   

    在设计界面双击窗体的话 也会生成
    this.Load+=new EventHandler(Form1_Load)
    只不过这个代码在Form1.Designer.cs 这个文件里.VS2005以上版本 通过设计器完成的代码都隐藏在 XXXX.Designer.cs 文件里.