Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogResult that doesn't close the form?

I have a form Form1 from which I display Form2 as a modal form. From Form2 I do all sort of editing and deleting of different set of values which should be reflected in Form1 after closing Form2. So what I do is RePopulateControls_in_Form1() after closing Form2. Since RePopulateControls_in_Form1()is a long process, I want to execute that method only if some modification (edit,add, delete) happens in Form2 and not when Form2 is just opened and closed.

So this is what I try to do in Form1:

        Form2 f = new Form2();
        if (f.ShowDialog(this) == DialogResult.Something)
            RePopulateControls_in_Form1()

And then in Form2 I do,

    private void bntEdit()
    {
        //If Edit?
        this.DialogResult = DialogResult.Something;
    }
    private void bntAdd()
    {
        //If Add?
        this.DialogResult = DialogResult.Something;
    }
    private void bntDelete()
    {
        //If Delete?
        this.DialogResult = DialogResult.Something;
    }

But my problem is .Something. If it is anything other than .None, Form2 simply gets closed. I do not want Form2 to get simply closed by its own unless the user closes it.

If I do this:

    //in Form1
    private void Form1_Click()
    {
        Form2 f = new Form2();
        if (f.ShowDialog(this) == DialogResult.None)
            RePopulateControls_in_Form1()
    }

    //in Form2
    private void Form2_SomeModification()
    {
        //If Modified?
        this.DialogResult = DialogResult.None;
    }

RePopulateControls_in_Form1() is not hit!

In short, in my program how can I tell the compiler to call RePopulateControls_in_Form1() only if values are modified in Form2?

Note: Repopulating is certainly required since the controls are dynamically created and a bit complex (actually what is created in Form2 is GUI controls and its labels etc).

like image 277
nawfal Avatar asked Jul 30 '11 05:07

nawfal


People also ask

What is dialog result?

The dialog result of a form is the value that is returned from the form when it is displayed as a modal dialog box.

Which are the values of Dialogresult enumeration?

The dialog box return value is Continue (usually sent from a button labeled Continue). The dialog box return value is Ignore (usually sent from a button labeled Ignore). The dialog box return value is No (usually sent from a button labeled No). Nothing is returned from the dialog box.


2 Answers

Setting DialogResult on Form hides the form and returns from ShowDialog. If you want to delay that until the user performs some other action (such as closing the form) you should store that state as a member field and set DialogResult in a handler for Form.Closing.

Also, if you do want to dismiss the modal form on a button press, you can use the Button.DialogResult property instead of making a Button.Click handler.

like image 94
Ryan Russell Avatar answered Sep 19 '22 02:09

Ryan Russell


A simple way might be not to use DialogResult at all but a dedicated property not interfering with the Form's behavior. - Then you should be able to program any logic you want.

like image 20
Nico Avatar answered Sep 22 '22 02:09

Nico