2011-10-11 107 views
10

我有用c#编写的Windows窗体应用程序。当有人按下“清除”按钮时,我想重新加载表单。但我无法实现呼叫负载事件。这些线路也没有工作:重新加载Windows窗体而不关闭并重新打开

this.Refresh(); 
    this.Load +=new EventHandler(Grafik_Load); // 'Grafik' is the name of the form. 

我应该怎么做呢?感谢您的帮助..

+1

Application.Restart();可能会解决 – Burimi

+0

,但它会显示一个闪烁(它的关闭和打开表单),这是该职位要求避免的。 – Sandy

回答

5

将“加载”代码放在一个单独的函数中,并从您自己的代码/加载事件处理函数中调用该函数。

+0

其实我已经有喜欢的负载功能: 私人无效Grafik_Load(对象发件人,EventArgs的){ .... } 但 我没有找到正确的语法来调用它 – user741319

+1

Grafik_Load(NULL,NULL ) 应该管用。 – CodingBarfield

0

我发现hide/show,show部分创建了同一个窗体的另一个实例,所以我最好放置当前窗体,创建它的一个新实例并显示它。

Grafik objFrmGrafik = new Grafik(); 
this.Dispose(); 
objFrmGrafik .Show(); 
0

首页为MDI格式名称。我已经测试过它。

home.ActiveForm.Dispose(); 
      home sd = new home(); 
      sd.Show(); 
4
 private void callonload() 
     { 
      //code which u wrriten on load event 
     } 
     private void Form_Load(object sender, EventArgs e) 
     { 
      callonload(); 
     } 
     private void btn_clear_Click(object sender, EventArgs e) 
     { 
      callonload(); 
     } 
0
//it is a good idea to use the 'sender' object when calling the form load method 
//because doing so will let you determine if the sender was a button click or something else... 

private void button2_Click(object sender, EventArgs e) 
{ 
    //you may want to reset any global variables or any other 
    //housekeeping before calling the form load method 
    Form1_Load(sender, e); 
} 

private void Form1_Load(object sender, EventArgs e) 
{ 
    if (sender is Button) 
    { 
     //the message box will only show if the sender is a button 
     MessageBox.Show("You Clicked a button"); 
    } 
} 
+0

欢迎来到StackOverflow。只有代码在他们的答案往往会被标记为删除,因为他们是“低质量”。请阅读关于回答问题的帮助部分,然后考虑在答案中添加一些评论。 – Graham

相关问题