iOS Question Use B4XComboBox and IsInitialized

angel_

Well-Known Member
Licensed User
Longtime User
In a project I have something similar to this, it's just an example, if it's not loaded the B4XComboBox gives error (in B4A it works) I have to use Try EndTry instead of IsInitialized
B4X:
    Dim Lista As List = Array("aa", "bb", "cc")
    Dim Seleccion As String
    
    #If B4a
        Seleccion = B4XComboBox1.SelectedItem
    #Else If B4i
        '1st option
        If B4XComboBox1.IsInitialized Then
            Seleccion = B4XComboBox1.SelectedItem
        Else
            Seleccion = ""
        End If
        
        '2nd option
'        Try
'            Seleccion = B4XComboBox1.SelectedItem
'        Catch
'            Seleccion = ""
'            Log(LastException)
'        End Try
    #End If
    
    B4XComboBox1.SetItems(Lista)
    B4XComboBox1.SelectedIndex = Max(B4XComboBox1.IndexOf(Seleccion), 0)
 

Attachments

  • TestCbo.zip
    168.7 KB · Views: 154

angel_

Well-Known Member
Licensed User
Longtime User
I don't understand what you are trying to do. You cannot get the selected item before you set the items.
Let's see if that's how it is understood, if not, I'll upload an example. The question is if I have to use Try EndTry or is another option better

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("Page2")
    
    kvs.Initialize(xui.DefaultFolder, "kvs.dat")
    
    B4XSwitch1.Value = kvs.GetDefault("B4XSwitch1", True)
    B4XSwitch2.Value = Not(B4XSwitch1.Value)
    
    LoadCombo
    
    B4XComboBox1.SelectedIndex = kvs.GetDefault("B4XComboBox1", B4XComboBox1.SelectedIndex)
End Sub

Sub B4XPage_Disappear
    kvs.Put("B4XSwitch1", B4XSwitch1.Value)
    kvs.Put("B4XComboBox1", B4XComboBox1.SelectedIndex)
End Sub

Sub B4XSwitch_ValueChanged (Value As Boolean)
    LoadCombo
End Sub

Sub LoadCombo
    #If B4a
        Before = B4XComboBox1.SelectedItem
    #Else If B4i
    '1st option
    If B4XComboBox1.IsInitialized Then
        Before = B4XComboBox1.SelectedItem
    Else
        Before = ""
    End If
        
    '2nd option
'    Try
'        Before = B4XComboBox1.SelectedItem
'    Catch
'        Before = ""
'        Log(LastException)
'    End Try
    #End If
    
    Lista.Initialize
    
    If B4XSwitch1.Value Then
        Lista = Lista1
    Else
        Lista = Lista2
    End If

    B4XComboBox1.SetItems(Lista)
    B4XComboBox1.SelectedIndex = Max(B4XComboBox1.IndexOf(Before), 0)
End Sub
 
Upvote 0

angel_

Well-Known Member
Licensed User
Longtime User
Sorry, maybe I'll explain better with the attachment
 

Attachments

  • B4iProject.zip
    171.5 KB · Views: 147
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…