Click to See Complete Forum and Search --> : difficulty closing parent form and creating a new one (when child form closes)


jonny_chympo
June 16th, 2008, 04:53 PM
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:


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

Arjay
June 16th, 2008, 07:19 PM
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.

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

Arjay
June 16th, 2008, 08:24 PM
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

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:

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