Android Question Combining a variable with a button name, if possible how?

MikeSimpson

Member
Licensed User
Longtime User
I have created 44 buttons named Button1 to Button44 with the designer.
Is it possible to change a property of a button depending on a variable?
For example if I make a code like this to disable all 44 buttons:

B4X:
For X = 1 to 44
        ButtonX.Enabled = False
Next

Apperently this is not correct, but is there a way for "ButtonX"?
 

derez

Expert
Licensed User
Longtime User
You can create the buttons as an array, simple way is by code:
Dim buttons(44) as button
Then you can do
B4X:
For i= 0 to 43
        Buttons(i).Enabled = False
Next
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User

Yes that works really good for programmatically added buttons. To get this to work with buttons from Designer you need to put references to the right buttons into this array after loading the layout.
B4X:
Buttons(0) = Button0
Buttons(1) = Button1
Buttons(2) = Button2
[..]
Buttons(43) = Button43
 
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
Thank you, DonManfred. As I don't know jet how to handle lists, I went to the suggestion from derez, but was confused how that could work with my already created button.

With your 2nd post you clered this up, so I will try with the references.

Thank you.
 
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
Thank you for the offer to look at my code. I am learning, so it is probably not the smartest code, but it is working with no errors, eccept the buttons. If you click on "Alphabet" and then on "Play", I want to disable all 44 buttons so they can't be pressed during the alphabet is playing.
I added the code to the btnPlay_Click, but nothing happend to the buttons.

I did the following:

B4X:
Dim Buttons(45) As Button


Buttons(1) = Button1
Buttons(2) = Button2
.
.
Buttons(43) = Button43
Buttons(44) = Button44

And to disable the buttons:
B4X:
Sub btnPlay_Click

    For i = 1 To 44
        Buttons(i).Initialize("Buttons")
        Buttons(i).Enabled = False
    Next
   
    Timer1.Initialize("Timer1", 500)
    Timer1.Enabled = True
    Ende = False
    Playcount = 1
   
End Sub

The file is to big for an attachment, but you can download it here
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
In the Designer you should set the same "Event Name" for all the buttons (ex. "BtnEvPlay").
Also, you should use the Tag property of each button setting it to the name of the button (the "order number", in your case).

then
B4X:
Sub BtnEvPlay_Click
  private btn as Button = Sender
  ' here you could use:
  ' Select case btn.Tag
  '    Case 1
  '    Case 2
  '   ...
  ' End Select
  Timer1...
End Sub

P.S. sorry, I was thinking of something else.

To set a property is ok to use the code DonManfred.

Another way is:
put all and only your buttons in a panel and use:
B4X:
For Each V As View In Panel1.GetAllViewsRecursive
        Private btn As Button = V
        ' or:
        ' If GetType(V) Is Button Then
        btn.Enabled = False
    Next
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
Luca was faster than me..
anyway, please find attached a test app that shows how to use the GetAllViewRecursive function.

B4X:
For Each b As Button In Activity.GetAllViewsRecursive
    If b.Tag <> "untouchable" Then b.Enabled=False
Next

I use a specific Tag value for those buttons that should never be disabled.
If your buttons are on a panel, just substitute Panelx to Activity in above code.
 

Attachments

  • test.zip
    7.3 KB · Views: 122
Upvote 0

klaus

Expert
Licensed User
Longtime User
Attached you find a project where the buttons are added in the code, which is much easier.
The Buttons are in an array.
You should verify the characters in the buttons, I put them in a string array to add them easily to the buttons.
I added a new empty panel for the buttons, but I left the original one with the buttons in the layout file, renamed it to pnlAlpha1 and set it to invisible and. You should remove it after testing.

This is the code to add the buttons:
B4X:
Sub InitButtons
    Dim Row, Col, i As Int
   
    pnlAlpha.Top = 36%y
    pnlAlpha.Height = 64%y
    For Row = 0 To RowNb - 1
        For Col = 0 To ColNb - 1
            i = Row * ColNb + Col
            Buttons(i).Initialize("btnAlpha")
            pnlAlpha.AddView(Buttons(i), Col * btnWidth, Row * btnHeight, btnWidth, btnHeight)
            Buttons(i).Text = btnText(i)
            Buttons(i).Tag = i + 1
            Buttons(i).TextSize = 28
            Buttons(i).Typeface = Typeface.DEFAULT_BOLD
        Next
    Next
End Sub
You see the other modification in the files below.

As the whole zip file is too big I post only the changed files.
You must rename ThaiLeseTrainer.txt to ThaiLeseTrainer.b4a.
 

Attachments

  • Alpha.bas
    5.2 KB · Views: 127
  • ThaiLeseTrainer.txt
    3.6 KB · Views: 121
  • alpha.bal
    17.3 KB · Views: 127
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
Thank you very much.
I will have a closer look later and study your solution.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…