How do I determine which button was clicked

  • Thread starter Thread starter dbuchanan
  • Start date Start date
D

dbuchanan

I have added handlers handlers to direct all buttons to one method like
this;

'Add handlers for buttons
AddHandler btnManageJobs.Click, AddressOf Button_Click
AddHandler btnSeqOfOper.Click, AddressOf Button_Click
AddHandler btnViewer.Click, AddressOf Button_Click
AddHandler btnScfmCalculationTool.Click, AddressOf Button_Click
AddHandler btnAdminSetup.Click, AddressOf Button_Click

How do I determine which button was clicked? I think I use "sender" but
do not know how. Can someone help me?

Thank you,
dbuchanan
 
If you need to access properties specific to the Button class then you can
cast the "sender" to the Button type using DirectCast.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vakeydirectcast.asp

To compare the "sender" to the buttons to see which Button was clicked you
can do something similar to the following code.

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
If (sender Is Button1) Then
' Handle Button1.Click
ElseIf (sender Is Button2) Then
' Handle Button2.Click
ElseIf (sender Is Button3) Then
' Handle Button3.Click
End If
End Sub
 
dbuchanan said:
I have added handlers handlers to direct all buttons to one method like
this;

'Add handlers for buttons
AddHandler btnManageJobs.Click, AddressOf Button_Click
AddHandler btnSeqOfOper.Click, AddressOf Button_Click
AddHandler btnViewer.Click, AddressOf Button_Click
AddHandler btnScfmCalculationTool.Click, AddressOf Button_Click
AddHandler btnAdminSetup.Click, AddressOf Button_Click

How do I determine which button was clicked? I think I use "sender" but
do not know how. Can someone help me?


\\\
Dim SourceControl As Button = DirectCast(sender, Button)
Select Case True
Case SourceControl Is btnManageJobs
...
Case...
...
End Select
///
 
dbuchanan said:
I have added handlers handlers to direct all buttons to one method like
this;

'Add handlers for buttons
AddHandler btnManageJobs.Click, AddressOf Button_Click
AddHandler btnSeqOfOper.Click, AddressOf Button_Click
AddHandler btnViewer.Click, AddressOf Button_Click
AddHandler btnScfmCalculationTool.Click, AddressOf Button_Click
AddHandler btnAdminSetup.Click, AddressOf Button_Click

How do I determine which button was clicked? I think I use "sender" but
do not know how. Can someone help me?

Thank you,
dbuchanan

In the click event handler...

name = CType(sender, Button).Name
Select Case name
Case "btnManageJobs"
'ManageBlah()
Case "btnSeqOfOper"
'GetSeq
Case "btnViewer"
'GetViewer
Case "btnScfmCalculationTool"
'GetTool
Case Else
'handle it
End Select

A better way might be to use a module-level hashtable with button names and
indexes.

Carl
 
Carl,

I've never used a hashtable. Do you have an example you could share.

Thank you,
dbuchanan
 
I would prefer not to check on the control's name property, since a change
from the control's name will result in a breaking change that will not be
easily detected.

Tim's solution is less error-prone.
 
Hi Jelle,

How is Tim not using the controls name property? How is Button1,
Button2.... not the button's name? It looks like it to me.


Thank you,
dbuchanan
 
If it was in quotes it would be. I am comparing references, not names. And I
like Herfried's way the best since it seems cleaner than a whole bunch of
if...elses. I'm not a VB guy so I wasn't sure if you could compare
references through a Select statement.
 
Hi Tim, Jelle, others

Thanks for the input I understand what you mean by references - if the
button name changes the code is broken, but the compiler will indicate
where the break is the Task List.

Herfried's code is the one I chose for the same reasons Tim stated.


Thanks,
dbuchanan
 
Back
Top