在 C# 中将事件从一个表单传播到另一个表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/977326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:42:45  来源:igfitidea点击:

Propagating events from one Form to another Form in C#

c#.neteventsevent-handling

提问by user366312

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

如何单击一种形式的 Button 并以另一种形式更新 TextBox 中的文本?

采纳答案by Justin Niessner

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.

如果您尝试使用 WinForms,则可以在“子”表单中实现自定义事件。单击“子”表单中的按钮时,您可以触发该事件。

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

然后,您的“父”表单将侦听该事件并处理它自己的 TextBox 更新。

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...";
    }
}

回答by Paul Sonier

Roughly; the one form must have a reference to some underlying object holding the text; this object should fire an event on the update of the text; the TextBox in another form should have a delegate subscribing to that event, which will discover that the underlying text has changed; once the TextBox delegate has been informed, the TextBox should query the underlying object for the new value of the text, and update the TextBox with the new text.

大致; 一种形式必须引用一些包含文本的底层对象;这个对象应该在文本更新时触发一个事件;另一种形式的 TextBox 应该有一个订阅该事件的委托,它会发现底层文本已经改变;一旦通知了 TextBox 委托,TextBox 应该查询底层对象以获得文本的新值,并用新文本更新 TextBox。

回答by Thies

Assuming WinForms;

假设 WinForms;

If the textbox is bound to a property of an object, you would implement the INotifyPropertyChanged interface on the object, and notify about the value of the string being changed.

如果文本框绑定到对象的属性,您将在对象上实现 INotifyPropertyChanged 接口,并通知正在更改的字符串的值。

public class MyClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string title;
    public string Title {
      get { return title; } 
      set { 
        if(value != title)
        {
          this.title = value;
          if (this.PropertyChanged != null)
          {
             this.PropertyChanged(this, new PropertyChangedEventArgs("Title"));
          }
       }
  }

With the above, if you bind to the Title property - the update will go through 'automatically' to all forms/textboxes that bind to the object. I would recommend this above sending specific events, as this is the common way of notifying binding of updates to object properties.

有了上面的内容,如果您绑定到 Title 属性 - 更新将“自动”传递到绑定到该对象的所有表单/文本框。我建议在上面发送特定事件,因为这是通知对象属性更新绑定的常用方法。