The Vb.Net Sender and e Event Parameters

Vb.Net Sender and e Event, In VB6, an event subroutine, like Button1_Click, changed into tons much less complicated because the gadget referred to as the subroutine strictly by using name.If a Button1_Click event existed, the system called it. It’s direct and straightforward.

If a Button1_Click event existed, the system called it. It’s direct and straightforward.

But in VB.NET, there are main improvements that make VB.NET SOOPercharged (it truly is “OOP” for Object Oriented Programming).

The “Handles” clause controls whether or not the device calls the subroutine, no longer the call. The sender and e parameters are surpassed to the subroutine. Use of Parameters Let’s take a look at a easy example to see the distinction that parameters make in VB.NET.

Private Sub Button1_Click(

ByVal sender As System.Object,

ByVal e As System.EventArgs

) Handles Button1.Click

‘ Your code goes right here

End Sub

Event subroutines usually receive a “sender” object and a system EventArgs parameter “e”. Because the EventArgs parameter is an item, it supports whatever homes and methods are vital. For instance, the vintage VB6 MouseMove event subroutine used to obtain four parameters:

Button As Integer Shift As Integer X As Single Y As Single When more advanced mice came out with greater buttons, VB6 had a actual trouble supporting them. VB.NET most effective passes one MouseEventArgs parameter however it supports a lot more residences and methods. And every of them are gadgets that assist even extra. For example, the e.Button belongings carries these kinds of homes:

  • Left
  • Middle
  • Right
  • None
  • XButton1
  • XButton2

If someone invents a “trancendental” mouse with a “virtual” button, VB.NET will simplest must replace the .NET Framework to support it and no preceding code will wreck as a end result.

Vb.Net Sender

There are a number of .NET technology that without a doubt depend upon those parameters. For instance, given that your PC commonly most effective has a unmarried display screen to display pictures, your code has to merge the graphics it creates into the identical photo used by Windows. For that motive, a single “graphics” item must be shared. The foremost manner that your code is able to use that “pics” item is to use the e parameter this is handed to the OnPaint occasion with the PaintEventArgs object.

Protected Overrides Sub OnPaint(

ByVal e As System.Windows.Forms.PaintEventArgs)

Dim g As Graphics = e.Graphics

Other Examples What else can you do with those parameters? To illustrate, assume you want to locate whether a string, possibly something you entered right into a Textbox, exists in someone of a collection of different Textboxes when you click on on one. You should code some dozen in reality equal subroutines for each Textbox:

If TextBox42.Text.IndexOf(

SearchString.Text) = -1

Then NotFound.Text =

“Not Found”

But it is loads easier to code simply one and permit it cope with they all. The sender parameter will display which Textbox became clicked.

Private Sub FindIt(

ByVal sender As System.Object,

ByVal e As System.EventArgs

) Handles TextBox1.Enter,

TextBox2.Enter,

. . . And on and on . . .

TextBox42.Enter

Dim myTextbox As TextBox

myTextbox = sender

Dim IndexChar As Integer =

myTextbox.Text.IndexOf(

SearchString.Text)

If IndexChar = -1 Then _

NotFound.Text = “Not Found” _

Else _

NotFound.Text = “Found It!”

End Sub

Recently, a programmer requested me for a higher manner to “delete the line that changed into clicked in any of six distinctive lists.” He had it running in a couple of dozen lines of code that clearly burdened me. But the use of sender, it became simply quite easy:

Private Sub ListBox_Click(

ByVal sender As Object,

Vb.Net Sender and e Event, Event Parameters

ByVal e As System.EventArgs

) Handles ListBox1.Click, ListBox2.Click

Dim myListBox As New ListBox

myListBox = sender

myListBox.Items.RemoveAt(myListBox.SelectedIndex)

End Sub

One more example to nail down the point is a question that turned into despatched in by means of Pierre in Belgium.

Vb.Net Sender and e Event, Pierre changed into trying out the equality of Button1 and sender using the Is operator for gadgets:

If sender Is Button1 Then …

This is syntactically accurate due to the fact sender and Button1 are each gadgets that can be referenced. And for the reason that sender virtually is equal with Button1, why doesn’t it work?

The answer relies upon on a keyword that is observed a little earlier in the declaration. First, permit’s check the Microsoft documentation for the Is operator.

Visual Basic compares two object reference variables with the Is Operator. This operator determines if reference variables consult with the equal object instance.

Notice that sender is exceeded ByVal. That method that a copy of Button1 is exceeded, now not the actual item itself.

Vb.Net Sender and e Event, So when Pierre assessments to look if sender and Button1 are the equal example, the end result is False.

To check whether or not Button1 or Button2 has been clicked, you need to turn sender into an real Button object and then check a assets of that object. Text is typically used, however you may check a cost in Tag or maybe the Location assets.

This code works:

Dim myButton As Button

myButton = sender

If myButton.Text = “Button1” Then