DialogResult that doesn't close the form?

10,222

Solution 1

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.

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

Share:
10,222
nawfal
Author by

nawfal

You should accept the answer if it helped you by selecting the green tick mark on the top left of the answer. That's the encouraged practice at SO. Not only it helps future visitors, it encourages users to answer your questions. You should post what you have tried and tell us where you're stuck. That gives us more detail, and we can quickly give an answer by copy-pasting the code with minor modifications. Questions like I-need-this,-give-me-code doesnt work in SO. In the current format the question will be closed. You may read http://stackoverflow.com/faq additionally before posting. Do a good search on SO to ensure you're not asking a duplicated question, or else your question will be closed. Quick tag search

Updated on June 15, 2022

Comments

  • nawfal
    nawfal almost 2 years

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

  • nawfal
    nawfal almost 13 years
    OK I get the first part, but the 2nd logic is not a proper coding technique!
  • nawfal
    nawfal almost 13 years
    But I feel @Ryan Russell has such a cool call..Let me mark his
  • ScruffyDuck
    ScruffyDuck almost 13 years
    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....
  • nawfal
    nawfal almost 13 years
    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 :)