我如何从mdi父表单调用活动mdichild表单的方法

时间:2016-04-16 11:09:25

标签: c#

我有mdi父级分区容器控件。我分成两个小组。

面板1包含子窗体,面板2包含一些按钮,如SAVE,DELETE和UPDATE。

Panel 1可以加载一些Child表单。 我想在单击SAVE按钮时调用面板1中活动子窗体的方法。

2 个答案:

答案 0 :(得分:1)

您必须传递表单的实例才能在其他类中使用。请参阅下面的两个表单项目:

表格1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Form2 form2;
        public Form1()
        {
            InitializeComponent();
            form2 = new Form2(this);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            form2.Show();
            string  results = form2.GetData();
        }
    }
}

表格2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        Form1 form1;
        public Form2(Form1 nform1)
        {
            InitializeComponent();

            this.FormClosing +=  new FormClosingEventHandler(Form2_FormClosing);
            form1 = nform1;
            form1.Hide();
        }
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            //stops form from closing
            e.Cancel = true;
            this.Hide();
        }
        public string GetData()
        {
            return "The quick brown fox jumped over the lazy dog";
        }

    }
}

答案 1 :(得分:0)

您可以使用ActiveMdiChild属性来获取表单的活跃子级。

相关问题