catching a button_click as variable/string

MDEnt

Member
Licensed User
Longtime User
If I were to have a number of buttons spread throughout my app - across different panels for example. Is there an easy way to catch the button name that was clicked as a variable/string?

Thanks to all for a great support on a great product.
 

JogiDroid

Member
Licensed User
Longtime User
If I were to have a number of buttons spread throughout my app - across different panels for example. Is there an easy way to catch the button name that was clicked as a variable/string?

Thanks to all for a great support on a great product.

You could use buttons Tag property/variable to hold your object/string..
 
Upvote 0

MDEnt

Member
Licensed User
Longtime User
OK, I could probably use the Tag in my project.

If I were to plan on using an IF to detect the click of a button, how do I detect/capture the tag for which button has been clicked?

Thanks for the help.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
In a Click event, the value 'Sender' is a pointer to the object that raised the Click event. So if you want the Tag value stored in GetTagValue, use this code in the Click event:

B4X:
   Dim btn As Button
   btn = Sender
   GetTagValue=btn.Tag
 
Upvote 0

FrankR

Member
Licensed User
Longtime User
But why no Name/Id attribute

I came across this over the weekend myself, and I'm using Tag for now.

But why don't we have a Name/id attribute?
 
Upvote 0

FrankR

Member
Licensed User
Longtime User
Thank you for pointing out that previous dialog.

Still not following why we wouldn't have a separate name attribute.
Even if it is not precisely the java i.d. under the covers, I don't know why
we wouldn't have a separate tag element called Name that we can use the same way we're using Tag for finding Views, saving Tag for other uses.
 
Upvote 0

FrankR

Member
Licensed User
Longtime User
Also, is everyone doing like me and iterating through all the controls of a parent to find a particular control, or is there some simpler approach I'm not aware of yet?
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
You could stick them in a Map and get them that way. It would be far more efficient.
B4X:
Dim Map1 As Map
Map1.Initialize
...
Dim b As Button
name = "aname"
b.Initialise(name)
Map1.Put(name, b) ' key can be whatever you want
...
Dim b2 As Button
b2 = Map1.Get("name")
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User

TOP TIP :sign0098:, I will be using that some time soon.
 
Upvote 0

MDEnt

Member
Licensed User
Longtime User
A few interesting options it seems - utlimately here is what I am trying to accomplish:

I have 25 buttons (each one on a separate page/view in designer) - each button is named similar (buttonV1, buttonV2 ... etc.)

I have 25 Subs each very much identical - the button simply moves to the next panel...

Sub buttonV1_Click
Load PageV2
End Sub

Sub buttonV2_Click
Load PageV3
End Sub

and so on...

everything works fine as it is - my number of panels with buttons may grow - so I'm looking to (efficiently) consolidate all the subs into a single sub by somehow making a single variable that can identify which button was clicked and pass the variable to the sub by building a string for example - that way it will call the appropriate next panel in the series.
 
Last edited:
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
What you need to do is to set the event name of all the buttons to be the same (eg HandleButtons), and the tag to be the individual panel name
Then in the HandleButtons_Click event:
B4X:
Dim btn As Button
btn = Sender
GetTagValue=btn.Tag
Load GetTagValue
 
Upvote 0

MDEnt

Member
Licensed User
Longtime User
Yes, I see how this could work. I'm playing around with it as suggested and getting:

Compiling code. Error
Error parsing program.
Error description: Undeclared variable 'load' is used before it was assigned any value.
Occurred on line: 130
Load GetTagValue
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Load GetTagValue
That is not valid B4A syntax. Assuming you have writen a Sub called Load

Sub Load(page As String)
...
End Sub


Then you need to call it as


Sub button_Click
Dim btn As Button
btn = Sender
GetTagValue=btn.Tag
Load(GetTagValue)
End Sub
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User

Sorry, I was assuming that your code example
B4X:
Sub buttonV2_Click
Load PageV3
End Sub
was paraphrased, and worded mine accordingly.

If you post up how you want the panels handling (are you creating them "on the fly" or just showing/hiding them) then we can probably advise (post one of the actual subs you have at the moment).
 
Upvote 0

MDEnt

Member
Licensed User
Longtime User
Frst off - thanks so much to all for the help - learning a lot here with all suggestions.

Actually my provided code example is correct (my bad with having a space after the Load in my initial example) in that it calls the panel in a further sub (for other programmatic reasons) example follows:

Sub buttonV2_Click
LoadPageV3
End Sub
.
.
.
Sub LoadPageV3
panel03.Initialize("panel03")
Activity.AddView(panel03, 0, 0, 100%x, 100%y)
panel03.LoadLayout("page03")
End Sub
.
.
.
Sub buttonV3_Click
LoadPageV4
End Sub
.
.
.
Sub LoadPageV4
panel04.Initialize("panel04")
Activity.AddView(panel04, 0, 0, 100%x, 100%y)
panel04.LoadLayout("page04")
End Sub
.
.
.
etc.

Right now I'm trying to consolidate all the buttonVX_clicks into one sub - then I'll tackle consolidating the panel subs down the road...
 
Last edited:
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
OK, try this:
set the event name of all the buttons to be the same (eg HandleButtons)
set the tag to be the individual sub name (LoadPageV3 etc)


Then in the HandleButtons_Click event:
B4X:
Dim btn As Button
btn = Sender
GetTagValue=btn.Tag
CallSub("",GetTagValue)

You can probably do away with using a string for GetTagValue -
try CallSub("",btn.Tag)
 
Upvote 0

MDEnt

Member
Licensed User
Longtime User
Success! Thanks so much!

Now to consolidate those panel subs - a new question thread coming soon I'm sure!
 
Upvote 0

CarlM

Member
Licensed User
Longtime User
OK, try this:
set the event name of all the buttons to be the same (eg HandleButtons)

:sign0013:

Sorry to hijack this - but I can't find how to set an event handler to a sub that I created in code (eg: Sub FireWhenAnyButtonIsClicked). Using the designers 'Generate Members' creates a sub for each control/view; but its not possible to generate a single Sub for multiple views using this option.

Do I need to create the buttons in code then inititialise them with the same argument?

Thanks.

Sorry - I have it - - using Search and tutorials helps!
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…