How to "addhandler" in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joedeene
    Contributor
    • Jul 2008
    • 579

    How to "addhandler" in C#

    Hello all,

    I know it is a simple question but i'm barely experience with C#, and i need a way to find out how to add a handler for when my referenced form is closing...

    In VB.NET it was a simple line of code like this...

    Code:
    AddHandler btn.Click, AddressOf btn_Click
    but in c# I just can't seem to figure it out, I just want it to be something like this, but not in VB.NET, in C#.net

    Code:
    AddHandler form2.closing, AddressOf myform_closing
    Also, if there is some special way I have to call my "sub", as it is in VB.NET, could you please help me out...i've been searching all over but the examples given are much complicated...

    joedeene
  • l034n
    New Member
    • Oct 2008
    • 15

    #2
    To add an event handler, you can write this in C#:
    form2.FormClosi ng += new FormClosingEven tHandler(form2_ FormClosing);

    Note that after writing +=, the intellisense (if you use VS) will tell you that you may press TAB to add the event (which will generate the rest of the line, and then if you accept the default name of your callback function you may just press TAB one more time and you have your callback function.
    For calling it you don't have to do anything, as it's a callback function and will be called by the other party (WinForm in this example) when the event will be raised (FormClosing in the above example).

    NOTE: You may also add the event handler from VS "visually". At the properties window, click at the icon with a thunderbolt (Events) and find the event that you're looking for. Then, just double-click to automatically generate a stub for the function, or pass a name in the box.

    Cheers

    Comment

    • joedeene
      Contributor
      • Jul 2008
      • 579

      #3
      ok thanks much, its a dynamic form so i cant do the visually properties anyway, but the code should work, i knew it was simple but i wasnt sure, thanks again

      joedeene

      Comment

      Working...