CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2008
    Posts
    1

    Post difficulty closing parent form and creating a new one (when child form closes)

    Briefly:
    Hi, I have a form A that opens a child form B (by clicking a button in form A). In form B, data is updated and form B is closed when the OK button is clicked. Because of the way I've implemented my database I need to create a new form A and delete the old one when form B closes - this will ensure my data is updated in form A.

    I have tried the following (which makes sense in my head) but when I click the OK button on the NewPatientForm (form B), both form A and B close without displaying a new form A:

    Code:
    //Form A
    public partial class EPTForm : Form
        {
            public EPTForm()
            {
                InitializeComponent();
            }
    
            private void btnNewPatient_Click(object sender, EventArgs e)
            {
                NewPatientForm frm = new NewPatientForm(this);
                frm.ShowDialog();
            }
        }
    
    //Form B
    public partial class NewPatientForm : Form
        {
            EPTForm frmEptFormInstance = null;
    
            public NewPatientForm(EPTForm frm)
            {
                InitializeComponent();
                this.frmEptFormInstance = frm;
            }
    
            private void btnOk_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void NewPatientForm_FormClosing(object sender, FormClosingEventArgs e)
            {
                this.frmEptFormInstance.Close();
    
                EPTForm eptFrm = new EPTForm();
                eptFrm.Show();
            }
        }

    I hope this explanation is clear enough, please let me know if you need clarification. Thanks for any help anyone can provide!

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

    Re: difficulty closing parent form and creating a new one (when child form closes)

    Generally having to reopen a form indicates a flaw in the design.

    I don't know what FormA contains exactly, so I'm going to guess that it's a list of patients retrieved from the database.

    Assuming this is correct, then you just need to make another database call to request the data after form b has closed (and has inserted a record into the database).

    Most folks populate a dialog from a database in OnLoad. If that is correct, just move the database retrieval code into a separate method that can be called from OnLoad. This same method can be called again after sucessfully inserting a new patient record.

    Code:
    //Form A
    private void btnNewPatient_Click(object sender, EventArgs e)
    {
      NewPatientForm frm = new NewPatientForm(this);
      if( DialogResult.Ok == frm.ShowDialog() )
      {
        RetrievePatientRecords( );
      }
    }
    
    private void OnLoad(object sender, EventArgs e)
    {
      RetrievePatientRecords( );
    }
    
    private void RetrievePatientRecords( )
    {
      // retrieve the patient records and bind them to the control
    }

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

    Re: difficulty closing parent form and creating a new one (when child form closes)

    Here's a tip about reducing code clutter when displaying dialogs. This has nothing to do with the posted problem (hopefully, you have an answer from my previous post).

    I like code that uses classes, or forms, or whatever to be clean as possible when calling them.

    For this reason, I'll generally add a static ShowDialog method to the form.

    For example, consider the NewPatientForm form

    Code:
    public partial class NewPatientForm : Form
    {
      public static DialogResult ShowDialog( Form parent )
      {
        NewPatientForm form = new NewPatientForm ( );
        form.Owner = parent;
        return form.ShowDialog( );
      }
      // ....
    }
    This allows us to cleanly invoke the dialog from the EPTForm form:

    Code:
    private void btnNewPatient_Click(object sender, EventArgs e)
    {
      if( DialogResult.Ok == NewPatientForm.ShowDialog( this ) )
      {
        RetrievePatientRecords( );
      }
    }

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured