单击来自form1的按钮时从form2调用按钮

问题描述 投票:-1回答:2

这是一个演示示例,我无法使用原始代码。此上下文非常简化。当我从母窗体(form1)单击按钮时,我想从子窗体(form2)调用按钮/事件。我想通过订阅来做到这一点(因为我是编码的新手,所以我并没有真正的担心)。

母亲形式:

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

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.Show();
            //Call the Button from Form2 here
        }


    }

子表格:

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

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("MessageBox called from Form1 or Form2");
        }
    }

这是一个演示示例,我无法使用原始代码。此上下文非常简化。我想从子窗体(form1)中单击按钮/事件时从子窗体(form1)中调用按钮/事件...

c# .net windows forms
2个回答
0
投票

不要调用其他形式的“按钮单击”,如果要从不同的位置调用某些逻辑,请将逻辑提取到专用的类中并从两个位置都调用它]]

public class MyLogic
{
    public void Execute(string someParameter)
    {
        // Do something with parameter
    }
}

0
投票

[您想做的事情,您想做的事情一般来说不是一个好主意,因为您不应该依赖另一个表单中的另一个UI控件。

© www.soinside.com 2019 - 2024. All rights reserved.