0

我有一个名为的MDIParent表单、MDIChild表单和普通表单form1form1它是从 MDIChild 继承的,并且表单一个有名为 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;
            }
        }
    }
}

我用下面的代码调用加载表单函数[点击新按钮]

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

我的表单正在加载..在保存按钮onClick功能中,我调用了名为 form1 的函数getdata()

public void getdata()
{
    messageBox.show(textBox1.text);
}
4

1 回答 1

2
 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());
            }
        }
    }
于 2013-02-24T07:55:01.957 回答