Taher
Taher

Reputation: 593

c# winforms -Pass Parameter Between Modal Forms

I have a main form with Purchase button that opens a modal form with combo box and 2 buttons, Confirm and Close.

When I select the item and click Confirm it is automatically saved in the database.

What I want to do is when I hit Close button and return to the main form, the DataGridView must be refreshed only if I purchased at least 1 item.

Upvotes: 1

Views: 2994

Answers (4)

Servy
Servy

Reputation: 203840

To do something when the form is closed attach a handler to the FormClosed event, you can access whatever information you need from the form in its handler:

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

Upvotes: 3

nateirvin
nateirvin

Reputation: 1183

I generally write a helper method on the modal form itself, like so:

public static ModalResponse ShowModal()
{
    OtherForm otherForm = new OtherForm();  
    otherForm.ShowDialog();
    //craft a ModalResponse based on the state of otherForm, and return it here
}

and invoke it like so in the parent Form:

public void button_Click(object sender, EventArgs e)
{
    var response = OtherForm.ShowModal();
    //do things with the response
}

(ModalResponse is not a class that exists somewhere in the framework, it's just an example.)

The reason I favor this approach rather than just capturing the DialogResult is because DialogResult will (generally) only tell you what button was clicked. If you need more info about the state of the form input (as the OP mentioned), then a 'richer' object (richer than an Enum anyway) is needed.

Upvotes: 2

Neil Smith
Neil Smith

Reputation: 2565

I would use the form's DialogResult to determine if close or canceled was clicked:

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

And then you can use it like

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.
    }
}

The nice part is since you instantiated the Modal, you can still access its properties in your DialogResult checks. You're Modal class can set a property that specifies that at least one item was purchased and then check that property and respond in the else if where you know Cancel was clicked.

Upvotes: 0

Larry
Larry

Reputation: 18051

A Form is a class like many other and can contains properties. The lifetime of a Form is not necessarily in sync with its visibility.

That's why you can set properties before the form is shown, change them in the code behind the modal form, then use them after the form has been closed.

// 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;

    ...
}

Of course, the DialogResult property can be used, but this way allows many more properties to be retrieved, as the list of selected product for example.

Upvotes: 2

Related Questions