开发者

Reloading windows form without closing and reopening

开发者 https://www.devze.com 2023-04-12 00:10 出处:网络
I have an windows forms application written in c#. I want to reload form when someone press the \"clear\" button in it. But I couldn\'t achieve to call Load event.These lines didn\'t also work :

I have an windows forms application written in c#. I want to reload form when someone press the "clear" button in it. But I couldn't achieve to call Load event.These lines didn't also work :

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

What should I do about thi开发者_开发问答s? Thanks for helping..


Place the 'load' code in a separate function and call that function from you're own code/Load event handler.


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


i found that the hide/show, the show part creates another instance of the same form, so I better dispose the current one, create a new instance of it, and show it.

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


Home is MDI-Form name. i have tested it.

 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");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号