Naming Buttons

Marek

New Member
Licensed User
Longtime User
Hello,

I'm wondering if there's a way to intialize buttons to some value when they are in ordered numerically.

For example,

Button1.Text
Button2.Text
Button3.Text
etc

Is there a way to initalize these in a loop
i.e. Button(i).Text =
or
Concatenate the part before the .text

I appreciate any help.

Thanks
 

peacemaker

Expert
Licensed User
Longtime User
Something like this typing here manually, not checked:

B4X:
Sub Globals
Dim Buttons() as Button
end sub


Sub Activity_Create(FirstTime As Boolean)
For i=0 to X-1   'think about needed X qty
  dim a as Button
  a.Initialize("Buttons")
  Activity.AddView(a, i1, i2, w, h) 'think about coordinates and dimentions
  Buttons(i) = a 'try to use these names of buttons
next

Sub Buttons_Click
Dim a as Button
a = Sender  'but here, seems, anyway it needs to check all the buttons to understand which is pressed

end sub
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Sub Globals
Dim Buttons() as Button
end sub


Sub Activity_Create(FirstTime As Boolean)
For i=0 to X-1 'think about needed X qty
dim a as Button
a.Initialize("Buttons")
Activity.AddView(a, i1, i2, w, h) 'think about coordinates and dimentions
Buttons(i) = a 'try to use these names of buttons
next

Sub Buttons_Click
Dim a as Button
a = Sender 'but here, seems, anyway it needs to check all the buttons to understand which is pressed

end sub

even a little simpler, using this code:

B4X:
Sub Globals
Dim Buttons(X) as Button   'think about needed X qty
end sub


Sub Activity_Create(FirstTime As Boolean)
For i=0 to X-1  
  buttons(i).Initialize("Buttons")
  Activity.AddView(buttons(i), i1, i2, w, h) 'think about coordinates and dimentions
next

Sub Buttons_Click
Dim a as Button
a = Sender  'but here, seems, anyway it needs to check all the buttons to understand which is pressed

end sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You should set the Tag propety to know what button raised the event.
B4X:
Sub Globals
   Dim Buttons(X) as Button   'think about needed X qty
End Sub


Sub Activity_Create(FirstTime As Boolean)
   For i=0 to X-1  
     Buttons(i).Initialize("Buttons")
     Activity.AddView(Buttons(i), i1, i2, w, h) 'think about coordinates and dimentions
     Buttons(i).Tag = i   
   Next
End Sub

Sub Buttons_Click
  Dim a As Button
  a = Sender  
  Select a.Tag
  Case 0  ' buttons(0)
  .
  Case 1  ' buttons(1)
  .
  End Select
End Sub
Best regards.
 
Upvote 0
Top