Hi all.
I am developing an MDI application, but have hit a snag. I have multiple different MDI child forms, that may be present in the MDI parent. Each of the child forms (not the same form) have a SaveData function.
I have a button on the MDI parent. I want this button to run the active child's SaveData function. I have tried to use inheritence on the forms, so that the SaveData function is always present (overridden by each instance). But I still cant get this to work as it should.
Here is what I have (only pasted relevant code):
Here is the form I inherit for the child forms:
public partial class frmTemplate : Form
{
public void SaveData()
{
MessageBox.Show("Function Not Implemented");
}
}
Example of one of the child form:
public partial class frmTest : frmTemplate
{
public void SaveData()
{
MessageBox.Show("Save Clicked " + strFormName );
//At the moment just show text until it works
//This should overide the template function
}
}
Now in the MDI parent (have shown 2 examples to show my trouble):
private void Save()
{
//First Test - IT WORKS... however have to know what frm
frmTest frmTemp = (frmTest)this.ActiveMdiChild;
frmTemp.SaveData();
//I guess this does what it should, but not what I want.
frmTemplate frmTemp2 = (frmTemplate)(this.get);
frmTemp2.SaveData();
}
When I cast the activeMDIChild to the correct form that is the active child, it works, I get a message box with the function overide text. This is OK. However, the problem is I am going to have at least 30 different forms, and hence will need to write out the static cast for each case... this I would like to avoid (feels sloppy):
if (Active form is of type x)
Cast for x type
if (Active form is of type y)
Cast for y type
if (active form is of type z)
Cast for z type
..... and so on.... sloppy.
The second call (casting to the form that is inherited), this just outputs the standard not implemented text. I guess this is as it should. However, I thought that since this function was overidden, it would have called the correct function.
Is there a solution to this problem here? Or is there a better way to acheive what I am after.
Thanks for any help.
I am developing an MDI application, but have hit a snag. I have multiple different MDI child forms, that may be present in the MDI parent. Each of the child forms (not the same form) have a SaveData function.
I have a button on the MDI parent. I want this button to run the active child's SaveData function. I have tried to use inheritence on the forms, so that the SaveData function is always present (overridden by each instance). But I still cant get this to work as it should.
Here is what I have (only pasted relevant code):
Here is the form I inherit for the child forms:
public partial class frmTemplate : Form
{
public void SaveData()
{
MessageBox.Show("Function Not Implemented");
}
}
Example of one of the child form:
public partial class frmTest : frmTemplate
{
public void SaveData()
{
MessageBox.Show("Save Clicked " + strFormName );
//At the moment just show text until it works
//This should overide the template function
}
}
Now in the MDI parent (have shown 2 examples to show my trouble):
private void Save()
{
//First Test - IT WORKS... however have to know what frm
frmTest frmTemp = (frmTest)this.ActiveMdiChild;
frmTemp.SaveData();
//I guess this does what it should, but not what I want.
frmTemplate frmTemp2 = (frmTemplate)(this.get);
frmTemp2.SaveData();
}
When I cast the activeMDIChild to the correct form that is the active child, it works, I get a message box with the function overide text. This is OK. However, the problem is I am going to have at least 30 different forms, and hence will need to write out the static cast for each case... this I would like to avoid (feels sloppy):
if (Active form is of type x)
Cast for x type
if (Active form is of type y)
Cast for y type
if (active form is of type z)
Cast for z type
..... and so on.... sloppy.
The second call (casting to the form that is inherited), this just outputs the standard not implemented text. I guess this is as it should. However, I thought that since this function was overidden, it would have called the correct function.
Is there a solution to this problem here? Or is there a better way to acheive what I am after.
Thanks for any help.