Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine what type of object is the sender of an event?

Tags:

vb.net

Heres my sub:

Dim onThisTable as String ="Name"

Private Sub skill_mouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.MouseHover, button2.MouseHover, panel1.MouseHover, panel2.MouseHover, pbox1.MouseHover
  descriptionLabel.Text = dbClass.getDescription(sender.Text, onThisTable)
End Sub

Now I wish to give onThisTable a different value depending what the user pass over (panel or a pbox or a button) but I cant find what is the correct way to compare what type it is ...

Private Sub skill_mouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.MouseHover, button2.MouseHover, panel1.MouseHover, panel2.MouseHover, pbox1.MouseHover
  if sender is ( a button )
     onThisTable = "Admin"
  else if sender is ( a panel )
     onThisTable = "dbObject"
  else 
     onThisTable ="Name"
  end if

   descriptionLabel.Text = dbClass.getDescription(sender.Text, onThisTable)
End Sub
like image 847
Thierry Savard Saucier Avatar asked Nov 25 '11 14:11

Thierry Savard Saucier


People also ask

What is the object sender?

Object Sender is a parameter called Sender that contains a reference to the control/object that raised the event.

What is sender As object in VB net?

The sender parameter will reveal which Textbox was clicked. Private Sub FindIt( ByVal sender As System.Object, ByVal e As System.EventArgs. ) Handles TextBox1.

What is Sender in C sharp?

In the general object, the sender is one of the parameters in the C# language, and also, it is used to create the instance of the object, which is raised by the specific events on the application. That event is handled using the Eventhandler mechanism that is mostly handled and responsible for creating the objects.

What is sender As object E as EventArgs in VB net?

Sender is the object that raised the event and e contains the data of the event. All events in . NET contain such arguments. EventArgs is the base class of all event arguments and doesn't say much about the event.


1 Answers

You can use the TypeOf keyword as descibed here (link)

    If TypeOf sender Is Button Then
        onThisTable = "Admin"
    ElseIf TypeOf sender Is System.Windows.Forms.Panel Then
        onThisTable = "dbObject"
    Else
        onThisTable = "Name"
    End If
like image 153
Phil Murray Avatar answered Sep 25 '22 06:09

Phil Murray