ComboBox and ArrayList problem.

Discussion in 'Questions (Windows Mobile)' started by kyto, Oct 11, 2007.

  1. kyto

    kyto Member Licensed 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)

    Code:
    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!
     
  2. Erel

    Erel Administrator Staff Member Licensed 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:
    Code:
    Sub ComboBox1_SelectionChanged (Index, Value)
        
    if index >= 0 then Label2.Text = ArrayList1.Item(Index)
    End Sub
     
  3. Rioven

    Rioven Active Member Licensed 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.


    Code:
    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"101012030)
       
    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: Oct 11, 2007
  4. kyto

    kyto Member Licensed 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!
     

    Attached Files:

    • Test.sbp
      File size:
      985 bytes
      Views:
      32
    Last edited: Oct 12, 2007
  5. Rioven

    Rioven Active Member Licensed 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.

    Code:
    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
     

    Attached Files:

  6. kyto

    kyto Member Licensed 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!
     
  7. gwapong

    gwapong Member Licensed User

    how to open this sbp file?
     
  8. mjcoon

    mjcoon Well-Known Member Licensed User

    Buy Basic4PPC (for Windows Mobile)? Else what is your problem?

    Mike.
     
Loading...