在 C# 中从另一个表单监听主表单中的事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/610313/
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
Listening to Events in Main Form from Another Form in C#
提问by Dave
I have an application that has a main form and uses an event handler to process incoming data and reflect the changes in various controls on the main form. This works fine.
我有一个应用程序,它有一个主窗体并使用事件处理程序来处理传入的数据并反映主窗体上各种控件的更改。这工作正常。
I also have another form in the application. There can be multiple instances of this second form running at any given time.
我在申请中还有另一种形式。可以在任何给定时间运行第二种形式的多个实例。
What I'd like to do is have each instance of this second form listen to the event handler in the main form and update controls on its instance of the second form.
我想做的是让第二个表单的每个实例侦听主表单中的事件处理程序并更新其第二个表单实例上的控件。
How would I do this?
我该怎么做?
Here's some sample code. I want to information from the_timer_Tick event handler to update each instance of SecondaryForm.
这是一些示例代码。我想从 the_timer_Tick 事件处理程序中获取信息来更新 SecondaryForm 的每个实例。
public partial class Form1 : Form
{
Timer the_timer = new Timer();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
the_timer.Tick += new EventHandler(the_timer_Tick);
the_timer.Interval = 2000;
the_timer.Enabled = true;
}
void the_timer_Tick(object sender, EventArgs e)
{
// I would like code in here to update all instances of SecondaryForm
// that happen to be open now.
MessageBox.Show("Timer ticked");
}
private void stop_timer_button_Click(object sender, EventArgs e)
{
the_timer.Enabled = false;
}
private void start_form_button_Click(object sender, EventArgs e)
{
SecondaryForm new_form = new SecondaryForm();
new_form.Show();
}
}
采纳答案by abatishchev
class SecondForm
{
private FirstForm firstForm;
public SecondForm()
{
InitializeComponent();
// this means unregistering on form closing, uncomment if is necessary (anonymous delegate)
//this.Form_Closing += delegate { firstForm.SomeEvent -= SecondForm_SomeMethod; };
}
public SecondaryForm(FirstForm form) : this()
{
this.firstForm = form;
firstForm.Timer.Tick += new EventHandler(Timer_Tick);
}
// make it public in case of external event handlers registration
private void Timer_Tick(object sender, EventArgs e)
{
// now you can access firstForm or it's timer here
}
}
class FirstForm
{
public Timer Timer
{
get
{
return this.the_timerl
}
}
public FirstForm()
{
InitializeComponent();
}
private void Button_Click(object sender, EventArgs e)
{
new SecondForm(this).ShowDialog(); // in case of internal event handlers registration (in constructor)
// or
SecondForm secondForm = new SecondForm(this);
the_timer.Tick += new EventHandler(secondForm.Timer_tick); // that method must be public
}
回答by Kjetil Watnedal
I guess you can make SecondaryForm take in the parent form in the constructor, and the add an event handler in the constructor.
我想你可以让 SecondaryForm 在构造函数中接受父表单,并在构造函数中添加一个事件处理程序。
private void start_form_button_Click(object sender, EventArgs e)
{
SecondaryForm new_form = new SecondaryForm(this);
new_form.Show();
}
In SecondaryForm.cs:
在 SecondaryForm.cs 中:
public SecondaryForm(ISomeView parentView)
{
parentView.SomeEvent += .....
}
回答by Michael Meadows
Consider using loosely coupled events. This will allow you to couple the classes in such a way that they never have to be directly aware of each other. The Unity application block comes with an extension called EventBrokerthat makes this very simple.
考虑使用松散耦合的事件。这将允许您以这样的方式耦合类,它们永远不必直接相互了解。Unity 应用程序块带有一个名为EventBroker的扩展,这使得这非常简单。
Here's a little lick of the sugar:
这里有一点糖:
public static class EVENTS
{
public const string UPDATE_TICKED = "event://Form1/Ticked";
}
public partial class Form1 : Form
{
[Publishes(EVENTS.UPDATE_TICKED)]
public event EventHandler Ticked;
void the_timer_Tick(object sender, EventArgs e)
{
// I would like code in here to update all instances of SecondaryForm
// that happen to be open now.
MessageBox.Show("Timer ticked");
OnTicked();
}
protected virtual void OnTicked()
{
if (Ticked == null) return;
Ticked(this, e);
}
}
public partial class SecondaryForm : Form
{
[SubscribesTo(EVENTS.UPDATE_TICKED)]
private void Form1_Ticked(object sender, EventHandler e)
{
// code to handle tick in SecondaryForm
}
}
Now if you construct both of these classes using Unity, they will automatically be wired together.
现在,如果您使用 Unity 构建这两个类,它们将自动连接在一起。
Update
更新
Newer solutions use message bus to handle loosely coupled events. See http://masstransit-project.com/or http://nimbusapi.github.io/as examples.
较新的解决方案使用消息总线来处理松散耦合的事件。请参阅http://masstransit-project.com/或http://nimbusapi.github.io/作为示例。