none
InvalidArgument=Value of '0' is not valid for 'index' in combo box RRS feed

  • Question

  • Hi,

    I have my fronend UI application written in C#.There is a combo box on one of this form which is being updated by an unmanged VC++ api.

    When an item needs to be added to combo, the backend api is called (which takes the handle of the textbox where user enters the item to be added) which addes the item and refresh the combo box.

    When an item is to be deleted from the combo,the backend api is called (which takes the handle of the combo box) and deletes the selected item.

    After the item is deleted the backend api takes care to disable the combo if there are no elements in the combo.

    The problem i am facing is when i try to select an item from the combo box  i get the exception

    System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'index'.
    Parameter name: index
       at System.Windows.Forms.ComboBox.ObjectCollection.get_Item(Int32 index)
       at System.Windows.Forms.ComboBox.UpdateText()
       at System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
       at System.Windows.Forms.ComboBox.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    Please help with this.

    Regards

    Vinutha

     

    Monday, June 13, 2011 11:12 AM

Answers

  • This error usually means that there are no items in the collection. This probably has to do with the fact that you are updating the object using native code, then trying to access it using managed code. Even though the Windows object does have items in it, the managed collections may not have been initialized correctly.

    The framework keeps its own collection of items which have added to the list (combobox.objectcollection). When you add or remove items using the Framework API, the internal collection is updated and the Windows object is updated. If you add items using native code, the Framework collection is not updated - it stays empty.

    Lesson for the day - do not mix and match native code and managed code on the same control. More specifically, avoid using native code to manipulate managed framework controls. It almost always causes problems.

    Monday, June 13, 2011 2:47 PM
  • Hi Vinutha,

    The ComboBox in VB.Net is a managed control, which is brand-new compared to the ComboBox in VB6.0. I don't think you can still use this c++ unmanaged api to alter the ComboBox directly. I suggest to use managed code to update your combobox instead.

    Best regards,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 17, 2011 2:22 AM
    Moderator

All replies

  • This error usually means that there are no items in the collection. This probably has to do with the fact that you are updating the object using native code, then trying to access it using managed code. Even though the Windows object does have items in it, the managed collections may not have been initialized correctly.

    The framework keeps its own collection of items which have added to the list (combobox.objectcollection). When you add or remove items using the Framework API, the internal collection is updated and the Windows object is updated. If you add items using native code, the Framework collection is not updated - it stays empty.

    Lesson for the day - do not mix and match native code and managed code on the same control. More specifically, avoid using native code to manipulate managed framework controls. It almost always causes problems.

    Monday, June 13, 2011 2:47 PM
  • Hi,

    Thanks for the reply.

    1.I have another application which is in VB6.0 and using the unmanged VC++ api calls to update (add/delete) the combo box.

    In this VB6 application if am able to access item and dont get exception as in .NET.

    How is the handling of the items in the VB6 ComboBox different from handling items in .NET ComboBox.

    2.Now that i dont have any control on how unmanaged API is updating the combo box in managed code.

    Is there any work around or alternate solution for this problem.

    Regards

    Vinutha

    Tuesday, June 14, 2011 10:19 AM
  • Hi Vinutha,

    The ComboBox in VB.Net is a managed control, which is brand-new compared to the ComboBox in VB6.0. I don't think you can still use this c++ unmanaged api to alter the ComboBox directly. I suggest to use managed code to update your combobox instead.

    Best regards,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 17, 2011 2:22 AM
    Moderator