Delegates in C#
-
Hi Guys, Quick question : I have 2 forms : FORM1 and FORM2. In FORM1 i am trying to implemen a PRINT PREVIW button to work like the PRINT PREVIW button in FORM2. Here is the delegate, event Starter() and Subscriber() lines.
public delegate void DelegatPrintPreview(object sender, EventArgs e);
public static event DelegatPrintPreview EvenimentDelegatPrintPreview;public static void StarterPrintPreview(object o, EventArgs ev) { if (EvenimentDelegatPrintPreview != null) { EvenimentDelegatPrintPreview(o, ev); } } public static void SubscribeToPrintPreview(DelegatPrintPreview pf) { EvenimentDelegatPrintPreview += new DelegatPrintPreview(pf); }
the question is : Where do i put the delegate(in the form with the same button or the other one )? and the same question with the Starter() and Subscriber() methods ? Regards, Alex
“Be the change you want to see in the world.”
-
Hi Guys, Quick question : I have 2 forms : FORM1 and FORM2. In FORM1 i am trying to implemen a PRINT PREVIW button to work like the PRINT PREVIW button in FORM2. Here is the delegate, event Starter() and Subscriber() lines.
public delegate void DelegatPrintPreview(object sender, EventArgs e);
public static event DelegatPrintPreview EvenimentDelegatPrintPreview;public static void StarterPrintPreview(object o, EventArgs ev) { if (EvenimentDelegatPrintPreview != null) { EvenimentDelegatPrintPreview(o, ev); } } public static void SubscribeToPrintPreview(DelegatPrintPreview pf) { EvenimentDelegatPrintPreview += new DelegatPrintPreview(pf); }
the question is : Where do i put the delegate(in the form with the same button or the other one )? and the same question with the Starter() and Subscriber() methods ? Regards, Alex
“Be the change you want to see in the world.”
Firstly, you don't need to define that delegate. The EventHandler delegate has the same signature. http://msdn.microsoft.com/en-us/library/system.eventhandler.aspx[^] You'd want to stick your event handler in both forms, and implement each seperately. If they share the same code, put that in a common function and call that from both handlers.
Regards, Rob Philpott.
-
Firstly, you don't need to define that delegate. The EventHandler delegate has the same signature. http://msdn.microsoft.com/en-us/library/system.eventhandler.aspx[^] You'd want to stick your event handler in both forms, and implement each seperately. If they share the same code, put that in a common function and call that from both handlers.
Regards, Rob Philpott.
10x for the advice Rob. My delegate works just as it is. The issue is that Form1 is the first that starts when i run the program . In form 2 i have the button to print ( with the code). To avoid copying the same code in Form1, i wanted to create a delegate. So i created it in Form2 and also subscribed the print preview method to in in Form2. I made it but it only works when i start Fom1 and then start form2 and close it. Then i can use the button in Form1 for print preview. This is because when i use the starter method in Form1 for the button in the Menu Strip i run it like this :
private void printPreviewToolStripMenuItem\_Click(object sender, EventArgs e) { **Form2**.StarterPrintPreview(sender, e); }
So it needs form2 started once in order to run...and i am struggling with this to make it work :( PS:I declared the Methods in Form2 STATIC but i can't see them in form1.i need to call them using FORM2
“Be the change you want to see in the world.”
-
10x for the advice Rob. My delegate works just as it is. The issue is that Form1 is the first that starts when i run the program . In form 2 i have the button to print ( with the code). To avoid copying the same code in Form1, i wanted to create a delegate. So i created it in Form2 and also subscribed the print preview method to in in Form2. I made it but it only works when i start Fom1 and then start form2 and close it. Then i can use the button in Form1 for print preview. This is because when i use the starter method in Form1 for the button in the Menu Strip i run it like this :
private void printPreviewToolStripMenuItem\_Click(object sender, EventArgs e) { **Form2**.StarterPrintPreview(sender, e); }
So it needs form2 started once in order to run...and i am struggling with this to make it work :( PS:I declared the Methods in Form2 STATIC but i can't see them in form1.i need to call them using FORM2
“Be the change you want to see in the world.”
al3xutzu00 wrote:
I declared the Methods in Form2 STATIC but i can't see them in form1
Did you make them public/internal? If the code doesn't logically belong to Form2, how about having a separate static class that has the common code?
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
-
al3xutzu00 wrote:
I declared the Methods in Form2 STATIC but i can't see them in form1
Did you make them public/internal? If the code doesn't logically belong to Form2, how about having a separate static class that has the common code?
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
I'm very much with Senthil's comments above. You really don't want some common functionality in form code. Otherwise, as your experiencing you need to create form2 in order to do print preview in form1. form2 has obviously got massive amounts of GUI stuff in it which you don't need or want. Try and abstract out the preview functionality into another class and call that from both.
Regards, Rob Philpott.