传播从一种形式的事件在C#中的另一种形式形式、事件

2023-09-03 02:12:49 作者:匆匆过客

我如何可以单击按钮以某种形式和更新以另一种形式在一个文本框的文本?

How can I click a Button in one form and update text in a TextBox in another form?

推荐答案

如果你正在尝试使用的WinForms,你可以实现你的孩子的形式自定义事件。你可以有一个事件火在你的孩子的形式被点击的按钮时。

If you're attempting to use WinForms, you can implement a custom event in your "child" form. You could have that event fire when the button in your "child" form was clicked.

您父的形式随后将监听事件和处理它自己的文本框的更新。

Your "parent" form would then listen for the event and handle it's own TextBox update.

public class ChildForm : Form
{
    public delegate SomeEventHandler(object sender, EventArgs e);
    public event SomeEventHandler SomeEvent;

    // Your code here
}

public class ParentForm : Form
{
    ChildForm child = new ChildForm();
    child.SomeEvent += new EventHandler(this.HandleSomeEvent);

    public void HandleSomeEvent(object sender, EventArgs e)
    {
        this.someTextBox.Text = "Whatever Text You Want...";
    }
}