WinForms opening and closing forms sequentially

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?=

    WinForms opening and closing forms sequentially

    I am (still) relatively new to Windows applications, most of my experience
    has been Web based, and I am confused about what exactly happens when the
    Main() method is called and how to manipulate forms opening & closing.

    An example of this issue is as follows. I have a logon form open as the
    first thing. The main functional form opens when a user has successfully
    logged on. From the main form, a user should be able to logout which will
    reshow the logon form and the state should be essentially identical to if
    they have never logged on in the first place. What I find is that I can't
    close any form in this string, or everything shuts down, so I have to hide
    forms. But if I do that, then they still lurk in the background and do
    strange things.

    I have been thinking that I should make an orchestrator class with a method
    something like this

    public void ReplaceCurrentl yShownForm(Form toOpen, Form currentlyOpen)
    {
    currentlyOpen.C lose();
    currentlyOpen.D ispose();
    toOpen.Show();
    }
    and have Main instantiate this class which then runs until the whole app is
    closed down.
    Would that make sense?

    I have been looking for examples of code in which different windows forms
    are show at different times in the program, but I have not yet found
    anything. If you could point me to something, that would be helpful.
    Thanks!
    Ethan

  • Peter Duniho

    #2
    Re: WinForms opening and closing forms sequentially

    On Thu, 18 Sep 2008 12:59:01 -0700, Ethan Strauss
    <EthanStrauss@d iscussions.micr osoft.comwrote:
    I am (still) relatively new to Windows applications, most of my
    experience
    has been Web based, and I am confused about what exactly happens when
    the
    Main() method is called and how to manipulate forms opening & closing.
    I recommend looking at the Main() method then. It's in, by default, the
    Program.cs file. Also by default, it only has three things it does: two
    calls to initialize some of the GUI stuff, and then a statement that calls
    Application.Run () with a new form instance. Application.Run () will return
    once the form passed to it is closed, closing all other open forms owned
    by that thread (which is usually all other open forms without exception)
    at the same time.

    One way to change the behavior of your program is to change the Main()
    method so that it calls Application.Run () differently. For example, if
    you don't pass it a form instance, then closing any form won't cause
    Application.Run () to exit. Only a call to Application.Exi tThread()
    would. That gives you more control over the lifetime of your application,
    regardless of which forms are shown or closed.
    An example of this issue is as follows. I have a logon form open as the
    first thing. The main functional form opens when a user has successfully
    logged on. From the main form, a user should be able to logout which
    will
    reshow the logon form and the state should be essentially identical to if
    they have never logged on in the first place. What I find is that I can't
    close any form in this string, or everything shuts down, so I have to
    hide
    forms. But if I do that, then they still lurk in the background and do
    strange things.
    "Do strange things"? Like what? If you forms are doing strange things
    when hidden, then that's something you should be concerned about. Not
    because it's an impediment to the basic functionality, but because you've
    made a mistake in authoring your forms somehow. Ideally, your forms
    should be strictly UI, but in any case there's no reason that they should
    do anything "strange" while hidden.

    Also, it should not be trued that you "can't close any form in this
    string". By default, there's only one form that's critical: the one
    passed to Application.Run (). You can show and close arbitrarily many
    other forms without problem; it's only closing that first form that would
    cause the application to end.

    So, another option is to show/close other forms as you'd like, just taking
    care to never close the logon form. Hide that when it's not needed, show
    it when it is. Close it when you want the application to exit. If it
    does "strange things" when it's hidden, fix it so that it doesn't.
    I have been thinking that I should make an orchestrator class with a
    method
    something like this
    >
    public void ReplaceCurrentl yShownForm(Form toOpen, Form
    currentlyOpen)
    {
    currentlyOpen.C lose();
    currentlyOpen.D ispose();
    toOpen.Show();
    }
    and have Main instantiate this class which then runs until the whole app
    is
    closed down.
    Would that make sense?
    How would that help? At some point, you still need to call
    Application.Run (). And with or without a method such as above, the same
    rules apply.

    By the way, Form.Close() does a Dispose() implicitly, unless you used
    Form.ShowDialog () to show the form. There's no need to call Dispose()
    after closing a non-modal form.

    Pete

    Comment

    • =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?=

      #3
      Re: WinForms opening and closing forms sequentially

      Thanks Peter.
      That's helpful. I think I was making things way more complex than they
      should be by using .ShowDialog() when that was not really what I wanted,
      showing, hiding, and reshowing forms without making sure they were properly
      refreshed and so forth. I think I can probably go back through and simply
      everything by making sure that the initial form is not closed and making sure
      I reset everything when a user ends up back at a form that had existed
      before.
      Ethan

      Comment

      Working...