C#委托和事件问题 [英] C# delegate and events problem

查看:79
本文介绍了C#委托和事件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在项目中实现委托和事件时遇到问题.在这里,我给你我的问题的情况.


我有一个用户控件scanTicket.我在ApplicationScan类中创建scanTicket的实例.当我在applicationScan中单击button1时,它将显示scanticket.在做一些后台工作后,如果我单击scanticket中的确定"按钮,则必须关闭它并在应用程序中触发事件可以执行一些处理.这是我的问题.

你能帮我吗
任何人都可以帮助我解决我的问题.

问候
S. Keshaavan

Hi

I have problem in implementing delegates and events in my projects. Here with i am giving you the scenario of my problem.


I have a usercontrol scanTicket. I am creating instance of scanTicket in ApplicationScan class. When i click button1 in applicationScan it will show scanticket. After doing some background work if i click ok button in scanticket it has to close this and fire an event in applicationscan to do some process. This is my problem.

Could you help me please
Can any one help me to solve my problem please.

Regards
S. Keshaavan

推荐答案

从您的问题和对SAKyukov的评论中,您想要的事件顺序似乎是:
ApplicationScan类(我假设它是基于Form的)具有一个按钮.单击它后,您将创建一个scanTicket实例(再次基于Form),该实例对数据进行一些处理.然后,用户可以按scanTicket表单中的按钮,该按钮将向ApplicationScan实例发出事件信号,然后关闭scanTicket实例.

有三种查看方法:最简单的情况是让ApplicationScan使用ShowDialog方法显示scanTicket.这将冻结" ApplicationScan,直到关闭scanTicket实例.

如果您不能使用ShowDialog,而必须使用Show方法,那么您将不得不加入scanTicket事件.

下一个最简单的方法是将ApplicationScan挂接到scanTicket.FormClosing事件中. http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing.aspx [ ^ ]然后,它可以在完全关闭之前从scanTicket实例访问所需的任何信息.除了参与事件之外,您无需进行任何其他工作:
From your question and the comment you made to SAKyukov, the sequence of events you want appears to be:
ApplicationScan class (which I assume is Form based) has a button. When it is clicked, you create an instance of scanTicket (again, Form based) which does some work on the data. The user can then press a button in the scanTicket form, which will signal an event to the ApplicationScan instance, and close the scanTicket instance.

There are three ways to look at this: The simplest case is to have ApplicationScan display teh scanTicket with the ShowDialog method. This will "freeze" ApplicationScan until the scanTicket instance is closed.

If you can''t use ShowDialog, but must use the Show method instead, then you will have to hook into the scanTicket events.

The next simplest is to have ApplicationScan hook into the scanTicket.FormClosing event. http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing.aspx[^] It can then access whatever information it needed from the scanTicket instance before it closes completely. THis requires no extra work on your part, other than to hook into an event:
scanTicket sc = new scanTicket();
sc.FormClosing += new EventHandler(scanTicket_Closing);
sc.Show();
...

void scanTicket_Closing(object sender, EventArgs e)
    {
    ...
    }



如果您需要一个单独的事件,因为还有另一种方法可以关闭scanTicket而不向ApplicationScan发出信号,那么最简单的方法是:在scan Ticket中创建一个事件,ApplicationScan可以将其挂接到:



If you need a separate event, because there is another way to close scanTicket without signalling to ApplicationScan, then the easiest way is to do just that: create an event in scan Ticket, which ApplicationScan hooks into:

scanTicket sc = new scanTicket();
sc.WorkCompleted += new EventHandler(scanTicket_Completed);
sc.Show();
...

void scanTicket_Completed(object sender, EventArgs e)
    {
    scanTicket sc = sender as scanTicket;
    if (sc != null)
        {
        ...
        }
    }



并在scanTicket中:



And in scanTicket:

public event EventHandler WorkCompleted;
protected virtual void OnWorkCompleted(EventArgs e)
   {
   EventHandler eh = WorkCompleted;
   if (eh != null)
      {
      eh(this, e);
      }
   }
private void DoSomethingToChangeData()
   {
   OnWorkCompleted(null);
   }
}



感谢您的详细回答.我仍然有问题.启动应用程序时,一次创建多个表单的scanticket实例.

例子
1. ManaualEnter
2.条码
3. Applicationscan
4. Pluscode

这四种形式均以相同的顺序一一创建实例.在pluscode创建实例之后,应用程序可以将事件scanTicket_Completed变为null.我在运行应用程序时正在创建实例.因此,当我单击scanticket中的按钮时,scanTicket_Completed事件显示为null.

请问你能帮帮我吗.在此先感谢

问候
Keshaavan-NK7 4分钟前"


如果要使用每种表单创建一个新实例,则新实例将不会与其他实例共享任何信息-这包括事件.如果要共享信息,为什么每次都要创建一个新实例?为什么不只有一个实例,您的所有其他四种形式都连接到该实例?



"Thanks for your detail answer. Still i have problem. scanticket instance is created more than one form at a time while launching the application.

Example
1. ManaualEnter
2. BarCode
3. Applicationscan
4. Pluscode

All four form creating the instance in the same order one by one. After the pluscode create a instance applicationscan event scanTicket_Completed become null. I am creating instance while running the application. So when i click the button in scanticket scanTicket_Completed event shows null.

Could you help me please. Thanks in advance

Regards
Keshaavan - NK7 4 mins ago"


If you are creating a new instance with each form, then the new instance will not share any of the information with the others - this includes events. Why are you creating a new instance each time, if you want to share info? Why not have just one instance, which all four of your other forms connect to?


这篇关于C#委托和事件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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