C# WinForms从另一个窗体引发事件

C# WinForms从另一个窗体引发事件,c#,winforms,delegates,C#,Winforms,Delegates,我有一个名为MainForm的主窗体和一个名为ChildForm的子窗体 我想填充ChildForm的文本框,并在MainForm_按钮单击我想触发ChildForm_按钮单击事件 儿童形式: 主要表格: }要以另一种形式访问文本框,请执行以下操作: 在子窗体中将文本框的修饰符属性设置为public 在主窗体中,通过子窗体的对象访问文本框 例如: 要以其他形式访问事件,请执行以下操作: 将事件处理功能设置为public 通过表单的对象将null作为参数传递来调用函数 例如: 子窗体 public

我有一个名为MainForm的主窗体和一个名为ChildForm的子窗体 我想填充ChildForm的文本框,并在MainForm_按钮单击我想触发ChildForm_按钮单击事件

儿童形式: 主要表格:
}要以另一种形式访问文本框,请执行以下操作:

在子窗体中将文本框的修饰符属性设置为public

在主窗体中,通过子窗体的对象访问文本框

例如:

要以其他形式访问事件,请执行以下操作:

将事件处理功能设置为public

通过表单的对象将null作为参数传递来调用函数

例如:

子窗体

public partial class ChildForm : Form
{
    public ChildForm()
    {
        InitializeComponent();
        MainForm.OnChildTextChanged += MainForm_OnChildTextChanged;
        MainForm.OnButtonClick += MainForm_OnButtonClick;
        bttn1.Visible = false;
    }

    void MainForm_OnButtonClick(object sender, EventArgs e)
    {
        this.bttn1.PerformClick();
    }

    void MainForm_OnChildTextChanged(string value)
    {
        this.textBox1.Text = value;
    }

    private void bttn1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("I am hide. But shows message");
    }

}

public class Bttn : Button
{
    public new void PerformClick()
    {
        this.OnClick(EventArgs.Empty);
    }
}
创建父窗体

public partial class MainForm : Form
{
    public delegate void OnMyTextChanged(string value);
    public delegate void ButtonClicked(object sender, EventArgs e);

    public static event OnMyTextChanged OnChildTextChanged;
    public static event ButtonClicked OnButtonClick;

    ChildForm frm = new ChildForm();

    public MainForm()
    {
        InitializeComponent();
        frm.Show();
    }

    public void button1_Click(object sender, EventArgs e)
    {
        OnChildTextChanged("this is new value");
        OnButtonClick(sender, e);
    }
}

看起来像是一个非常初级的问题。使用搜索更努力@kyr你为什么不把那部分写在你的问题里?因此,这将很容易理解和回答。为此,您应该使用MVC模型视图控制器或MVP模型视图演示器方法。下面是一个示例:关于MyButton\u Clicknull,null;,不要将事件设置为公共事件,这很糟糕。相反,将MyButton设置为public并调用类似于MyButton.PerformClick的事件;但它只适用于Form_Load事件。非常感谢您的回复。你知道我在哪里可以阅读一些代表的例子吗?我已经用事件更新了答案。如果有用,请标出答案:这正是我需要的。很抱歉,我无法更好地解释。非常感谢。我还想问更多的问题。如果我设置按钮1.Visible=false;然后这个。按钮1。执行单击;不起作用。我不希望按钮1在运行时可见。这是一个自定义要求。所以需要创建一个自定义按钮来解决这个问题。请找到最新的答案
obj.txtBox.Text="MyValue";
obj.MyButton_Click(null, null);
public partial class ChildForm : Form
{
    public ChildForm()
    {
        InitializeComponent();
        MainForm.OnChildTextChanged += MainForm_OnChildTextChanged;
        MainForm.OnButtonClick += MainForm_OnButtonClick;
        bttn1.Visible = false;
    }

    void MainForm_OnButtonClick(object sender, EventArgs e)
    {
        this.bttn1.PerformClick();
    }

    void MainForm_OnChildTextChanged(string value)
    {
        this.textBox1.Text = value;
    }

    private void bttn1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("I am hide. But shows message");
    }

}

public class Bttn : Button
{
    public new void PerformClick()
    {
        this.OnClick(EventArgs.Empty);
    }
}
public partial class MainForm : Form
{
    public delegate void OnMyTextChanged(string value);
    public delegate void ButtonClicked(object sender, EventArgs e);

    public static event OnMyTextChanged OnChildTextChanged;
    public static event ButtonClicked OnButtonClick;

    ChildForm frm = new ChildForm();

    public MainForm()
    {
        InitializeComponent();
        frm.Show();
    }

    public void button1_Click(object sender, EventArgs e)
    {
        OnChildTextChanged("this is new value");
        OnButtonClick(sender, e);
    }
}