Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MDIParent calling a MDIChild event

Status
Not open for further replies.

sicohne

Technical User
Jun 30, 2003
51
GB
I have a toolbar in my parent form with 2 buttons. One button opens a new child form, the other saves the data in the child form to a database. The trouble is, the "save" event is stored in the child form. If I declare the child form in the "new" function, the save button cannot access the save function. If I declare the child form with the other variables at the beginning, the "new" function causes an error if all child forms are closed (Cannot access a disposed object named "ChildFrm").

How do I get round this?

Sorry if it's badly explained.
 
Use a delegate.

What you'd do is raise an event when the user selects to save their work. Anyone who is interested in seeing that event can then respond. So, if you have a child form, it would register itself as being interested in seeing those events by adding itself to the handler list for it.

This solves the case where the child form has been unloaded (because the reference to the delegate handler has been removed), but you will have to add a check to make sure the child form is the active form before beginning to actually do the save.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Sorry, i'm new to c# and at the mo only have the internet as a guide (books on order).

If I post the current code, can someone help with delegates as I have not used them yet.
Code:
public class Form1 : System.Windows.Forms.Form
{
....
FrmNew frm3 = new FrmNew();

public Form1()
...
[STAThread]
static void Main() 
{
Application.Run(new Form1());
}

public void newapp()
{
Global.childCount++; //increment number of children created this session
Global.childNumber++; //increment number of children currently open
if (Global.childNumber == 1)
{
toolBarButton2.Visible = true;
toolBarButton3.Visible = true;
}
						.
String formText = "New Application " + Global.childCount; // Set the text of the child form using the count of child forms
frm3.Text = formText;
frm3.MdiParent = this; //Make the new form a child form.
frm3.Show(); // Display the child form.
}

private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
switch (toolBar1.Buttons.IndexOf(e.Button))
{
...
case 2:
frm3.save();
break;
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top