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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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 said:
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
 
Back
Top