Combobox.SelecedItem ArgumentOutOfRangeException

N

Nick Hoggard

Hi All,

I'm having a bit of trouble with a combo box at the moment. I'm building
in VB.NET CF and when I try to set the selected item of a ComboBox I am
getting an ArgumentOutOfRangeException thrown.

The exception I'm getting is the following. The strange part is that it
ignores the Try / Catch block and is just displayed in the IDE directly (in
the exception popup you would expect to see if you forgot the try / catch
block).

"An unhandled exception of type 'System.ArgumentOutOfRangeException'
occurred in mscorlib.dll'
Additional information: ArgumentOutOfRangeException

The line in question reads

cmb.SelectedItem = objSelectedItem

where objSelectedItem was just a pointer to the item I want to select.

My first thought was that the object simply wasn't in the combos data
source, so I checked with the following watch:

ctype(cmb.datasource, ArrayList).contains(objSelectedItem)

This watch came back as true, so the object is definitely in the data
source.

The datasource at this stage has been changed since the pointer to
objSelectedItem was created, but the object is still in the ArrayList and
has not been re-instantiated. The only reason the datasource has changed
was to remove one of the items from the ArrayList

E.g.

dim objSelectedItem as MyCustomType = ctype(cmd.selectedItem,
MyCustomType)
dim al as ArrayList = ctype(cmb.datasource, ArrayList)
al.remove(someOtherObject)
cmb.datasource = nothing
cmb.datasource = al
cmb.SelectedItem = objSelectedItem

I'm not sure if this is the tidiest way to do this, but apart for the
current bug I'm encountering it seems to work ok.

Any help with this would be greatly appreciated.

Thanks in advance

Nick Hoggard
 
N

Nick Hoggard

Just as a bit more followup information, this also seems to happen in some
instances when I use selectedinde

E.g.

cmb.SelectedIndex = -1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top