1

I got my first progamming experience at Visual Basic 6.0. So now, I use Visual Basic 2015. And I See some Different at the Code. In Visual Basic 2015

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

So in VB 6.0 I didn't found such a code like "Handles MyBase.Load", what does Handles mean and what is it do?

Blatz
  • 89
  • 3
  • 9
  • 2
    [Events and Event Handling for Visual Basic 6.0 Users](https://msdn.microsoft.com/en-us/library/fw640fe8(v=vs.90).aspx). You probably want to go through [What's New in the Visual Basic Language for Visual Basic 6.0 Users](https://msdn.microsoft.com/en-gb/library/ms172618(v=vs.90).aspx) too. – GSerg Nov 01 '16 at 11:08
  • 5
    Searching the documentation should be your first attempt https://msdn.microsoft.com/en-us/library/6k46st1y.aspx – Steve Nov 01 '16 at 11:08

3 Answers3

2

From a VB6 perspective, it allows you to name your event handler procedure whatever you want. In VB6, you are required to have the format MyControl_someEvent, where MyControl is the name of the control and someEvent is the name of the event being handled. In VB.Net, you can call your event whatever you want. For example, the code you have above:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Could be written thus:

Private Sub HowAboutThemCUBS(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

And it would still fire when the MyBase.Load event got triggered.

You should read the links that the other responders have posted, too. There is more that you ought to know about the differences than just this (for example, this structure allows you to have a single handler that handles more than one type of event, which you couldn't do in VB6).

BobRodes
  • 5,990
  • 2
  • 24
  • 26
1

Handles will listen for the events that follow eg. MyBase.Load, and when one of those events happens, the method will run

A Friend
  • 2,750
  • 2
  • 14
  • 23
  • 1
    It's a bit incorrect saying that it "listens" to the event. What the `Handles` clause does is actually called _subscribing_ to the event. Simply explaining it, an event contains a list of event handlers (delegates). Whenever you subscribe to the event, the event handler (the method using the `Handles` clause, or the method given by the `AddHandler` statement) is added to the list. When the event is raised it will iterate through that list and invoke every handler. – Visual Vincent Nov 01 '16 at 11:33
  • It's actually possible to get the event handler list via reflection: http://stackoverflow.com/questions/660480/determine-list-of-event-handlers-bound-to-event – Visual Vincent Nov 01 '16 at 11:39
  • 1
    I figured it was easier to keep it simple, feel free to edit as you see fit. And that is a useful link! – A Friend Nov 01 '16 at 12:02
  • There's nothing wrong with keeping it simple, and perhaps your explanation is the best for someone who's new to VB.NET, but I felt like pointing out that it's not 100% correct (I didn't know if you knew what's actually going on, for instance). Still though, you gained my upvote for it. :) – Visual Vincent Nov 01 '16 at 12:18
0

Try reading the documentation for Handles it has a good explination about them:

msdn.microsoft.com/en-us/library/6k46st1y.aspx

Talal Abdoh
  • 159
  • 3
  • 19