Map question

hakha4

Member
Licensed User
Longtime User
Hi!
I'm a newbie to Basic4Android but done some programming in VB6 so it's relatively easy to manage the 'basics'. I want to awoid codes like :

B4X:
btn_onoff1.Checked = False
btn_onoff2.Checked = False
btn_onoff3.Checked = False
btn_onoff4.Checked = False
etc...

and instead programmaticly loop trough objects(wievs) and change properties.
I've read the tutorial mapping buttons and then create them with Activity.AddView.. .But can you map buttons created with designer ? I've not yet learned how to position wievs in program so designer is the best for me at the moment.

See code below :

B4X:
Sub Globals
   Dim btn_onoff1 As ToggleButton
   Dim btn_onoff2 As ToggleButton
   Dim btn_onoff3 As ToggleButton
   Dim btn_onoff4 As ToggleButton
   Dim btn_onoff5 As ToggleButton
   Dim btn_onoff6 As ToggleButton
   Dim btn_onoff10 As ToggleButton
   Dim btn_onoff11 As ToggleButton
   Dim btn_onoff12 As ToggleButton
   Dim btn_onoff13 As ToggleButton
   Dim btn_onoff14 As ToggleButton
   Dim btn_onoff15 As ToggleButton
   Dim btn_onoff16 As ToggleButton
   Dim btn_onoff7 As ToggleButton
   Dim btn_onoff8 As ToggleButton
   Dim btn_onoff9 As ToggleButton
      
   Dim Map1 As Map

End Sub


Sub Activity_Create(FirstTime As Boolean)

Activity.LoadLayout("TCP_main")

 '######### map togglebuttons #############


  Map1.Initialize

  For i = 0 To 15

    Dim Btn As ToggleButton 

    Btn.Initialize("ToggleButtons")

    Map1.Put("btn_onoff" & i, Btn)

    Btn.Tag = "btn_onoff" & (i+1)'starts with btn_onoff1

   Next

Set_allON

End Sub


Sub Set_allON

Dim btn As ToggleButton
 
Dim x As Int

 
For x = 0 To 15

 btn = Map1.GetValueAt(x)

 btn.Checked = True

Next


End Sub

Code compiles ok but don't change the checked property on the designer created Togglebuttons. Am I totally out in the blue or is there a solution to this?

Any help pointing me in right direction appreciated
Regards Håkan
 

rboeck

Well-Known Member
Licensed User
Longtime User
Hi, in the last days i had a problem like you but with Radiobuttons. I made a subroutine, where i referenced the panel, which is the parent of the views and made possibilties: The parameter 0 is used to clear all radiobuttons on the panel to non-checked.
The second using is, to pass a value coming from the database and show the correct value in the panel(s).
It should be easy to transfer this logic for your use.

B4X:
Sub SetRadioButtons (p As Panel,Val As String)

Dim n As Int 

If Val="0" Then   'auf 0 setzen

   For n=0 To p.NumberofViews-1

      Dim v As View 
      v=p.GetView(n)
      If v Is RadioButton Then
         
         Dim rd As RadioButton 
         rd=v
         If rd.Checked=True Then
            rd.Checked=False
            Return         
         End If
         
      End If
   Next

Else

   For n=0 To p.NumberofViews-1

      Dim v As View 
      v=p.GetView(n)
      If v Is RadioButton Then
         
         If v.Tag=Val Then 
            Dim rd As RadioButton 
            rd=v
            rd.Checked=True 
            Return         
         End If
         
      End If
   Next
   
End If

End Sub

There is a logic effect between diffent radiobuttons, but not with the toogle buttons, so you should remove the return from the lower loop.
I hope, it helps.
Greetings
Reinhard
 
Upvote 0

hakha4

Member
Licensed User
Longtime User
Hi!
Thanks for reply. Your code works fine for setting ALL in one sub. I also want to set individual buttons in another sub. After reading forum I found out that Sender can do this by setting same event (in this case 'btn_onoff') to all togglebuttons and different tag values.

Example code
B4X:
Sub btn_onoff_Click
Dim Send As ToggleButton
  Dim tmpStatus As String
  Send = Sender' sets the button that raised the event to Send
  
   Select Case Send.Checked 
    Case True
     ToastMessageShow(Send.Tag &" Set to ON :",False)
    Case False
     ToastMessageShow(Send.Tag &" Set to OFF :",False)
   End Select
 
 

End Sub

Maybe some other newbies can make use of this.

BTW I get more and more impressed of this software for every day,awesome work of developers

Regards Håkan
 
Upvote 0
Top