简体   繁体   English

听取从不同winform触发的自定义事件

[英]Listening for a custom event fired from different winform

I am working on a librarian application (in Visual Studio 2013, C#) which allows employers to loan, return and reserve books for customers. 我正在开发一个图书管理员应用程序(在Visual Studio 2013,C#中),它允许雇主为客户贷款,退货和预订图书。 There is a small problem with creating, firing and listening for custom events. 创建,触发和侦听自定义事件存在一个小问题。

There is one form which adds data to a so-called Reservartion table, in my database. 有一种形式可以将数据添加到我的数据库中的所谓Reservartion表中。 It adds a record with current date, itemID and other data. 它添加了包含当前日期,itemID和其他数据的记录。

When the adding of this done, I want to fire a custom event, which will be listened to from a different form. 当添加完成后,我想触发一个自定义事件,该事件将从另一个表单中被监听。 When this event is fired, I need the total data in the Reservation Table to be shown when the event is fired. 触发此事件时,我需要在触发事件时显示预留表中的总数据。

I use an onclick method in my main form, which opens a new form where data will be entered and afterwards, as described above, added into database. 我在我的主窗体中使用了一个onclick方法,它打开一个新的表单,输入数据,之后,如上所述,添加到数据库中。 When the data has been added, the form closes and the main form receives focus again. 添加数据后,表单将关闭,主窗体将再次获得焦点。

The only problem now is, when all this is done, I need the entire data in the Reservartion Table to be shown onscreen (it needs to refresh itself). 现在唯一的问题是,当所有这一切都完成后,我需要在Reservartion表中显示整个数据(它需要刷新自己)。 Which is where I need an event to be raised. 这是我需要举办活动的地方。

How do I create a custom event in one form, which will be listen for INSIDE an onclick method? 如何在一个表单中创建自定义事件,这将监听INSIDE和onclick方法?

I included some of the code for reference. 我提供了一些代码供参考。

This is the code from the main form buttonclick: 这是来自主窗体buttonclick的代码:

private void btnToevoegR_Click(object sender, EventArgs e)
{
    Forms.HandAddReserveer HAR = new Forms.HandAddReserveer();
    HAR.Show();
    // listen for event raised
    // When event is raised do this

    DataTable DT = DBReserveer.getAllReserveerItems();
    gvUitleen.DataSource = DT;

}

And in the other form, where the event should be raised 而在另一种形式,应该提出事件

private void button1_Click(object sender, EventArgs e)
{
    int pasID;
        int itemID;

        if (int.TryParse(tbItemID.Text, out itemID))
        {
            if (int.TryParse(tbPasID.Text, out pasID))
            {
                if ((DBReserveer.ReserveerPasCheck(itemID, pasID)) != 0)
                {
                    MessageBox.Show("Je hebt dit item al gereserveerd!");
                }
                if ((DBReserveer.ReserveerPasCheck(itemID, pasID)) == 0)
                {
                    if (MessageBox.Show("Weet je zeker dat je dit item wilt reserveren?", "Reserveren?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        DBReserveer.ReserveerItem(itemID, pasID);
                        if (DBReserveer.QueryStatus == true)
                        {
                            MessageBox.Show("Het item is gereserveerd!");
                            // Event should be raised from here
                        }
                    }
                }
            }
        }

}

You could subscribe to the FormClosed Event of HAR. 您可以订阅HAR的FormClosed事件。

private void btnToevoegR_Click(object sender, EventArgs e)
{
     Forms.HandAddReserveer HAR = new Forms.HandAddReserveer();
     HAR.FormClosed += new FormClosedEventHandler(HAR_FormClosed);
     HAR.Show();
}

private void Har_FormClosed(Object sender, FormClosedEventArgs e) {
     DataTable DT = DBReserveer.getAllReserveerItems();
     gvUitleen.DataSource = DT;
}

Another way would be to create your on event, like this in the form, that is being closed: 另一种方法是在表单中创建正在关闭的on事件:

public event EventHandler<EventArgs> ReservationComplete;

protected virtual void OnReservationComplete()
{
    EventHandler<EventArgs> handler = this.ReservationComplete;
    if (handler != null)
    {
        handler(this, EventArgs.Empty);
    }
}

Raise the event, by adding a call to OnReservationComplete() 通过添加对OnReservationComplete()的调用来提升事件

  if (DBReserveer.QueryStatus)
  {
     MessageBox.Show("Het item is gereserveerd!");
     this.OnReservationComplete();
      // Event should be raised from here
  }

and listen to the event like this (I'm not sure if the += syntax is correct. I'm writing the code from the top of my head. Feel free to correct): 并听取这样的事件(我不确定+ =语法是否正确。我正在编写我的头顶代码。随意纠正):

private void btnToevoegR_Click(object sender, EventArgs e)
{
     Forms.HandAddReserveer HAR = new Forms.HandAddReserveer();
     HAR.ReservationComplete += Har_ReservationComplete;
     HAR.Show();
}

private void Har_ReservationComplete(Object sender, EventArgs e) {
     DataTable DT = DBReserveer.getAllReserveerItems();
     gvUitleen.DataSource = DT;
}

Edit: Third option. 编辑:第三个选项。 You could display the 2nd form as modal. 您可以将第二种形式显示为模态。

private void btnToevoegR_Click(object sender, EventArgs e)
{
     Forms.HandAddReserveer HAR = new Forms.HandAddReserveer();
     HAR.ShowDialog();
     DataTable DT = DBReserveer.getAllReserveerItems();
     gvUitleen.DataSource = DT;
}

You could do it with an event. 你可以用一个事件做到这一点。 But why so cumbersome? 但为什么这么麻烦呢? Why not simply refreshing the list when reservation succeeded. 为什么不在预订成功时简单刷新列表。 You're already in the correct method for that anyway. 无论如何,你已经采用了正确的方法。

Edit Like this: 编辑像这样:

...        
if (MessageBox.Show("Je hebt dit item al gereserveerd!") == DialogResult.OK)
{
    <Refresh list>
}    
...

Edit 2 编辑2
Seems like I misinterpreted the question a bit. 好像我有点误解了这个问题。 If the list-to-refresh resides in the other form then you would give the second form a reference to it on opening. 如果list-to-refresh位于另一个表单中,那么您将在打开时为第二个表单提供对它的引用。

In the second form, you would declare a property that takes a reference to Form 1: 在第二种形式中,您将声明一个引用Form 1的属性:

public partial class Form2
{
    public Form1 CallingForm { get; set; }
    ...

... set this reference upon opening Form2 ... ...在打开Form2时设置此引用...

private void btnToevoegR_Click(object sender, EventArgs e)
{
    Forms.HandAddReserveer HAR = new Forms.HandAddReserveer();
    HAR.CallingForm = this;
    ...

... finally the second Form will call the Refresh operation on Form1 on closing: ...最后第二个Form将在关闭时调用Form1上的Refresh操作:

if (MessageBox.Show("Je hebt dit item al gereserveerd!") == DialogResult.OK)
{
    CallingForm.<Refresh list>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM