本文介绍了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

推荐答案

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#委托和事件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 12:12