从C#中的另一种形式听在主窗体活动 [英] Listening to Events in Main Form from Another Form in C#

查看:171
本文介绍了从C#中的另一种形式听在主窗体活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了一种主要形式,并使用一个事件处理程序来处理输入数据,并反映在主窗体上的变化,各种控制的应用程序。这工作得很好。

我也有应用程序中的另一种形式。可以有这种第二种形式运行的多个实例,在任何给定的时间。

我想要做的是有这种第二种形式的每个实例听在第二种形式它的实例的主要形式和更新控制事件处理程序。

我将如何做到这一点?

下面是一些示例code。我想从the_timer_Tick事件处理信息,以更新SecondaryForm的每个实例

 公共部分类Form1中:形态
{
    定时器the_timer =新的Timer();
    公共Form1中()
    {
        的InitializeComponent();
    }

    私人无效的button1_Click(对象发件人,EventArgs的)
    {
        the_timer.Tick + =新的EventHandler(the_timer_Tick);
        the_timer.Interval = 2000;
        the_timer.Enabled = TRUE;
    }

    无效the_timer_Tick(对象发件人,EventArgs的)
    {
        //我想code在这里更新SecondaryForm的所有实例
        //这种情况发生,现在是开放的。
        的MessageBox.show(计时器滴答);
    }

    私人无效stop_timer_button_Click(对象发件人,EventArgs的)
    {
        the_timer.Enabled = FALSE;
    }

    私人无效start_form_button_Click(对象发件人,EventArgs的)
    {
        SecondaryForm new_form =新SecondaryForm();
        new_form.Show();
    }
}
 

解决方案

 类Sec​​ondForm
{
  私人FirstForm firstForm;

  公共SecondForm()
  {
    的InitializeComponent();
    //这意味着对形式的收盘注销,取消,如果是必要的(匿名委托)
    //this.Form_Closing + =委托{firstForm.SomeEvent  -  = SecondForm_SomeMethod; };
  }

  公共SecondaryForm(FirstForm形式):这个()
  {
    this.firstForm =形式;
    firstForm.Timer.Tick + =新的EventHandler(Timer_Tick);
  }

  //把它公开的情况下,外部事件处理程序登记
  私人无效Timer_Tick(对象发件人,EventArgs的)
  {
    //现在你可以访问firstForm或者它的定时器这里
  }
}

类FirstForm
{
  公共计时器计时器
  {
    得到
    {
      返回this.the_timerl
    }
  }

  公共FirstForm()
  {
    的InitializeComponent();
  }

  私人无效Button_Click(对象发件人,EventArgs的)
  {
    新SecondForm(本).ShowDialog(); //如果内部事件处理程序注册(在构造函数)
    // 要么
    SecondForm secondForm =新SecondForm(本);
    the_timer.Tick + =新的EventHandler(secondForm.Timer_tick); //这个方法必须是public
  }
 

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.

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#中的另一种形式听在主窗体活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆