简体   繁体   English

从MDI父控件访问MDI子控件

[英]Access MDI child control from MDI parent control

I have a MDIParent form, MDIChild form and normal form called form1 , form1 is inherited from MDIChild and,form one has textbox called textBox1,in Parent form i have two buttons New and Save, when i click New child form should be loaded and when I click save a message box should be pop-up with textbox1.text value ,the problem is message box is pop-up with-out textbox1 text value 我有一个MDIParent表单, MDIChild表单和普通表单名为form1form1继承自MDIChild,表单一个有文本框,名为textBox1,在父表单中我有两个按钮New和Save,当我点击New child form时应加载,当我点击保存一个消息框应弹出带有textbox1.text值,问题是消息框是弹出的带有textbox1文本值

im using bellow code to load child form inside the parent form. 我使用下面的代码来加载父窗体内的子窗体。

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

and i use to call to the load form function using bellow code[clicking on new button] 我使用波纹管代码[点击新按钮]来调用加载表单功能

private void ShowNewForm(object sender, EventArgs e)
{
    load_form(new Form1());
}

load form function is 加载表单功能是

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

and my form is loading..in save button onClick function , i call to the form1 function called getdata() 并且我的表单正在getdata()在保存按钮onClick函数中,我调用了名为getdata()的form1函数

public void getdata()
{
    messageBox.show(textBox1.text);
}
 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());
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM