• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8565
  • Last Modified:

WithEvent in VB.net

Can someone give me a simple and basic  of what Withevent does?

I am very new to VB.net and I only have a background in DbaseIII+ programming and a bit of VBA.

I appreciate your help.

thanks
0
jskfan
Asked:
jskfan
  • 3
4 Solutions
 
aseusaincCommented:
Declaring a control variable WithEvents allows "automatic" wireup of event handlers with the controls in VB.NET.  In other words, when a control (for example, a command button) is declared WithEvents, double-clicking the control in the designer will generate the standard VB6-style EventHandler function (such as Command1_Click).  Note, however, that, besides adding the .NET arguments (source as objects, e as System.EventArgs), at the very end of the function definition you'll see a clause such as Handles Command1.Click.
For 99% of your windows and web form applications, this is fine (although any controls whose events are not of interest should be declared without this specifier, to save on memory).  The downside to it is that it is old-style coding -- one handler per function, and you no longer have control arrays to play with.
The alternative is not to declare the controls WithEvents, but use the AddHandler method to add an event handler at runtime.  This way you can "funnel" multiple controls to the same event handler, simulating the behavior of a control array.
Controls declared WithEvents can still be handled by attaching a handler using AddHandler.
Also, by default, VB.NET controls are generated as WithEvents, so keep that in mind.
Hope this helps.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
When you declare a variable using WithEvents, it allows you to automatically generate code for that controls events using the two DropDown boxes at the top of your code editor.

You can see these two DropDown boxes clearly in this picture:
http://www.bytescout.com/images/examples/vbnet_source_editor_window.jpg

The first dropdown box says "(Form1 Events)" while the second says "Load".

If you were to add this line of code to your Form:

    Private WithEvents btn As Button

Then you could select "btn" in the first dropdown box and see that all the events for "btn" would appear in the second dropdown box.  If you then selected "MouseDown" in the second dropdown, the code for the MouseDown() event of "btn" would be added to your code:

    Private Sub btn_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btn.MouseDown

    End Sub
0
 
jskfanAuthor Commented:
If I understand when you declare variable using WithEvents as your example: Private WithEvents btn As Button

it's just like you dragged and dropped a button control to your form, it inherits all events that a button control has.

I wonder in what circumstances you would need a control variable seeing that noone would go and click on it to raise the event.







0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Not all controls that raise events are GUI components that the user interacts with.  One simple example is the Timer control.  You can declare one in code like so:

    Private WithEvents tmr As System.Windows.Forms.Timer

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        tmr = New System.Windows.Forms.Timer
        tmr.Interval = 1000
        tmr.Start()
    End Sub

    Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
        Me.Text = DateTime.Now
    End Sub

The NotifyIcon control is another good example.  There are many more like this...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
As aseusainc pointed out, it is common to use AddHandler to achieve the same end goal:

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim tmr As New System.Windows.Forms.Timer
        AddHandler tmr.Tick, AddressOf Me.tmr_Tick
        tmr.Interval = 1000
        tmr.Start()
    End Sub

    Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.Text = DateTime.Now
    End Sub
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now