B4J Question select first item

alirezahassan

Active Member
Licensed User
hi,
when i add some items to my ChoiceBox view, The displayed item is empty and when i write ( CK_Search.SelectedIndex = 0 ), sub ( Private Sub CK_Search _SelectedIndexChanged(Index As Int, Value As Object) ) is performed and my code is running. what should i do to when i set my items, it shows the first item without performed _SelectedIndexChanged sub.
2021-07-05_00-01-10.png


B4X:
dim CK_Search As ChoiceBox
'.... Initialize,AddNode

CK_Search.Items.Add("20")
CK_Search.Items.Add("50")
CK_Search.Items.Add("100")
CK_Search.Items.Add("Show all")
CK_Search.SelectedIndex = 0 ' this code
 

JordiCP

Expert
Licensed User
Longtime User
A quick workaround is to add a control boolean var to indicate the assignment has been made programaticaly

Something similar to this
B4X:
Sub Class_Globals     'or the corresponding module
   Dim bChangedFromCode as Boolean = False
End Sub

Sub yourSub
...
dim CK_Search As ChoiceBox
'.... Initialize,AddNode

CK_Search.Items.Add("20")
CK_Search.Items.Add("50")
CK_Search.Items.Add("100")
CK_Search.Items.Add("Show all")
bChangedFromCode = True
CK_Search.SelectedIndex = 0 ' this code
End Sub


Private Sub CK_Search_SelectedIndexChanged(Index As Int, Value As Object) ) 
  If Not(bChangedFromCode) Then
    '...process the event here
  End If
  bChangedFromCode= False
End Sub
 
Upvote 0
Top