我有一个MDIParent窗体、MDIChild窗体和名为form1的普通窗体,form1继承自MDIChild,窗体1具有名为textBox1的文本框,在父窗体中,我有两个按钮:新建和保存,当我单击新子窗体时,应该会加载新建子窗体,当我单击保存时,应该会弹出一个带有textbox1.text值的消息框,问题是消息框弹出时没有textbox1文本值
我使用下面的代码在父窗体中加载子窗体。
public partial class MDIParent1 : Form
{
MdiClient mdi = null;
string fname;
public MDIParent1()
{
InitializeComponent();
foreach (Control c in this.Controls)
{
if (c is MdiClient)
{
mdi = (MdiClient)c;
break;
}
}
}
}我使用下面的代码在new按钮上调用load form函数。
private void ShowNewForm(object sender, EventArgs e)
{
load_form(new Form1());
}加载表单函数是
private void load_form(object form)
{
foreach (Form f in mdi.MdiChildren)
{
f.Close();
}
if (form == null)
return;
((Form)form).MdiParent = this;
((Form)form).Show();
((Form)form).AutoScroll = true;
fname = ((Form)form).Name;
}我的表单是loading..in保存按钮onClick函数,我调用名为getdata()的form1函数
public void getdata()
{
messageBox.show(textBox1.text);
}发布于 2013-02-24 15:55:02
public partial class MDIChild : Form
{
public virtual string GetMessage()
{
return this.Name;
}
}
public class Form2 : MDIChild
{
TextBox textBox1 = new TextBox();
public override string GetMessage()
{
return textBox1.Text;
}
}
public partial class MDIParent1 : Form
{
private MdiClient mdi = null;
private string fname;
private MDIChild currentActiveChild;
public MDIParent1()
{
base.InitializeComponent();
foreach (Control c in this.Controls)
{
if (c is MdiClient)
{
mdi = (MdiClient) c;
break;
}
}
}
private void ShowNewForm(object sender, EventArgs e)
{
currentActiveChild = new Form2();
load_form(currentActiveChild);
}
public void getdata()
{
if (currentActiveChild != null)
{
MessageBox.Show(currentActiveChild.GetMessage());
}
}
}https://stackoverflow.com/questions/15049355
复制相似问题