Android Question what is TAG used for in "SecondProgram"

howard bassen

Member
Licensed User
What is the purpose of the tag property for this tutorial program?

In the beginners guide, Second program, it seems that btnSender.Tag is supposed to get the tag property of each button that is clicked. However, if I change the values of the tags of each button, nothing happens, and the program run as usual.

--------------------

code:

Sub btnEvent_Click
'We need to know what button raised the event.
'we use the Sender Object which Is a special Object that holds
'the Object reference of the View that generated the event In the event routine.
Dim btnSender AsButton
btnSender = Sender

Select btnSender.Tag

Case"BS"
IflblResult.Text.Length >0Then
lblResult.Text = lblResult.Text.SubString2(0,lblResult.Text.Length - 1)
' >> lblResult displays the user input, not the result
EndIf
CaseElse
lblResult.Text = lblResult.Text & btnSender.Text
'>> IMPORTANT
' >>display the value of the Button In the lblResult Label
' >> and pass it to the CheckResult sub.
EndSelect
IflblResult.Text.Length = 0Then
'When clicking on the BS button we must remove the last
'character from the existing text In lblResult

btn0.Visible = False
Else
btn0.Visible = True
EndIf
End Sub
 

mangojack

Well-Known Member
Licensed User
Hi Howard .. please use [code]Insert Code here ...[/code] code tags when posting code.

The Tag property is handy when multiple views share the same event. It is a way of distinguishing which view was clicked / interacted with etc.
You don't have to use the Tag property .. in this example you could have tested the button.Text property to decide what code to run.

As for your code .. you changed the Tag property .. but the code output displays the button.Text ( did you change that also)

To test result make the following change ...
B4X:
Case Else
  lblResult.Text = lblResult.Text & btnSender.Tag   '@@  Display the Tag property instead ...
End Select
 

howard bassen

Member
Licensed User
Hi Howard .. please use [code]Insert Code here ...[/code] code tags when posting code.

The Tag property is handy when multiple views share the same event. It is a way of distinguishing which view was clicked / interacted with etc.
You don't have to use the Tag property .. in this example you could have tested the button.Text property to decide what code to run.

As for your code .. you changed the Tag property .. but the code output displays the button.Text ( did you change that also)

To test result make the following change ...
B4X:
Case Else
  lblResult.Text = lblResult.Text & btnSender.Tag   '@@  Display the Tag property instead ...
End Select
 

klaus

Expert
Licensed User
In the beginners guide, Second program, it seems that btnSender.Tag is supposed to get the tag property of each button that is clicked.
This is the case.
B4X:
Select btnSender.Tag
Case"BS"
CaseElse
And then, for the numeric buttons I used btnSender.Text because the button text is what I want.
I could also have used the Tag property, but in this case I prefered the Text propety..
B4X:
CaseElse
    lblResult.Text = lblResult.Text & btnSender.Text
However, if I change the values of the tags of each button, nothing happens, and the program run as usual.
This true for the numeric buttons, but if you change the Tag property of btnBS the program will not work properly !

For the code tags use this:
upload_2014-12-22_11-13-5.png
 
Top