C#的WINDOWS FORM,为何LOAD方法不运行?

inori0821 2010-06-26 08:11:46
小弟初涉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,发现没有改变
请问这可能是为什么啊?这个方法不是在窗体加载时自动执行的吗?
...全文
445 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
csproj 2010-06-26
  • 打赏
  • 举报
回复
在设计界面双击窗体的话 也会生成
this.Load+=new EventHandler(Form1_Load)
只不过这个代码在Form1.Designer.cs 这个文件里.

VS2005以上版本 通过设计器完成的代码都隐藏在 XXXX.Designer.cs 文件里.

Yangbin990 2010-06-26
  • 打赏
  • 举报
回复
1 楼,

给解释一下吧
  • 打赏
  • 举报
回复
这个应该是线程在作怪,Form1先于Form1_Load执行。放在Form1_Load里,Form1_Load方法执行完了,可能线程还没有执行
inori0821 2010-06-26
  • 打赏
  • 举报
回复
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中直接添加这个方法嘛
wuyq11 2010-06-26
  • 打赏
  • 举报
回复
public Form1()
{
InitializeComponent();
this.Load+=new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
DebugCheck.Text = "Now load!";
}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧