Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 13-Jan-11 20:07pm
v2
Comments
OriginalGriff 14-Jan-11 4:37am    
Answer updated (I forgot to add this when I changed it... blame a lack of caffeine, I do!

1 solution

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)
    {
    ...
    }


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)
        {
        ...
        }
    }


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


"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?
 
Share this answer
 
v2
Comments
NK7 14-Jan-11 3:58am    
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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900