B4J Question Add items to a ComboBox

JJ37

Member
Hello I want to add some names in a combobox (a few rows) but how to do that?
I have added a combobox1 control in internal designer and tried this code but that didn't work. What's wrong with this code?
What to do when I want to update this in case Person variable changes?
The combobox remains empty.

Private Person1 As String = "Jimmy"
Private Person2 As String = "Tim"
Private Person3 As String = "Eric"
Private Person4 As String = "Joe"
Private Person5 As String = "Simon"

ComboBox1.Items.Clear
ComboBox1.Items.AddAll(Array As String(Person1, Person2, Person3, Person4, person5))
 
Last edited:

JJ37

Member
Hi Erel. I think it can be improved for example the values should be updated automatically in the combo box. Here's my code

B4X:
#Region Project Attributes 
	#MainFormWidth: 600
	#MainFormHeight: 600 
#End Region

Sub Process_Globals
	Private fx As JFX
	Private MainForm As Form
	
	Private Person1 As String = "Jimmy"
	Private Person2 As String = "Tim"
	Private Person3 As String = "Eric"
	Private Person4 As String = "Joe"
	Private Person5 As String = "Simon"
	
	Private ComboBox1 As ComboBox
	Private cbIndex As Int
	Private btnUpdate As Button
	
End Sub

Sub AppStart (Form1 As Form, Args() As String)
	MainForm = Form1
	MainForm.RootPane.LoadLayout("1") 'Load the layout file.
	MainForm.Show
	
	UpdateComboBox
	ComboBox1.SelectedIndex = 0
	
End Sub

Sub UpdateComboBox
	
	ComboBox1.Items.Clear
	ComboBox1.Items.AddAll(Array As String("Please select", Person1, Person2, Person3, Person4, Person5))
	
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
	Return True
End Sub

Sub btnUpdate_Click
	UpdateComboBox
End Sub

Sub ComboBox1_ValueChanged (Value As Object)
	
	' Store Temporary Index
	If ComboBox1.SelectedIndex >=1 Then
		cbIndex = ComboBox1.SelectedIndex
		Log ("Stored Index: " & cbIndex)
		Log ("__________________________________")
	End If
	
	' Update Person Variable Logic
	 If cbIndex = 1 Then
	 		Person1 = Value
		else if cbIndex = 2 Then
			Person2 = Value
		else if cbIndex = 3 Then
			Person3 = Value
		else if cbIndex = 4 Then
			Person4 = Value
		else if cbIndex = 5 Then
			Person5 = Value
	End If
	
	' Show Log
	Log("SelectedIndex: " & ComboBox1.SelectedIndex)
	Log("Value: " & Value)
	Log ("__________________________________")
	
End Sub

Sub ComboBox1_SelectedIndexChanged(Index As Int, Value As Object)
	Log("Current Index is : " & Index & " (" & Value & ")")
	Log ("__________________________________")
End Sub
 
Last edited:

GMan

Well-Known Member
Licensed User
Your code works fine for me
 

JJ37

Member
That's great! But wouldn't it be nicer if the combo box is automatically updated every time the user changes name in the textfield and presses enter, and I've noticed that the SelectedIndex changes to -1 when something is entered in the combo box textfield simply because the selection loses focus when enter is pressed. This becomes a complex problem when the SelectedIndex and new name (value) should be changed and stored in case we imagine having an huge database with thousands of persons. My example serves as a basic foundation to more complex things, and perhaps as it gets more advanced we need to keep the userfriendly aspect and plan this to work in a different way. This example is easy and useful as long as there's just a few items and that we expect the user to save the updates, but I'd like to see a different approach, as it could be useful for learning new ways.

We need to somehow store the SelectedIndex and new Value to be able to control this sequence to build a more user-friendly GUI.
 
Last edited:
Top