dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Close and Keep

  1. #1
    Join Date
    Oct 2010
    Location
    Net 4.0 / VS 2010
    Posts
    43

    Question Close and Keep

    How should I code my application so it doesn't close out the whole application, (this.close) but just closes the form I am viewing? (So my program opens a new window then closes the other window, but how?)

  2. #2
    Join Date
    Oct 2010
    Location
    Net 4.0 / VS 2010
    Posts
    43

    Re: Close and Keep

    Anyone?

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,227

    Re: Close and Keep

    Can you describe what you are trying to do in more detail?

    How about a real example (other than closing one form and opening another)?

  4. #4
    Join Date
    Oct 2010
    Location
    Net 4.0 / VS 2010
    Posts
    43

    Re: Close and Keep

    So I'm trying to close Form1, OK? And then when I say

    ACAdmin acADMIN = new ACAdmin();
    acAdmin.Show();
    this.Close();

    It closes the whole thing..

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,227

    Re: Close and Keep

    If form1 is the main application form, when you close it, the application will quit running.

    Probably the simplest way to do what you want is to simply hide the main form.

    Code:
    private void buttonAdmin_Click( object sender, EventArgs e )
    {
      Visible = false;
      
      switch ( ACAdmin.ShowFrm( this ) )
      {
      case DialogResult.OK:
        Close( );
        break;
      case DialogResult.Cancel:
        TopLevel = true; 
        Visible = true;
      break;
      }
    }
    The code above displays the ACAdmin form and hides the main form. It Closes the main form if the user presses OK. If the user presses cancel. it closes the ACAdmin form but keeps the main form open.

    A couple of comments:

    If ShowFrm( ) doesn't look like a Form method, it's because it's a static helper method that wraps Form.ShowDialog( ).

    Code:
    public static DialogResult ShowFrm( IWin32Window owner )
    {
      var acAdmin = new ACAdmin( );
      return acAdmin.ShowDialog( owner );
    }
    Form.ShowDialog returns a enum DialogResults. You'll see that looking for the OK and Cancel values in the switch statement above. These are 'mapped' to the ACAdmin OK and Cancel buttons, but it isn't obvious how they're mapped. To map them, simply highlight the button, rightclick and open the properties window, and then set the DialogResult property. Since I've mapped both buttons in the ACAdmin form, the appropriate value will get returned when a button is clicked.

    Based on which button is pressed I either close the main form or unhides it.

    You'll notice I'm passing the this pointer to the ShowFrm method. This passes in the parent window handle to the ACAdmin form and allows us to center this form to the parent (it would be kind of goofy to have the parent form disappear and have the child form appear at a different location).

    Btw, using this. within the context of a class (e.g. this.Close( ) ) is unnecessary, because this is implied when you are calling members of a class from within a class.

    See the attached solution.
    Attached Files Attached Files

  6. #6
    Join Date
    Oct 2010
    Posts
    12

    Re: Close and Keep

    the "this" keyword simply put means "the active form" in the case your are using it in.

    So this would mean if the active form happens to be the main form or only form that the programs starts with, it will close the program.

    If you are looking to make a program look and behave like for example microsoft word where the menu always stays up the top and the "documents" are loaded in.

    Then this is called an MDI application. You can google for tutorials on this, but be warned it is far different from just opening forms from within forms.

    Hope this helps.

    Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)