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

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
 
G

Guest

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
 
T

tomb

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
 

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