DialogResult that doesn't close the form?
Asked Answered
J

3

7

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

Jocosity answered 30/7, 2011 at 5:36 Comment(0)
L
12

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.

Lowerclassman answered 30/7, 2011 at 5:48 Comment(0)
J
2

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.

Joli answered 30/7, 2011 at 5:55 Comment(1)
But I feel @Ryan Russell has such a cool call..Let me mark hisJocosity
P
0

I would use an event in Form 2. Fire that event when your Form2 is closing. Handling that event in Form1 would allow you to carry out any processing you want to. Further if needs be you could pass back some information from Form2 to Form1 in parameters to the event.

An alternative would be to set up a Global static variable - maybe just a bool. Then Form2 can set it to true or false depending on whether there are changes made. Form1 can read this when Form2 returns and if true carry out processing and set it back to false.

Priggery answered 30/7, 2011 at 5:50 Comment(3)
OK I get the first part, but the 2nd logic is not a proper coding technique!Jocosity
Why is it not? In the case above I would use an event. However if there is a value that is used throughout an application that has 20 forms and ten times that many classes how would you ensure that only one value is used. Would you prefer to pass the value from form to form and class to class via a constructor parameter or properties....Priggery
This is something as simple as just setting a flag to let Form1 know something about Form2. Why a global variable which isn't thread safe? I understand significance of a general global variable when you have to use it across 220 classes as you mention above. But here, not only that we should avoid static variables, which sleeps throughout the entire life cycle of application, as much as possible, but more importantly I'm wondering how would you foresee the requirement mentioned in the question scaling out to be something as big as to be employed in 220 classes from my simple question :)Jocosity

© 2022 - 2024 — McMap. All rights reserved.