• <i id='amdMs'><tr id='amdMs'><dt id='amdMs'><q id='amdMs'><span id='amdMs'><b id='amdMs'><form id='amdMs'><ins id='amdMs'></ins><ul id='amdMs'></ul><sub id='amdMs'></sub></form><legend id='amdMs'></legend><bdo id='amdMs'><pre id='amdMs'><center id='amdMs'></center></pre></bdo></b><th id='amdMs'></th></span></q></dt></tr></i><div id='amdMs'><tfoot id='amdMs'></tfoot><dl id='amdMs'><fieldset id='amdMs'></fieldset></dl></div>
      <bdo id='amdMs'></bdo><ul id='amdMs'></ul>

      <small id='amdMs'></small><noframes id='amdMs'>

      <legend id='amdMs'><style id='amdMs'><dir id='amdMs'><q id='amdMs'></q></dir></style></legend>
    1. <tfoot id='amdMs'></tfoot>

      1. 从 C# 中的另一个窗体监听主窗体中的事件

        时间:2023-09-16

          <bdo id='I5klx'></bdo><ul id='I5klx'></ul>
            <tfoot id='I5klx'></tfoot>

            <small id='I5klx'></small><noframes id='I5klx'>

            <i id='I5klx'><tr id='I5klx'><dt id='I5klx'><q id='I5klx'><span id='I5klx'><b id='I5klx'><form id='I5klx'><ins id='I5klx'></ins><ul id='I5klx'></ul><sub id='I5klx'></sub></form><legend id='I5klx'></legend><bdo id='I5klx'><pre id='I5klx'><center id='I5klx'></center></pre></bdo></b><th id='I5klx'></th></span></q></dt></tr></i><div id='I5klx'><tfoot id='I5klx'></tfoot><dl id='I5klx'><fieldset id='I5klx'></fieldset></dl></div>
              <tbody id='I5klx'></tbody>

              <legend id='I5klx'><style id='I5klx'><dir id='I5klx'><q id='I5klx'></q></dir></style></legend>
                  本文介绍了从 C# 中的另一个窗体监听主窗体中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个应用程序,它有一个主窗体并使用事件处理程序来处理传入数据并反映主窗体上各种控件的更改.这很好用.

                  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.

                  我该怎么做?

                  这里有一些示例代码.我想从 the_timer_Tick 事件处理程序中获取信息来更新 SecondaryForm 的每个实例.

                  Here's some sample code. I want to information from the_timer_Tick event handler to update each instance of 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();
                      }
                  }
                  

                  推荐答案

                  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
                    }
                  

                  这篇关于从 C# 中的另一个窗体监听主窗体中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 .NET 中查找 Windows 窗体控件的所有事件处理程序 下一篇:如何从 VB.NET 中的事件中获取实际的 EventHandler 委托实例?

                  相关文章

                  <i id='bySc4'><tr id='bySc4'><dt id='bySc4'><q id='bySc4'><span id='bySc4'><b id='bySc4'><form id='bySc4'><ins id='bySc4'></ins><ul id='bySc4'></ul><sub id='bySc4'></sub></form><legend id='bySc4'></legend><bdo id='bySc4'><pre id='bySc4'><center id='bySc4'></center></pre></bdo></b><th id='bySc4'></th></span></q></dt></tr></i><div id='bySc4'><tfoot id='bySc4'></tfoot><dl id='bySc4'><fieldset id='bySc4'></fieldset></dl></div>

                  <tfoot id='bySc4'></tfoot>

                        <bdo id='bySc4'></bdo><ul id='bySc4'></ul>
                    1. <small id='bySc4'></small><noframes id='bySc4'>

                      <legend id='bySc4'><style id='bySc4'><dir id='bySc4'><q id='bySc4'></q></dir></style></legend>