簡體   English   中英

重新加載窗體,無需關閉和重新打開

[英]Reloading windows form without closing and reopening

我有一個用c#編寫的Windows窗體應用程序。 當有人按下“清除”按鈕時,我想重新加載表單。 但我無法實現調用Load事件。這些行也不起作用:

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

我該怎么辦? 謝謝你幫忙..

將'load'代碼放在一個單獨的函數中,並從您自己的代碼/ Load事件處理程序中調用該函數。

        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();
        }

我發現hide / show,show部分創建了同一個表單的另一個實例,所以我最好配置當前的一個,創建它的新實例,並顯示它。

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

Home是MDI-Form名稱。 我測試過了。

 home.ActiveForm.Dispose();
            home sd = new home();
            sd.Show();
//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");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM