Detect button click-event in groupbox

  • Thread starter Thread starter BenCoo
  • Start date Start date
B

BenCoo

Hello everyone,

I've a form with a groupbox witch contain 4 buttons. I searching for a
solutions to detect when I click on one of the buttons returning a reference
to the button thas has been pressed.

Thanks in advance,

Benny
 
Hi Ben,

You mean something as in the click event of the button
Button1.text = "I have been clicked"

You can set for every button whatever you want or are you maybe talking bout
the toolbar?

Cor
 
BenCoo said:
I've a form with a groupbox witch contain 4 buttons. I searching for a
solutions to detect when I click on one of the buttons returning a
reference to the button thas has been pressed.

Add a handler to the button's 'Click' event (this can be done by
doubleclicking the button in the Windows Forms designer). You can cast the
parameter 'sender' to 'Button' if you need a reference to the button that
raised the event, for example.
 
The meaining of the buttons in the groupbox is to calculate a sum. Each
button has a text "10", "20", "30", and so on...In stead of use each buttons
click-event to calculate : sum += 10, sum+= 20, I want to use something
like this:

Select Case ButtonPressed
case "Button10"
sum += 10
case "Button20"
sum += 20
case "Button30"
sum += 30
End Select
 
BenCoo said:
The meaining of the buttons in the groupbox is to calculate a sum. Each
button has a text "10", "20", "30", and so on...In stead of use each buttons
click-event to calculate : sum += 10, sum+= 20, I want to use something
like this:

Select Case ButtonPressed
case "Button10"
sum += 10
case "Button20"
sum += 20
case "Button30"
sum += 30
End Select


You can declare one routine to handle multiple events, such as:


Private Sub ButtonClicks(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button10.Click, Button20.Click, Button30.Click
If sender Is Button10 Then
sum += 10
ElseIf sender Is Button20 Then
sum += 20
ElseIf sender Is Button30 Then
sum += 30
End If
End Sub


HTH
LFS
 
Thanks Larry !! You've put me on the right track !!

I use you're solution, but in the Tag-propertie of each button I place the
value (Button10.Tag = 10, Button20.Tag = 20, and so on) and then I've only
to add the next code to the eventhandler: sum += sender.Tag

Life can be simple ;-)

Again, Thanks a lot
Benny
 
Back
Top