-1
          Form= new form();

          Form.showDialog();

          This.close();

The new form opens but the this.close doesn't seem to activate. So now the new form is open but the old form is still there.

Note I am trying to close and open a new instance of the same form.

  • `Form.ShowDialog` Shows the form as a modal dialog box. – TheGeneral Aug 30 '19 at 23:54
  • Opening the same form again and closing the the existing one seems a little brittle, even if you make this non modal seems it like this could be done a better way. Why not just change your form dynamically? thats to say, what ever you do when opening the new form can probably be done in the existing one – TheGeneral Aug 30 '19 at 23:56
  • Not sure if we are on the same track here but what I really want to do is refresh this form after I have completed a transaction.. wipe the existing data from all textbox etc...it's a lot of textbox and listbox and other controls so doing a clear or reset for each individual will be .... – Rickal Hamilton Aug 31 '19 at 00:05
  • Don't use ShowDialog when you did not intend to create a dialog. If Show() makes your app terminate too soon, the most typical reason to get this wrong, then [look here](https://stackoverflow.com/questions/10769193/how-do-i-prevent-the-app-from-terminating-when-i-close-the-startup-form). – Hans Passant Aug 31 '19 at 00:15
  • How can I use show() keep my main form open but blocked from user ...make only new form usable untill closed? Thanks for the help I really appreciate – Rickal Hamilton Aug 31 '19 at 00:40

1 Answers1

0

Form.ShowDialog is a blocking call. This means that until it returns, the code after it won't run (and if that's on the UI thread, your application will appear unresponsive). From the MSDN page:

You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed.

You probably want Form.Show instead, to get a window without blocking. You haven't described your full use case, but showing something else and closing yourself is a very strange thing to do, guessing there is a better way to solve your problem as well.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • Yes sorry if my question was a bit vague am a bit new to this ...yes I am trying to open the new form while keeping the main form open but blocked....I mean it works fine with .show but . Show doesn't give the option to block the main form. – Rickal Hamilton Aug 31 '19 at 00:03
  • I think I will just take the long rout and clear the textbox one at a time – Rickal Hamilton Aug 31 '19 at 00:42
  • @RickalHamilton Stuff like this (clearing forms out) is way simpler with Data Binding in WPF. Unless you have a hard requirement to use WinForms, you might consider upgrading to the state of the art :) – BradleyDotNET Sep 03 '19 at 15:50