如何确定事件发件人的对象类型?

时间:2011-11-25 14:53:38

标签: vb.net

继承我的潜意识:

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

现在我想根据用户传递的内容(面板或pbox或按钮)给onThisTable一个不同的值,但是我找不到比较它的类型的正确方法...

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

1 个答案:

答案 0 :(得分:21)

您可以在此处使用TypeOf关键字(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