更新.窗体(1)打开另一个窗体(2).当Form(2)关闭时,Form(1)执行方法.需要帮忙! [英] Update. Form(1) opens another form(2). When Form(2) closes, Form(1) does method. Need Help!

查看:127
本文介绍了更新.窗体(1)打开另一个窗体(2).当Form(2)关闭时,Form(1)执行方法.需要帮忙!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是计算机科学系的学生.了解C#,委托和事件.

我已经制作了一个Windows Form(1),它具有一个可以打开另一个Form(2)的按钮.我希望能够在form(2)关闭之后在Form(1)上调用刷新方法.我目前正在处理form(2)上的AfterClosing事件,但我不知道如何从form(2)上调用form(1)上的方法.

人们可以提供帮助的任何建议或想法?

public partial class Form1 : Form //Windows form
private void button5_Click(object sender, EventArgs e) //Opens up webbrowser form
{
Form2 form2 = new Form2();
form2.Show();
}      


public partial class Form2 : Form //Windows form with web browser
{
   public Form2()
   {
      InitializeComponent();
   }
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
   {
      //Does refresh on form1
   }
}



提前致谢.

解决方案

简单:在创建Form2实例时,请插入FormClosing事件.然后,您将自动执行,并可以刷新所需的内容.这样,Form2不需要了解有关Form1的任何信息.

private void button5_Click(object sender, EventArgs e) //Opens up webbrowser form
   {
   Form2 form2 = new Form2();
   form2.FormClosing += new FormClosingEventHandler(ChildFormClosing);
   form2.Show();
   }
private void ChildFormClosing(object sender, FormClosingEventArgs e)
   {
   ...
   }


我将在Form1类中定义Form2_FormClosing()处理程序-这样,Form2无需了解有关Form1可以Refresh()本身.创建Form2之后,通常在显示它之前,在Form1上注册处理程序.

<编辑>
啊,OriginalGriff击败了我!
< \ Edit>

Dybs


您可以在from2关闭时在from1上调用方法,如下所示:

在Form1中这样写:

 公共  void  MyrefeshMethod()
        {
            MessageBox.Show(" );
        } 



并在Form2关闭事件中编写以下代码:

 私有  void  Form2_FormClosed(对象发​​件人,FormClosedEventArgs e)
        {
            Form1 frm =  Form1();
           
            frm.MyrefeshMethod();

        } 



希望这会有所帮助:)

有关更多查询,请在此处评论!!

Hi I am Computer Science Student. Learning about C#, delegates and events.

I have made a windows Form(1) that has a button that opens up another form(2). I want to be able to call a refresh method on Form(1) after form(2) closes. I am currently messing with the event AfterClosing on form(2), but I dont know how to call a method on form(1) from form(2).

Any suggestion or any ideas that people can help with?

public partial class Form1 : Form //Windows form
private void button5_Click(object sender, EventArgs e) //Opens up webbrowser form
{
Form2 form2 = new Form2();
form2.Show();
}      


public partial class Form2 : Form //Windows form with web browser
{
   public Form2()
   {
      InitializeComponent();
   }
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
   {
      //Does refresh on form1
   }
}



Thanks in Advance.

解决方案

Easy peasy: when you create the Form2 instance, hook into the FormClosing event. You will then automatically get executed, and can refresh whatever you need to. That way, Form2 does not need to know anything about form1.

private void button5_Click(object sender, EventArgs e) //Opens up webbrowser form
   {
   Form2 form2 = new Form2();
   form2.FormClosing += new FormClosingEventHandler(ChildFormClosing);
   form2.Show();
   }
private void ChildFormClosing(object sender, FormClosingEventArgs e)
   {
   ...
   }


I would define the Form2_FormClosing() handler in the Form1 class - that way Form2 doesn''t need to know anything about Form1, and Form1 can Refresh() itself. Register the handler on Form1 after you create Form2, typically before you show it.

<Edit>
Argh, OriginalGriff beat me to it!
<\Edit>

Dybs


You can call a method on from1 when from2 closes like this:

in Form1 write this:

public void MyrefeshMethod()
        {
            MessageBox.Show("refreshed");
        }



and in the Form2 closed event write this:

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Form1 frm = new Form1();
           
            frm.MyrefeshMethod();

        }



hope this helps :)

for further queries comment here!!


这篇关于更新.窗体(1)打开另一个窗体(2).当Form(2)关闭时,Form(1)执行方法.需要帮忙!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆