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
jskfanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.