如何从Form2刷新或更新MainForm [英] how to refresh or update MainForm from Form2

查看:79
本文介绍了如何从Form2刷新或更新MainForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有两种形式(MainForm和Form2). MainForm包含DataGridview Add 按钮,而Form2具有 Save 按钮.我想用更新的数据刷新MainForm后,单击了Form2的保存"按钮.我使用了以下代码:

this.refresh(),
this.Update(),
refreshform()//function written in mainForm but calling from Form2  



解决方案

,您可以公开Form2.Save按钮,并让MainFormMainForm中为该按钮添加事件处理程序,然后所有更新都会自动在MainForm中进行.

完全没有必要直接调用MainForm.


使用委托并从Form2引发事件.让您的主表单订阅该事件并执行刷新

 公共 部分  class  Form1:表格
{
    Form2 form2;
    公共 Form1()
    {
        InitializeComponent();
        form2 =  Form2();
        form2.FormDataSaved + =  Form2.OnDataUpdated(form2_FormDataSaved);
    }
    无效 form2_FormDataSaved()
    {
         .Refresh();
        // 但是您想更新此表单
    }
}

公共 部分  class  Form2:表格
{
    委托  void  OnDataUpdated();
    公共 事件 OnDataUpdated FormDataSaved;
    公共 Form2()
    {
        InitializeComponent();
    }
    私有 无效 SomeSaveOperation()
    {
        // 不论您保存的代码是什么
         .FormDataSaved();
    }
} 


从一种形式更新另一种形式使我感到有些不安.您正在做的就是将它们紧密地耦合在一起,这种方法往往会导致单片式应用程序,从而使您无法重用事物.

通常,您有第三件事(您是某种数据集?),它充当共享组件.然后,一种形式更新数据集,另一种形式自动响应.这样,您就可以使用任何一种形式而不必使用另一种形式.


hi all,

i''ve two forms(MainForm & Form2). MainForm contains DataGridview and an Add button, and Form2 has a Save button.I want to Refresh MainForm with Updated data After i clicked Save button of Form2. I used following codes:

this.refresh(),
this.Update(),
refreshform()//function written in mainForm but calling from Form2  



please reply with an exp.

解决方案

You can make the Form2.Save button public, and have MainForm add an event handler for that button in MainForm, and then all of the updating happens in MainForm automatically.

There''s absolutely no need to call into MainForm directly.


Use delegates and raise an event from Form2. Have your main form subcribe to the event and perform the refresh

public partial class Form1 : Form
{
    Form2 form2;
    public Form1()
    {
        InitializeComponent();
        form2 = new Form2();
        form2.FormDataSaved += new Form2.OnDataUpdated(form2_FormDataSaved);
    }
    void form2_FormDataSaved()
    {
        this.Refresh();
        // However you want to update this form
    }
}

public partial class Form2 : Form
{
    delegate void OnDataUpdated();
    public event OnDataUpdated FormDataSaved;
    public Form2()
    {
        InitializeComponent();
    }
    private void SomeSaveOperation()
    {
        // Whatever your save code is
        this.FormDataSaved();
    }
}


Updating one form from another makes me feel a little queasy to be honest. What you''re doing is tightly coupling them together and this approach tends to lead to monolithic applications where you''re ability to reuse things becomes limited.

You normally have a third thing (some sort of dataset in you case?) which acts as a shared component. Then, one form updates the the dataset and the other responds to it automatically. This way you can then use either form without the other.


这篇关于如何从Form2刷新或更新MainForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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