same sub for different buttons

NewB4a

Member
Licensed User
Longtime User
I want to change the text of some buttons like btn1,btn2,bt3,...( 'btn' & nr) by the same sub.

sub changeText(nr)
' changes the Text of Button btnnr
end sub

How to pass the number or anything else that defines the chosen button to the sub and change its text? Is there another simple way than using
select case(nr=..) ?
 

NewB4a

Member
Licensed User
Longtime User
The buttons are defined in Designer. Name and eventname can be anything if this should be useful.
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
The buttons are defined in Designer. Name and eventname can be anything if this should be useful.

This are not REALLY more informations ;)
 
Upvote 0

devlei

Active Member
Licensed User
Longtime User
Is this what you are trying to do?

B4X:
changeText(btn1, "Btn1's new text")

Sub changeText(RelevantButton As Button, NewText As String)
   RelevantButton.Text = NewText     
End Sub
 
Upvote 0

chuck3e

Active Member
Licensed User
Longtime User
NewB4a,

Here's what I've done:

Give all buttons the same event name (xxxxxx)

Give each button a different tag

sub xxxxxx_click

Dim btnSender As Button
Dim btnValue As Int
btnSender = Sender
btnValue = btnSender.Tag 'get button tag

If btnValue = x Then 'check which button
Buttonx.Text = "xyz" 'change text accordingly
End If

If btnValue = y Then
Buttony.Text = "xyz"
End If
End Sub

You could probably code the Ifs more efficiently but this gives you an idea of what I did.

-C
 
Upvote 0
Top