iOS Question How to access the view that added programmatically?

hongbii khaw

Member
Licensed User
Longtime User
Hi all,
I have a question regarding the view.
For example, the button is created programmatically,
B4X:
For j = 0 To 2
            For i = 1 To  3
                Dim button As Button
                button.Initialize("Button",button.STYLE_SYSTEM)
                button.Text = i+(j*3)
                button.Tag = "button"&(i+(j*3))
                Panel1.AddView(button,(i)*Panel1.Width/5,100+(j*100),100,100)
            Next
        Next
When the event raised, we handle the button by looking in it's tag.
B4X:
Sub Button_Click
Dim button1 as Button
button1 = sender
End Sub
My question is, how to access the button that are not pressed?
Consider the scenario, when the "button 1" is pressed and all other buttons have to be invisible.
Thank you.
 

sorex

Expert
Licensed User
Longtime User
store it to a map then you can retrieve it by name (code might be not 100% right)

B4X:
dim buttons as map
buttons.initialize
For j = 0 To 2
            For i = 1 To  3
                Dim button As Button
                button.Initialize("Button",button.STYLE_SYSTEM)
                button.Text = i+(j*3)
                button.Tag = "button"&(i+(j*3))
                Panel1.AddView(button,(i)*Panel1.Width/5,100+(j*100),100,100)
                buttons.put("button"&(i+(j*3)),button)
            Next
        Next
 
Upvote 0

Yvon Steinthal

Active Member
Licensed User
I would do something like this:

B4X:
Dim refBtn as Button = Sender

For each v as view in Panel1.getAllViewsRecursive
 if v is Button Then
    Dim tempBtn as Button = v
    if tempbtn <> refBtn Then
       tempbtn.visible = False
    end if
  end if
Next


But there may be a better way....

PS: I deliberatly did not write "v as Button" as there seems to be some problems when i specify the type of view in the For Each loop...
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I would do it with an array of buttons, like this:
B4X:
Dim buttons(9) as Buttons
Dim i, j, k As Int
For j = 0 To 2
    For i = 0 To 2
        k = i + 3 * j
        buttons(k).Initialize("buttons",button.STYLE_SYSTEM)
        buttons(k).Text = k + 1
        buttons(k).Tag = "button" & (k)
        Panel1.AddView(buttons(k), (i + 1) * Panel1.Width / 5, 100dip + (j * 100dip), 100dip, 100dip)
     Next
Next

I am not convinced by this:
Panel1.AddView(buttons(k), i * Panel1.Width / 5, 100dip + (j * 100dip), 100dip, 100dip)
The left property is according to Panel1.Width / 5, but thw width property is a constant value.
By the way, you should use dip values for view dimensions.
 
Last edited:
Upvote 0
Top