Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Delegates in C#

Delegates in C#

Scheduled Pinned Locked Moved C#
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    al3xutzu00
    wrote on last edited by
    #1

    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.”

    R 1 Reply Last reply
    0
    • A al3xutzu00

      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.”

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • R 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.

        A Offline
        A Offline
        al3xutzu00
        wrote on last edited by
        #3

        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.”

        S 1 Reply Last reply
        0
        • A al3xutzu00

          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.”

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          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

          R 1 Reply Last reply
          0
          • S S Senthil Kumar

            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

            R Offline
            R Offline
            Rob Philpott
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups