Setting Selected Index causes invalid cast exception!!!

  • Thread starter Thread starter Bilal Abbasi
  • Start date Start date
B

Bilal Abbasi

Hello all,

I am re-writing an application in dot net, that I originally wrote in VB6.
The snag I am getting into is at one part of the application setting the
selected index of a list box works, however, it fails for another list box.
Something is telling VB that I am casting. The error message I get is
"Specified cast is not valid".

Could someone please shed some light on this subjec. Please????

Bilal Abbasi
 
In your project options. turn on the OPTION STRICT. You may be experiencing
a late binding issue. By turning STRICT ON you will see the cast issue when
you build the application before its run.

Then either fix it or post the offending code.

Regards - OHM
 
* "Bilal Abbasi said:
I am re-writing an application in dot net, that I originally wrote in VB6.
The snag I am getting into is at one part of the application setting the
selected index of a list box works, however, it fails for another list box.
Something is telling VB that I am casting. The error message I get is
"Specified cast is not valid".

Post the code.
 
I did change the option strict to on. This required me to do a few changes.
After all the changes the build did not give me errors, however, when I run
the project, it still throws the InvalidCastException.
list2.selectedIndex = 0
I even changed it to
dim i as integer = 0
list2.selectedIndex = i
 
Hi Bilal,

This is weird, did you try it by removing the listbox and drag it again on
your form.

And are you sure it is this, and not something just below it, send some more
code in the surrounding of this, maybe we can help you?

Cor
 
The statement

list2.selectedIndex = 0 should work provided that an item exists at the
position. However, I would not expect it to give InvalidCast.

Is this of the class ListBox ?


regards - OHM
 
* "Bilal Abbasi said:
I did change the option strict to on. This required me to do a few changes.
After all the changes the build did not give me errors, however, when I run
the project, it still throws the InvalidCastException.
list2.selectedIndex = 0
I even changed it to
dim i as integer = 0
list2.selectedIndex = i

Should work. Is 'list2' an instance of the Windows Forms ListBox control?
 
This is weird.

I changed the selectedIndex = 1. It started to work. Changed it back to 0.
Continues to work. Don't know what happened.

Bilal
 
Back
Top