Checkbox manipulation at runtime

JonPM

Well-Known Member
Licensed User
Longtime User
The forum solved my last problem, and now for a new one :)

How do you check and uncheck a checkbox at runtime?? Here is what I am trying:

B4X:
Sub Activity_Create(FirstTime As Boolean)
If File.Exists(File.DirInternal,"Map.txt") = True Then
      Dim Map1 As Map
      Map1 = File.ReadMap(File.DirInternal,"Map.txt")
      FavStartUp = Map1.Get("FavStartUp")
   Else
      Dim Map1 As Map
      Map1.Initialize
      Map1.Put("FavStartUp","False")
      File.WriteMap(File.DirInternal, "Map.txt", Map1)
   End If
   
   chkFavStartup.Initialize("")
   If FavStartUp = "True" Then
   chkFavStartup.Checked = True
      Else
         chkFavStartup.Checked = False
   End If
End Sub

I have tried using Invalidate but that doesn't work. I have confirmed that FavStartup in fact does equal True with:
B4X:
Sub Button1_Click
   Dim Map1 As Map
   Map1 = File.ReadMap(File.DirInternal,"Map.txt")
   Msgbox(Map1.Get("FavStartUp"),"")
End Sub

On a side note, do I need the Dim Map1 as Map each time I am accessing map1? I was just following the examples Erel did in the post "Text Files".
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I recommend you to use True and False and not "True" or "False".
You should then declared FavStartUp as Boolean.
Your code looks correct.

Where are you adding chkFavStartUp to the Activity? If you have added it with the designer then you shouldn't initialize it again.

On a side note, do I need the Dim Map1 as Map each time I am accessing map1
No. You can declare it once.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
In your code map1 is a local variable, so is not accessible from other subs. You may find it easier to have it as a global.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
After having declared FavStartUp as Boolean, you can simplify your code:
B4X:
If FavStartUp = True Then     
   chkFavStartup.Checked = True        
Else           
   chkFavStartup.Checked = False     
End If
to
B4X:
chkFavStartup.Checked = FavStartUp
Best regards.
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
I recommend you to use True and False and not "True" or "False".
You should then declared FavStartUp as Boolean.
Your code looks correct.

Where are you adding chkFavStartUp to the Activity? If you have added it with the designer then you shouldn't initialize it again.

I did as you said but it is still not working. My button confirms when it is true/false, but the checkbox doesn't change at startup.
Also, I used the designer to add chkFavStartUp to the activity. But if I don't initialize it in Activity_Create before the line "chkFavStartup.Checked = FavStartUp" I get an error saying it needs to be initialized.

Here is what I tried this time:
B4X:
''Activity module
Sub Process_Globals

   Dim Map1 As Map

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim btnSaveSettings As Button
   Dim chkFavStartup As CheckBox
   Dim FavStartUp As Boolean
   Dim Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
      
   If File.Exists(File.DirInternal,"Map.dat") = True Then
      Map1 = File.ReadMap(File.DirInternal,"Map.dat")
      FavStartUp = Map1.Get("FavStartUp")
   Else
      Map1.Initialize
      Map1.Put("FavStartUp",False)
      File.WriteMap(File.DirInternal, "Map.dat", Map1)
   End If

   Activity.LoadLayout("main")
   
   chkFavStartup.Initialize("")
   chkFavStartup.Checked = FavStartUp

End Sub



Sub btnSaveSettings_Click
   Map1.Initialize
   If chkFavStartup.Checked = True Then
   Map1.Put("FavStartUp",True)
   File.WriteMap(File.DirInternal, "Map.dat", Map1)
   Else
      Map1.Put("FavStartUp",False)
      File.WriteMap(File.DirInternal, "Map.dat", Map1)
   End If

   ToastMessageShow("Settings Saved",False)

End Sub

Sub Button1_Click
   Map1 = File.ReadMap(File.DirInternal,"Map.dat")
   Msgbox(Map1.Get("FavStartUp"),"")
End Sub
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
Doh! Problem fixed!
I failed to mention in previous posts that I am using TabHost. The reason I had to initialize chkFavStartUp was because that line of code was above the TabHost creation lines of code. Once I moved it below, I deleted the initialize line and it all worked!
 
Upvote 0
Top