C# c winforms-在模态窗体之间传递参数

C# c winforms-在模态窗体之间传递参数,c#,winforms,modal-dialog,C#,Winforms,Modal Dialog,我有一个带有购买按钮的主窗体,它打开一个带有组合框和两个按钮的模式窗体,确认和关闭 当我选择项目并单击“确认”时,它将自动保存在数据库中 我想做的是,当我点击“关闭”按钮并返回主窗体时,只有在我至少购买了1项时,才能刷新DataGridView。要在窗体关闭时执行某些操作,请将处理程序附加到FormClosed事件,您可以从窗体的处理程序中访问所需的任何信息: OtherForm other = new OtherForm(); other.FormClosed += (s, args) =&g

我有一个带有购买按钮的主窗体,它打开一个带有组合框和两个按钮的模式窗体,确认和关闭

当我选择项目并单击“确认”时,它将自动保存在数据库中


我想做的是,当我点击“关闭”按钮并返回主窗体时,只有在我至少购买了1项时,才能刷新DataGridView。

要在窗体关闭时执行某些操作,请将处理程序附加到FormClosed事件,您可以从窗体的处理程序中访问所需的任何信息:

OtherForm other = new OtherForm();
other.FormClosed += (s, args) => 
{
    if(otherForm.SomeInformation == someValue)
        DoSomething(other.SomeData);
};
other.Show();

要在窗体关闭时执行某些操作,请将处理程序附加到FormClosed事件,您可以从窗体的处理程序中访问所需的任何信息:

OtherForm other = new OtherForm();
other.FormClosed += (s, args) => 
{
    if(otherForm.SomeInformation == someValue)
        DoSomething(other.SomeData);
};
other.Show();

我通常在模态表单本身上编写一个helper方法,如下所示:

public static ModalResponse ShowModal()
{
    OtherForm otherForm = new OtherForm();  
    otherForm.ShowDialog();
    //craft a ModalResponse based on the state of otherForm, and return it here
}
并在父窗体中这样调用它:

public void button_Click(object sender, EventArgs e)
{
    var response = OtherForm.ShowModal();
    //do things with the response
}
ModalResponse不是框架中某个地方存在的类,它只是一个示例


我之所以喜欢这种方法而不仅仅是捕获DialogResult,是因为DialogResult通常只会告诉您单击了什么按钮。如果您需要有关OP所提到的表单输入状态的更多信息,那么无论如何都需要一个比枚举更丰富的“更丰富”对象。

我通常在模态表单本身上编写一个helper方法,如下所示:

public static ModalResponse ShowModal()
{
    OtherForm otherForm = new OtherForm();  
    otherForm.ShowDialog();
    //craft a ModalResponse based on the state of otherForm, and return it here
}
并在父窗体中这样调用它:

public void button_Click(object sender, EventArgs e)
{
    var response = OtherForm.ShowModal();
    //do things with the response
}
ModalResponse不是框架中某个地方存在的类,它只是一个示例


我之所以喜欢这种方法而不仅仅是捕获DialogResult,是因为DialogResult通常只会告诉您单击了什么按钮。如果您需要有关OP所述表单输入状态的更多信息,则无论如何都需要一个比枚举更丰富的“更丰富”对象。

表单是一个类似于许多其他类的类,可以包含属性。 窗体的生存期不一定与其可见性同步

这就是为什么可以在显示窗体之前设置属性,在模态窗体后面的代码中更改它们,然后在窗体关闭后使用它们

// Create a instance of the modal form
using(var frm = new MyModalForm())
{
    // Initialize some properties
    frm.Text = "Product catalog";
    ...

    // Show the modal form. At this time, the code execution is suspended until
    // the modal form is closed
    frm.ShowDialog(this);

    // Once the form is closed, it remains instanciated and can still be used
    if (frm.RefreshRequired)
    {
        // Perform the datagrid refresh
    }
}

// At this time, the form is disposed because of the using block, and
// all the resources it used are cleaned

public class MyModalForm : ...
{
    ...
    public MyModalForm()
    {
        this.InitializeComponent();
    }

    // Set this flag to true or false according to the
    // operations done in the modal form
    // (in the event code of the buttons for example)

    public bool RefreshRequired = false;

    ...
}

当然,可以使用DialogResult属性,但这种方式允许检索更多的属性,例如所选产品的列表。

表单与其他许多属性一样是一个类,可以包含属性。 窗体的生存期不一定与其可见性同步

这就是为什么可以在显示窗体之前设置属性,在模态窗体后面的代码中更改它们,然后在窗体关闭后使用它们

// Create a instance of the modal form
using(var frm = new MyModalForm())
{
    // Initialize some properties
    frm.Text = "Product catalog";
    ...

    // Show the modal form. At this time, the code execution is suspended until
    // the modal form is closed
    frm.ShowDialog(this);

    // Once the form is closed, it remains instanciated and can still be used
    if (frm.RefreshRequired)
    {
        // Perform the datagrid refresh
    }
}

// At this time, the form is disposed because of the using block, and
// all the resources it used are cleaned

public class MyModalForm : ...
{
    ...
    public MyModalForm()
    {
        this.InitializeComponent();
    }

    // Set this flag to true or false according to the
    // operations done in the modal form
    // (in the event code of the buttons for example)

    public bool RefreshRequired = false;

    ...
}
当然,可以使用DialogResult属性,但这种方式允许检索更多属性,例如所选产品的列表。

我将使用表单的DialogResult来确定是否单击了关闭或取消:

public class Modal : Form
{
    public Modal {
        InitializeComponent();
    }

    private void Confirm_Click(object sender, EventArgs e) {
        this.DialogResult = DialogResult.OK;
    }

    private void Cancel_Click(object sender, EventArgs e) {
        this.DialogResult = DialogResult.Cancel;
    }
}
然后你可以像这样使用它

using (var modal = new Modal()) {
    modal.ShowDialog();
    if (modal.DialogResult == DialogResult.OK) {
        // respond to confirm click here.
    }
    else if (modal.DialogResult == DialogResult.Cancel) {
        // respond to cancel click here.
    }
}
很好的一点是,由于您实例化了该模式,您仍然可以在DialogResult检查中访问其属性。您的Modal类可以设置一个属性,指定至少购买了一个项目,然后检查该属性,如果您知道单击了“取消”,则在else中响应。

我将使用表单的对话框结果来确定单击了“关闭”还是“取消”:

public class Modal : Form
{
    public Modal {
        InitializeComponent();
    }

    private void Confirm_Click(object sender, EventArgs e) {
        this.DialogResult = DialogResult.OK;
    }

    private void Cancel_Click(object sender, EventArgs e) {
        this.DialogResult = DialogResult.Cancel;
    }
}
然后你可以像这样使用它

using (var modal = new Modal()) {
    modal.ShowDialog();
    if (modal.DialogResult == DialogResult.OK) {
        // respond to confirm click here.
    }
    else if (modal.DialogResult == DialogResult.Cancel) {
        // respond to cancel click here.
    }
}

很好的一点是,由于您实例化了该模式,您仍然可以在DialogResult检查中访问其属性。您的Modal类可以设置一个属性,指定至少购买了一个项目,然后检查该属性,如果您知道单击了取消,则在else中响应。

这是一个非常模糊/广泛的问题,除非您在代码中向我们展示一些细节。只需为表单的DialogResult属性指定适当的值即可。关闭对话框并设置ShowDialog调用的返回值。使用DialogResult。如果刷新DGV没有任何意义,请取消。模态形式对初学者来说很混乱。许多人认为表单在关闭时会永远消失,但事实并非如此,因为它只是一个类实例。DialogResult属性模式加剧了这方面的混乱。除非您在代码中向我们展示一些细节,否则这是一个相当模糊/广泛的问题。只需为表单的DialogResult属性指定一个适当的值即可。关闭对话框并设置ShowDialog调用的返回值。使用DialogResult。如果刷新DGV没有任何意义,请取消。模态形式对初学者来说很混乱。许多人认为表单在关闭时会永远消失,但事实并非如此,因为它只是一个类实例。DialogResult属性模式加剧了这方面的混乱。请您举例说明您的代码,我无法理解。谢谢。@Tima怎么样?你不明白吗?将处理程序附加到此事件,然后在窗体关闭时执行任何操作。这就是全部我的意思是,args@Tima它们是事件处理程序的参数。你不需要使用它们中的任何一个,它们只是一个
当我把第二个+=改为=>时,你的代码才起作用,你是说它是+=还是仅仅是一个错误?请你举例说明你的代码好吗?我看不懂。谢谢。@Tima怎么样?你不明白吗?将处理程序附加到此事件,然后在窗体关闭时执行任何操作。这就是全部我的意思是,args@Tima它们是事件处理程序的参数。你不需要使用它们中的任何一个,它们只需要在那里。只有当我将第二个+=更改为=>时,你的代码才起作用,你的意思是+=还是这只是一个错误?我尝试了这个并成功了,谢谢。为了学习,我还会尝试其他回复。再次感谢。我试过了,成功了,谢谢。为了学习,我还会尝试其他回复。再次感谢。