How to get control identification from (Sender as Object)

  • Thread starter Thread starter Marty
  • Start date Start date
M

Marty

Hi,

I have this event that is performed by three different controls, how can
I know (from sender object) wich control is used to trigger the event?

Private Sub HighlightSelectedRow(ByVal sender As Object, ByVal e As
System.EventArgs) Handles tbxAmount.Click, lblID.Click, lblExpense.Click

Dim ctlControl As System.Type
ctlControl = sender.GetType

'???
Select Case ctlControl
'???
End Select

End Sub


Thanks

Marty
 
You can use TypeOf and CType.

If TypeOf sender Is Label Then
Dim lbl As Label = CType(sender, Label)
Select Case lbl.Name
Case Is = "lblID"
' do something
Case Is = "lblExpense"
' do something
End Select
ElseIf TypeOf sender Is TextBox Then
Dim txt As TextBox = CType(sender, TextBox)
' do something
End If
 
Wonderful, thanks you!

Marty
You can use TypeOf and CType.

If TypeOf sender Is Label Then
Dim lbl As Label = CType(sender, Label)
Select Case lbl.Name
Case Is = "lblID"
' do something
Case Is = "lblExpense"
' do something
End Select
ElseIf TypeOf sender Is TextBox Then
Dim txt As TextBox = CType(sender, TextBox)
' do something
End If
 
Marty,


In addition to Jared,

Keep in mind that you do not have to know the type when the member is
inheritted from control.

They have by instance all name, text, click etc.

Cor
 
Back
Top