By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,600 Members | 1,683 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,600 IT Pros & Developers. It's quick & easy.

if listbox.selecteditem(I) I get Invalid cast Exception was unha

P: n/a
I am new to .net can someone teranslate the foloowing in .net
Ls and Lt will be listboxes.
Sub ListCpSelected(Ls As Control, Lt As Control)
'Copies selected items from one list to another
Dim I As Integer
I = 0
While (I < Ls.ListCount) --- I beleive is - listbox.selectedindices.count
If Ls.Selected(I) Then I am using if listbox.selecteditem(I) I get
Invalid cast Exception was unhandled
Lt.AddItem Ls.List(I)
I = I + 1
Wend
End Sub

Mar 14 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Nach wrote:
I am new to .net can someone teranslate the foloowing in .net
Ls and Lt will be listboxes.
Sub ListCpSelected(Ls As Control, Lt As Control)
'Copies selected items from one list to another
Dim I As Integer
I = 0
While (I < Ls.ListCount) --- I beleive is - listbox.selectedindices.count
If Ls.Selected(I) Then I am using if listbox.selecteditem(I) I get
Invalid cast Exception was unhandled
Lt.AddItem Ls.List(I)
I = I + 1
Wend
End Sub

SelectedItem is not an array, it returns an object. You may want to try
SelectedIndex, returns an integer, you can use that in the next line of
code.

T
Mar 14 '07 #2

P: n/a
Nach,

If you are trying to copy the selected items from one listbox to another
listbox, here is some code that might work for you:

Private Sub CopySelected(ByVal lst1 As ListBox, ByVal lst2 As ListBox)

Dim SelectedItems(lst1.SelectedItems.Count - 1) As Object

lst1.SelectedItems.CopyTo(SelectedItems, 0)
lst2.Items.AddRange(SelectedItems)

End Sub

Kerry Moorman
"Nach" wrote:
I am new to .net can someone teranslate the foloowing in .net
Ls and Lt will be listboxes.
Sub ListCpSelected(Ls As Control, Lt As Control)
'Copies selected items from one list to another
Dim I As Integer
I = 0
While (I < Ls.ListCount) --- I beleive is - listbox.selectedindices.count
If Ls.Selected(I) Then I am using if listbox.selecteditem(I) I get
Invalid cast Exception was unhandled
Lt.AddItem Ls.List(I)
I = I + 1
Wend
End Sub
Mar 14 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.