ComboBox and ArrayList problem.

kyto

Member
Licensed User
Longtime User
I have a problem with a combobox and an arraylist, allocate a numerical value to each of the items of the combobox, and a button to allocate combobox value in -1 (ComboBox1. SelectedIndex = -1)

B4X:
Sub Globals
   'Declare the global variables here.

End Sub

Sub App_Start
     Form1.Show
    ArrayList1.Add(1)
    ArrayList1.Add(1)
    ArrayList1.Add(2)
    ArrayList1.Add(3)
    ArrayList1.Add(4)
    ArrayList1.Add(5)
End Sub

Sub ComboBox1_SelectionChanged (Index, Value)
    Label2.Text = ArrayList1.Item(Index)
End Sub

Sub Button1_Click
ComboBox1.SelectedIndex=-1
End Sub

but it returns me error:

ComboBox1. SelectedIndex = -1
Error description: NullReferenceException
Continue?

This happens only on having pressed Reset Button, whit a ComboBox1. SelectedIndex <>-1.

Which is the solution to this problem?

Thanks!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is probably the SelectionChanged event that really raises the error.
Once you set the SelectedIndex to -1, the SelectionChanged event is raised and as there is no item indexed as -1 the error shows.
This will fix it:
B4X:
Sub ComboBox1_SelectionChanged (Index, Value)
    if index >= 0 then Label2.Text = ArrayList1.Item(Index)
End Sub
 

Rioven

Active Member
Licensed User
Longtime User
hi Kyto, not the best code but works...

The sample is to create the combobox on runtime(note: you should not have the control on the designer) and assign the arraylist to this combobox.

I don't know what you try to achieve, it could have some better solutions or workarounds.


B4X:
Sub Globals
   'Declare the global variables here.
End Sub

Sub App_Start
     Form1.Show
    ArrayList1.Add(1)
    ArrayList1.Add(1)
    ArrayList1.Add(2)
    ArrayList1.Add(3)
    ArrayList1.Add(4)
    ArrayList1.Add(5)

 AddAssignCombobox
   
End Sub

Sub  AddAssignCombobox
AddComboBox ("Form1", "ComboBox1", 10, 10, 120, 30)
   For i= 0 To ArrayList1.Count-1
   ComboBox1.Add(ArrayList1.item(i))
    Next i
End Sub


Sub ComboBox1_SelectionChanged (Index, Value)
    Label2.Text = ArrayList1.Item(Index)
End Sub

Sub Button1_Click
ComboBox1.dispose
AddAssignCombobox
Label2.Text=""
End Sub

Regards,
 
Last edited:

kyto

Member
Licensed User
Longtime User
It works … partially.

Hello!

Thanks for this help. :)

Erel: I tried the solution that you give me, and I do not make that works.

Rioven: Your solution worked for me in (test), code but running compiled in my PPC (I tried in 2 different with different operating systems, WM6 and WM 2003), gives me a error if I select an item, and without to drop the stylus I drag to comboselectedIndex =-1, and then drop it.
An unexpected error has occurred in Test.Exe…
Details:
TEST.EXE
NullReferenceException

at Dbasic.Controls.CCombo.SelectedIndexChanged()
at System.Windows.Forms.ComboBox.OnSelectedIndexChanged()
at System.Windows.Forms.ComboBox.WnProc()
at System.Windows.Forms.Control._InternalWnProc()
at Microsoft.AGL.Forms.EVL.EnterMainLoop()
at System.Windows.Forms.Application.Run()
at B4Pruntime.Class1.Main()

What causes this error?
I tried both solutions in (Test) code, in addition to my real code (Child test).
Saludos.
Thanks!
 

Attachments

  • Test.sbp
    985 bytes · Views: 256
Last edited:

Rioven

Active Member
Licensed User
Longtime User
Hi Kyto,

Your only problem is to clear the displayed at comboboxes...right?

As Erel said, you can not index the -1, this will give you error.

Another workaround is to put a label or textbox in front of combobox, and assign text to it once combobox_selectionchanged.

Also, just clear the label or textbox once reset button is pressed.

You may use this label or textbox values for your calculation process instead.

B4X:
Sub Globals
   'Declare the global variables here.
End Sub

Sub App_Start
     Form1.Show
    ArrayList1.Add(1)
    ArrayList1.Add(2)
    ArrayList1.Add(3)
    ArrayList1.Add(4)
    ArrayList1.Add(5)

 AddAssignCombobox
   
End Sub

Sub  AddAssignCombobox
   For i= 0 To ArrayList1.Count-1
   ComboBox1.Add(ArrayList1.item(i))
    Next i
End Sub


Sub ComboBox1_SelectionChanged (Index, Value)
    label2.Text = ArrayList1.Item(Index)
End Sub

Sub Reset_Click
Label2.Text=""
End Sub


See sample...attached
 

Attachments

  • test2.sbp
    982 bytes · Views: 245

kyto

Member
Licensed User
Longtime User
Thanks! Solved.

Rioven: I did what you indicated me in your example, and I already solved the problem, in addition to giving me several ideas.
:sign0188:
Thank you very much!
 
Top