B4J Question [SOLVED] Checkbox value using code for a CheckboxTreeView

Peter Lewis

Active Member
Licensed User
Longtime User
Hi,

I am trying to get the value from a MYSQL database and populate the checkbox on the tree. I can get the state if I physically click it.

I did not see anywhere how I can populate the tree with its checkbox value after generating the TreeView.

If it can only be done at the generation of the Tree, then I will need to move the generation code to not be ONCE OFF operation at the begin of the program, but then I will need to be able to clear the Tree, another thing I could not find.

Any direction ?

Thank you




1726093527012.png
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Example of changing the checked state of a specific item:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private TreeView1 As TreeView
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    TreeView1.SetCheckBoxesMode
    For i1 = 1 To 20
        Dim cti As CheckboxTreeItem
        cti.Initialize("", "Item #" & i1)
        cti.Checked = Rnd(0, 2) = 1
        cti.Expanded = True
        TreeView1.Root.Children.Add(cti)
        For i2 = 1 To Rnd(0, 5)
            Dim cti2 As CheckboxTreeItem
            cti2.Initialize("", "Subitem #" & i2)
            cti2.Checked = Rnd(0, 2) = 1
            cti.Children.Add(cti2)
        Next
    Next
End Sub


Private Sub btnToggle_Click
    'Toggle the state of item #3 - subitem #2
    'note that this will throw an error if there is no such item. Run again if it happens as the tree is generated randomly
    Dim item3 As CheckboxTreeItem = TreeView1.Root.Children.Get(2)
    Dim subitem2 As CheckboxTreeItem = item3.Children.Get(1)
    subitem2.Checked = Not(subitem2.Checked)
End Sub
 

Attachments

  • Project.zip
    8.6 KB · Views: 37
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
Example of changing the checked state of a specific item:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private TreeView1 As TreeView
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    TreeView1.SetCheckBoxesMode
    For i1 = 1 To 20
        Dim cti As CheckboxTreeItem
        cti.Initialize("", "Item #" & i1)
        cti.Checked = Rnd(0, 2) = 1
        cti.Expanded = True
        TreeView1.Root.Children.Add(cti)
        For i2 = 1 To Rnd(0, 5)
            Dim cti2 As CheckboxTreeItem
            cti2.Initialize("", "Subitem #" & i2)
            cti2.Checked = Rnd(0, 2) = 1
            cti.Children.Add(cti2)
        Next
    Next
End Sub


Private Sub btnToggle_Click
    'Toggle the state of item #3 - subitem #2
    'note that this will throw an error if there is no such item. Run again if it happens as the tree is generated randomly
    Dim item3 As CheckboxTreeItem = TreeView1.Root.Children.Get(2)
    Dim subitem2 As CheckboxTreeItem = item3.Children.Get(1)
    subitem2.Checked = Not(subitem2.Checked)
End Sub
That is perfect, Thank You
 
Upvote 0
Top