• 0

C# closing and reopening the same form


Question

        private void ReOpen()
        {
            Hide();
            var reopen = new FrmPlaceItem();
            reopen.ShowDialog();
            Dispose();
        }

Soooo....  when I call this method, is it actually closing the current form and reopening a new one; or is the previous form still hidden :/

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Hiding the form does not close it. To close the form, call Close() (you don't need to call Dispose()). 

 

ShowDialog() is a blocking call that doesn't return until the dialog is closed. If you call Close() after calling ShowDialog() on the new form, the current form won't close until the dialog is closed. Avoid using ShowDialog() unless you actually want a modal window.

 

It doesn't make any sense to try to show a modal window after closing the parent window (hence why your code can't work). Not sure what you're trying to achieve...

Link to comment
Share on other sites

  • 0

Hiding the form does not close it. To close the form, call Close() (you don't need to call Dispose()). 

 

ShowDialog() is a blocking call that doesn't return until the dialog is closed. If you call Close() after calling ShowDialog() on the new form, the current form won't close until the dialog is closed. Avoid using ShowDialog() unless you actually want a modal window.

 

It doesn't make any sense to try to show a modal window after closing the parent window (hence why your code can't work). Not sure what you're trying to achieve...

I have an excel add-in and when someone clicks "Place and Next" its to do its operation, move down a row in excel, and reopen the same form again.

Link to comment
Share on other sites

  • 0

Basically in order to do something like that.. you would have something like:
 

private void openChildWindow()
{
     MyChildForm childForm = new MyChildForm();
     childForm.ShowDialog();
}

then you just have an event every cell click like, or during your "Pick and Next" you would call the function.
 

private void Cell_Click(Object sender, EventArgs e)
{
     openChildWindow();
}

It doesn't make sense to open an instance of a child form inside of the child form.  You need a parent to control that.

Link to comment
Share on other sites

  • 0

Basically in order to do something like that.. you would have something like:

 

private void openChildWindow()
{
     MyChildForm childForm = new MyChildForm();
     childForm.ShowDialog();
}

then you just have an event every cell click like, or during your "Pick and Next" you would call the function.

 

private void Cell_Click(Object sender, EventArgs e)
{
     openChildWindow();
}

It doesn't make sense to open an instance of a child form inside of the child form.  You need a parent to control that.

Ah well that explanation makes much more sense.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.