winform 子窗体调用主窗体的方法

1.主窗体

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.refreshlist += Frm2_refreshlist;
            frm2.ShowDialog();
        }

        private void Frm2_refreshlist()
        {
            MessageBox.Show("列表刷新了");
        }
    }

2.子窗体 

    public partial class Form2 : Form
    {
        public event Action refreshlist;
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
            this.Dispose();
            if (refreshlist != null)
            {
                refreshlist();
            }
        }
    }

 

上一篇:C#.Net 判断窗体是否创建并激活


下一篇:winform窗体之间传递参数