Error Using SelectedIndices of Listbox with custom objects

A

Alex Stevens

Hi,

I'm writing a usercontrol which displays the typical two listboxes and the ability to move items from one to the other.

The listboxes are populated with my custom objects (SwapItem), which simply have a ValueMember, DisplayMember a couple of other properties and the tostring function to display the DisplayMember in the listbox.

The issue I'm getting is with the SelectedIndices array
I selected four items in the listbox and then I use:

For i As Int32 = 0 To lstAvailable.SelectedIndices.Count - 1
lstItem = CType(lstAvailable.Items.Item(lstAvailable.SelectedIndices(i)), SwapListBoxItem)
lstItem.Selected = True
intIndex = i
Next

Before execution gets to this, the lstAvailable.SelectedIndices.count returns 4.
Happy Days.

However when I try to evaluate lstAvailable.SelectedIndices(0) or lstAvailable.SelectedIndices(1) or lstAvailable.SelectedIndices(2) or lstAvailable.SelectedIndices(4) i get the error "Index was outside the bounds of the array".
This can't be true as the.count is 4.

I get exactly the same results using the SelectedItems array to.
Is this a symptom of using my own custom object in the listbox (I've posted the custom object code below for reference)??

Once the custom object is instantiated and then populated I use this line to add it to the listbox:
lstAvailable.Items.Add(SwapItem)

Any ideas anyone

Thanks

Alex


*****Custom Object Code Start******
Public Class SwapListBoxItem

Private m_objValueMember As Object = ""
Private m_strDisplayMember As String = ""
Private m_bolSelected As Boolean = False

Public Property ValueMember() As Object
Get
Return m_objValueMember
End Get

Set(ByVal ValueMember As Object)

m_objValueMember = ValueMember

End Set

End Property

Public Property DisplayMember() As String

Get

Return m_strDisplayMember

End Get

Set(ByVal DisplayMember As String)

m_strDisplayMember = DisplayMember

End Set

End Property

Public Property Selected() As Boolean

Get

Return m_bolSelected

End Get

Set(ByVal Selected As Boolean)

m_bolSelected = Selected

End Set

End Property

Public Sub ChangeSelected()

m_bolSelected = Not m_bolSelected

End Sub

Public Overrides Function ToString() As String

Return Me.DisplayMember

End Function

End Class

*****Custom Object Code End******
 
A

Alex Stevens

In addition, when I use:
Dim lst As ListBoxItem

For Each lst In lstAvailable.SelectedItems
lstItem.Selected = True
intIndex = lstAvailable.SelectedIndices.Item(0)
Next

The lstAvailable.SelectedItems.Count returns 4, but the execution doesn't iterate, just jumps over the For statement.

Thanks

Alex

Hi,

I'm writing a usercontrol which displays the typical two listboxes and the ability to move items from one to the other.

The listboxes are populated with my custom objects (SwapItem), which simply have a ValueMember, DisplayMember a couple of other properties and the tostring function to display the DisplayMember in the listbox.

The issue I'm getting is with the SelectedIndices array
I selected four items in the listbox and then I use:

For i As Int32 = 0 To lstAvailable.SelectedIndices.Count - 1
lstItem = CType(lstAvailable.Items.Item(lstAvailable.SelectedIndices(i)), SwapListBoxItem)
lstItem.Selected = True
intIndex = i
Next

Before execution gets to this, the lstAvailable.SelectedIndices.count returns 4.
Happy Days.

However when I try to evaluate lstAvailable.SelectedIndices(0) or lstAvailable.SelectedIndices(1) or lstAvailable.SelectedIndices(2) or lstAvailable.SelectedIndices(4) i get the error "Index was outside the bounds of the array".
This can't be true as the.count is 4.

I get exactly the same results using the SelectedItems array to.
Is this a symptom of using my own custom object in the listbox (I've posted the custom object code below for reference)??

Once the custom object is instantiated and then populated I use this line to add it to the listbox:
lstAvailable.Items.Add(SwapItem)

Any ideas anyone

Thanks

Alex


*****Custom Object Code Start******
Public Class SwapListBoxItem

Private m_objValueMember As Object = ""
Private m_strDisplayMember As String = ""
Private m_bolSelected As Boolean = False

Public Property ValueMember() As Object
Get
Return m_objValueMember
End Get

Set(ByVal ValueMember As Object)

m_objValueMember = ValueMember

End Set

End Property

Public Property DisplayMember() As String

Get

Return m_strDisplayMember

End Get

Set(ByVal DisplayMember As String)

m_strDisplayMember = DisplayMember

End Set

End Property

Public Property Selected() As Boolean

Get

Return m_bolSelected

End Get

Set(ByVal Selected As Boolean)

m_bolSelected = Selected

End Set

End Property

Public Sub ChangeSelected()

m_bolSelected = Not m_bolSelected

End Sub

Public Overrides Function ToString() As String

Return Me.DisplayMember

End Function

End Class

*****Custom Object Code End******
 
A

Alex Stevens

Well, that was irritating.......

I copied the code into a new project to attach to this message.
Added a new listbox to a new form and hooked up the code.

Guess what....it worked.

I then gradually worked back to the original form (which is actually a custom control), and ended up simply deleting the listbox on the control, and adding a new one and renaming it.

Guess what it started to work again.
(I wondered why there weren't many posting with the same syptoms).

Also, lstAvailable.SelectedIndices(i) works just the same as lstAvailable.SelectedIndices.Item(i)

I wish I could invoice MS for what was a day or so of my time!!!

In addition, when I use:
Dim lst As ListBoxItem

For Each lst In lstAvailable.SelectedItems
lstItem.Selected = True
intIndex = lstAvailable.SelectedIndices.Item(0)
Next

The lstAvailable.SelectedItems.Count returns 4, but the execution doesn't iterate, just jumps over the For statement.

Thanks

Alex

Hi,

I'm writing a usercontrol which displays the typical two listboxes and the ability to move items from one to the other.

The listboxes are populated with my custom objects (SwapItem), which simply have a ValueMember, DisplayMember a couple of other properties and the tostring function to display the DisplayMember in the listbox.

The issue I'm getting is with the SelectedIndices array
I selected four items in the listbox and then I use:

For i As Int32 = 0 To lstAvailable.SelectedIndices.Count - 1
lstItem = CType(lstAvailable.Items.Item(lstAvailable.SelectedIndices(i)), SwapListBoxItem)
lstItem.Selected = True
intIndex = i
Next

Before execution gets to this, the lstAvailable.SelectedIndices.count returns 4.
Happy Days.

However when I try to evaluate lstAvailable.SelectedIndices(0) or lstAvailable.SelectedIndices(1) or lstAvailable.SelectedIndices(2) or lstAvailable.SelectedIndices(4) i get the error "Index was outside the bounds of the array".
This can't be true as the.count is 4.

I get exactly the same results using the SelectedItems array to.
Is this a symptom of using my own custom object in the listbox (I've posted the custom object code below for reference)??

Once the custom object is instantiated and then populated I use this line to add it to the listbox:
lstAvailable.Items.Add(SwapItem)

Any ideas anyone

Thanks

Alex


*****Custom Object Code Start******
Public Class SwapListBoxItem

Private m_objValueMember As Object = ""
Private m_strDisplayMember As String = ""
Private m_bolSelected As Boolean = False

Public Property ValueMember() As Object
Get
Return m_objValueMember
End Get

Set(ByVal ValueMember As Object)

m_objValueMember = ValueMember

End Set

End Property

Public Property DisplayMember() As String

Get

Return m_strDisplayMember

End Get

Set(ByVal DisplayMember As String)

m_strDisplayMember = DisplayMember

End Set

End Property

Public Property Selected() As Boolean

Get

Return m_bolSelected

End Get

Set(ByVal Selected As Boolean)

m_bolSelected = Selected

End Set

End Property

Public Sub ChangeSelected()

m_bolSelected = Not m_bolSelected

End Sub

Public Overrides Function ToString() As String

Return Me.DisplayMember

End Function

End Class

*****Custom Object Code End******
 

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