Combobox Weirdness

D

Don

I'm getting a strange error from the following code:

With Me.Combobox

' Clear list
.Items.Clear()

' Add items to the list
.Items.Add("item1")
.Items.Add("item2")
.Items.Add("item3")

' Select the default item
.SelectedItem = "item3" ' <--- This causes an exception

End With


The exception raised when trying to set the SelectedItem property of the
combox is:

Cast from string "item3" to type 'Integer' is not valid

I'm 100% sure this code used to work. I hadn't touched that project for
about a month, then I came back to it to fix up some code so that it would
compile with Option Strict on, but this code wasn't touched. I turned
Option Strict back off and it still craps out. The error message itself
doesn't make anysense because the SelectedItem property takes an Object, not
an integer, and the entire list is populated with strings, not integer.
What gives?

- Don
 
H

Herfried K. Wagner [MVP]

* "Don said:
I'm getting a strange error from the following code:

With Me.Combobox

' Clear list
.Items.Clear()

' Add items to the list
.Items.Add("item1")
.Items.Add("item2")
.Items.Add("item3")

' Select the default item
.SelectedItem = "item3" ' <--- This causes an exception

End With


The exception raised when trying to set the SelectedItem property of the
combox is:

Cast from string "item3" to type 'Integer' is not valid

Works perfectly for me. Are you sure you are assigning to
'SelectedItem' and not to 'SelectedIndex'?
 
D

Don

Herfried K. Wagner said:
Works perfectly for me. Are you sure you are assigning to
'SelectedItem' and not to 'SelectedIndex'?

Yup. SelectedItem. I think this must be some obscure VB bug or something
as there are other parts of my code that do just that and work fine. I have
no idea why it started happening. I guess I'll just have to chop that bit
of code it out, along with the combobox itself, and redo it.

- Don
 
D

Don

Ah, never mind. I figured it out. I had code in the SelectedIndexChanged
event that I had modified improperly. :p

- Don
 

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