Good articles on event handling in VB.NET

I've started moving in to more advanced VB and I am presently having a look at event handling. I have tried to find some good resources on Google but event handling in VB.NET doesn't seem to be the stuff that people write about.

On of my main questions is about the difference in event handling between C# and VB. Is it my perception or is event handling in VB a lot more basic

I would appreciate it if anyone could point me in the direction of a good web resource or a book on event handling in VB.

Thanks
jjrdk


Answer this question

Good articles on event handling in VB.NET

  • Lazarus Darkeyes

    The fundamental event handling mechanism between VB.NET and C# are the same.  However there are major syntax differences.  For example VB supports WithEvents declaration.



    Imports System.Windows.Forms
    Class Form1
        Inherits Form

        Friend WithEvents Button1 As Button

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        End Sub
    End Class

     


    This example shows Button1's click event is automatically hooked up with the function.

    You can also manually hook up events in VB by using AddHandler keyword


    Class foo
        Event MyEvent()
    End Class

    Module Module1
        Sub Main()
            Dim x As New foo
            AddHandler x.MyEvent, AddressOf EventHandler
        End Sub

        Sub EventHandler()

        End Sub
    End Module

     



    There are some good articles on event handling in VB.NET as well.  For example check out
    http://www.panopticoncentral.net/archive/2004/08/03/1536.aspx

    Ting



  • Obsidian

    Thanks for the reply. I have managed to create events and their handlers. But one thing that still baffles me is the difference in event handling between C# and VB. In C# you are able to return a value from an event handler, whereas this is not possible in VB (even VB 2005). Why the difference (yes I know they are different languages) But I don't find it uncommon for an event to trigger some kind of update for values. Instead of returning a value from the event handler you must now pass a variable to the event handler using ByRef or use the event handler to update some global value that can be used later on.

    All of this may work well in a closed program, but if it is to be possible for developers to change it it becomes messy (IMHO).

    Quite specifically I am thinking of a web application using the new ICallbackEventHandler interface. In order for this to perform event handling you must hard code the event processing into the RaiseCallbackEvent function. Whereas if VB supported return values for event handlers you could have the RaiseCallbackEvent function trigger an event and return the resulting code to the web page (as is possible in C#).

    jjrdk

  • Naser

    I've been digging into the subject and have stubled across a major obstacle when implementing events into my classses, a serialization problem as explained here.

    My question now is has this been rectified with the final release of VB.NET 2005

    jjrdk

  • Hunter420

    Im not very familiar with VB.NET however in C#, the way to make an event for let's say a menu Click, the way it would be done would be

    this.mnuConnect.Click += new System.EventHandler(this.MnuConnectClick);
     


    just to be clear, "MnuConnectClick" is the void(or Sub) which the program calls when a user clicks the menu item(which is why that is what goes in the EventHandler parameter)


  • Good articles on event handling in VB.NET