Hi Guys,
I am new to c# so please bear with me. I am trying to write a small MDI application and I need help. Here is what I am trying to do.
I have few child forms in my application. Every child form have public function GetStr that returns the values of controls as a string & a button btnLoad.
I want to create a toolbar button on MDI form when user clicks it, it will get string from Active child form executing function GetStr. Also I want to create one more button when clicked, it will execute click action of btnLoad of the active child form. And this code should work for all forms.
I found few samples over the net but those are not working for me. I tried using "using" clause but it didnt work either because it returns value when form is initialized. I am using VS 2008, c#.
Any idea how do I achieve this most efficient way? Sample code is apprciated.
Thank you for you help in advance.
Nitin
zirano lattPosted May 10, 2009, 11:55 PM
Please see below code snippet. Hopefully, it is what you are asking about. Any doubt, please let me know.
public partial class MDIParentForm : Form
{
//tool strip menu click event
private void whoIsActiveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.ActiveMdiChild != null)
{
IChildForm childForm = (IChildForm)this.ActiveMdiChild;
MessageBox.Show(childForm.GetStr());
}
}
}
interface IChildForm
{
string GetStr();
}
public partial class MDIChildForm1 : Form,IChildForm
{
public string GetStr()
{
return "MDIChildForm1";
}
}
public partial class MDIChildForm2: Form,IChildForm
{
public string GetStr()
{
return "MDIChildForm2";
}
}
NitinPosted May 11, 2009, 6:16 PM
NitinPosted May 7, 2009, 7:04 PM
How do I execute click action of Button on MDIChildForm1 from MDI Toolbar when MDIChildForm1 is active?
Thank you so much. Appriciate your help.
zirano lattPosted May 7, 2009, 1:16 AM
to share data among the child mdi forms, create a public property in MDI parent/container form and access that property through out all your child form.
public partial class MDIParentForm : Form
{
public string SharedName { get; set; }
}
public partial class MDIChildForm1 : Form
{
public SetShareName(string str)
{ ((MDIParentForm)MdiParent).SharedName = str;
}
}
public partial class MDIChildForm2: Form
{
public string GetShareName()
{ return ((MDIParentForm)MdiParent).SharedName;
}
}