Jump to content



Photo

C# closing and reopening the same form


  • Please log in to reply
7 replies to this topic

#1 Kalint

Kalint

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 16-January 07

Posted 27 September 2013 - 01:14

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




#2 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 12
  • Joined: 26-October 05

Posted 27 September 2013 - 04:09

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



#3 OP Kalint

Kalint

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 16-January 07

Posted 27 September 2013 - 15:48

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.



#4 firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 8
  • Joined: 30-October 05
  • Location: Alberta, Canada
  • OS: Windows 7
  • Phone: Android (4.4.2)

Posted 28 September 2013 - 12:19

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.



#5 OP Kalint

Kalint

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 16-January 07

Posted 28 September 2013 - 19:11

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.



#6 firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 8
  • Joined: 30-October 05
  • Location: Alberta, Canada
  • OS: Windows 7
  • Phone: Android (4.4.2)

Posted 28 September 2013 - 20:35

Ah well that explanation makes much more sense.

You get it all figured out?



#7 OP Kalint

Kalint

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 16-January 07

Posted 28 September 2013 - 21:06

You get it all figured out?

Yes I have, thanks!



#8 firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 8
  • Joined: 30-October 05
  • Location: Alberta, Canada
  • OS: Windows 7
  • Phone: Android (4.4.2)

Posted 28 September 2013 - 23:30

Yes I have, thanks!

Awesome :D no problem.