Error Using SelectedIndicesof Listbox with custom objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Stevens

    #1

    Error Using SelectedIndicesof Listbox with custom objects

    I'd already posted this in microsoft.publi c.dotnet.framew ork.windowsform s
    and microsoft.publi c.dotnet.framew ork.windowsform s.controls to no avail so
    apologies for the cross-posting.

    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.Se lectedIndices.C ount - 1
    lstItem =
    CType(lstAvaila ble.Items.Item( lstAvailable.Se lectedIndices(i )),
    SwapListBoxItem )
    lstItem.Selecte d = True
    intIndex = i
    Next

    Before execution gets to this, the lstAvailable.Se lectedIndices.c ount
    returns 4.
    Happy Days.

    However when I try to evaluate lstAvailable.Se lectedIndices(0 ) or
    lstAvailable.Se lectedIndices(1 ) or lstAvailable.Se lectedIndices(2 ) or
    lstAvailable.Se lectedIndices(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.It ems.Add(SwapIte m)

    Also, when I use:
    Dim lst As SwapListBoxItem
    For Each lst In lstAvailable.Se lectedItems
    lstItem.Selecte d = True
    Next

    The lstAvailable.Se lectedItems.Cou nt returns 4, but the execution doesn't
    iterate, just jumps over the For statement.
    However, I can use this and it iterates correctly:

    Dim lstItem As SwapListBoxItem
    For Each lstItem In lstSelected.Ite ms
    lstItem.Selecte d = True
    Next

    So it seems that the items collection can iterate my customer objects but
    the selecteditems cannot????

    Thanks

    Alex

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

    Private m_objValueMembe r As Object = ""
    Private m_strDisplayMem ber As String = ""
    Private m_bolSelected As Boolean = False

    Public Property ValueMember() As Object
    Get
    Return m_objValueMembe r
    End Get
    Set(ByVal ValueMember As Object)
    m_objValueMembe r = ValueMember
    End Set
    End Property

    Public Property DisplayMember() As String
    Get
    Return m_strDisplayMem ber
    End Get
    Set(ByVal DisplayMember As String)
    m_strDisplayMem ber = 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.DisplayMembe r
    End Function

    End Class

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


  • One Handed Man \( OHM#\)

    #2
    Re: Error Using SelectedIndices of Listbox with custom objects

    Remember, you are returning a collection of indicies so you should use the
    follwing to access a particular element

    ListBox1.Select edIndicies.Item ( numOfElement )

    OHM

    "Alex Stevens" <AlexStevens_NO SPAMPLEASE@gcc. co.uk> wrote in message
    news:eUxgXQ5TEH A.2448@TK2MSFTN GP09.phx.gbl...[color=blue]
    > I'd already posted this in microsoft.publi c.dotnet.framew ork.windowsform s
    > and microsoft.publi c.dotnet.framew ork.windowsform s.controls to no avail so
    > apologies for the cross-posting.
    >
    > 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[/color]
    simply[color=blue]
    > 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.Se lectedIndices.C ount - 1
    > lstItem =
    > CType(lstAvaila ble.Items.Item( lstAvailable.Se lectedIndices(i )),
    > SwapListBoxItem )
    > lstItem.Selecte d = True
    > intIndex = i
    > Next
    >
    > Before execution gets to this, the lstAvailable.Se lectedIndices.c ount
    > returns 4.
    > Happy Days.
    >
    > However when I try to evaluate lstAvailable.Se lectedIndices(0 ) or
    > lstAvailable.Se lectedIndices(1 ) or lstAvailable.Se lectedIndices(2 ) or
    > lstAvailable.Se lectedIndices(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[/color]
    posted[color=blue]
    > the custom object code below for reference)??
    >
    > Once the custom object is instantiated and then populated I use this line[/color]
    to[color=blue]
    > add it to the listbox:
    > lstAvailable.It ems.Add(SwapIte m)
    >
    > Also, when I use:
    > Dim lst As SwapListBoxItem
    > For Each lst In lstAvailable.Se lectedItems
    > lstItem.Selecte d = True
    > Next
    >
    > The lstAvailable.Se lectedItems.Cou nt returns 4, but the execution doesn't
    > iterate, just jumps over the For statement.
    > However, I can use this and it iterates correctly:
    >
    > Dim lstItem As SwapListBoxItem
    > For Each lstItem In lstSelected.Ite ms
    > lstItem.Selecte d = True
    > Next
    >
    > So it seems that the items collection can iterate my customer objects but
    > the selecteditems cannot????
    >
    > Thanks
    >
    > Alex
    >
    > *****Custom Object Code Start******
    > Public Class SwapListBoxItem
    >
    > Private m_objValueMembe r As Object = ""
    > Private m_strDisplayMem ber As String = ""
    > Private m_bolSelected As Boolean = False
    >
    > Public Property ValueMember() As Object
    > Get
    > Return m_objValueMembe r
    > End Get
    > Set(ByVal ValueMember As Object)
    > m_objValueMembe r = ValueMember
    > End Set
    > End Property
    >
    > Public Property DisplayMember() As String
    > Get
    > Return m_strDisplayMem ber
    > End Get
    > Set(ByVal DisplayMember As String)
    > m_strDisplayMem ber = 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.DisplayMembe r
    > End Function
    >
    > End Class
    >
    > *****Custom Object Code End******
    >
    >[/color]


    Comment

    • Alex Stevens

      #3
      Re: Error Using SelectedIndices of Listbox with custom objects

      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.Se lectedIndices(i ) works just the same as
      lstAvailable.Se lectedIndices.I tem(i)

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


      "One Handed Man ( OHM#)" <news.microsoft .com> wrote in message
      news:ug%2377l5T EHA.2464@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > Remember, you are returning a collection of indicies so you should use the
      > follwing to access a particular element
      >
      > ListBox1.Select edIndicies.Item ( numOfElement )
      >
      > OHM
      >
      > "Alex Stevens" <AlexStevens_NO SPAMPLEASE@gcc. co.uk> wrote in message
      > news:eUxgXQ5TEH A.2448@TK2MSFTN GP09.phx.gbl...[color=green]
      > > I'd already posted this in[/color][/color]
      microsoft.publi c.dotnet.framew ork.windowsform s[color=blue][color=green]
      > > and microsoft.publi c.dotnet.framew ork.windowsform s.controls to no avail[/color][/color]
      so[color=blue][color=green]
      > > apologies for the cross-posting.
      > >
      > > Hi,
      > >
      > > I'm writing a usercontrol which displays the typical two listboxes and[/color][/color]
      the[color=blue][color=green]
      > > ability to move items from one to the other.
      > >
      > > The listboxes are populated with my custom objects (SwapItem), which[/color]
      > simply[color=green]
      > > 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.Se lectedIndices.C ount - 1
      > > lstItem =
      > > CType(lstAvaila ble.Items.Item( lstAvailable.Se lectedIndices(i )),
      > > SwapListBoxItem )
      > > lstItem.Selecte d = True
      > > intIndex = i
      > > Next
      > >
      > > Before execution gets to this, the lstAvailable.Se lectedIndices.c ount
      > > returns 4.
      > > Happy Days.
      > >
      > > However when I try to evaluate lstAvailable.Se lectedIndices(0 ) or
      > > lstAvailable.Se lectedIndices(1 ) or lstAvailable.Se lectedIndices(2 ) or
      > > lstAvailable.Se lectedIndices(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[/color]
      > posted[color=green]
      > > the custom object code below for reference)??
      > >
      > > Once the custom object is instantiated and then populated I use this[/color][/color]
      line[color=blue]
      > to[color=green]
      > > add it to the listbox:
      > > lstAvailable.It ems.Add(SwapIte m)
      > >
      > > Also, when I use:
      > > Dim lst As SwapListBoxItem
      > > For Each lst In lstAvailable.Se lectedItems
      > > lstItem.Selecte d = True
      > > Next
      > >
      > > The lstAvailable.Se lectedItems.Cou nt returns 4, but the execution[/color][/color]
      doesn't[color=blue][color=green]
      > > iterate, just jumps over the For statement.
      > > However, I can use this and it iterates correctly:
      > >
      > > Dim lstItem As SwapListBoxItem
      > > For Each lstItem In lstSelected.Ite ms
      > > lstItem.Selecte d = True
      > > Next
      > >
      > > So it seems that the items collection can iterate my customer objects[/color][/color]
      but[color=blue][color=green]
      > > the selecteditems cannot????
      > >
      > > Thanks
      > >
      > > Alex
      > >
      > > *****Custom Object Code Start******
      > > Public Class SwapListBoxItem
      > >
      > > Private m_objValueMembe r As Object = ""
      > > Private m_strDisplayMem ber As String = ""
      > > Private m_bolSelected As Boolean = False
      > >
      > > Public Property ValueMember() As Object
      > > Get
      > > Return m_objValueMembe r
      > > End Get
      > > Set(ByVal ValueMember As Object)
      > > m_objValueMembe r = ValueMember
      > > End Set
      > > End Property
      > >
      > > Public Property DisplayMember() As String
      > > Get
      > > Return m_strDisplayMem ber
      > > End Get
      > > Set(ByVal DisplayMember As String)
      > > m_strDisplayMem ber = 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.DisplayMembe r
      > > End Function
      > >
      > > End Class
      > >
      > > *****Custom Object Code End******
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • One Handed Man \( OHM#\)

        #4
        Re: Error Using SelectedIndices of Listbox with custom objects

        Actually, I tested this and got the same error, it was only when I used
        ..Item that it worked consistantly, I found Once I had encountered the error
        once, I ket getting it by omitting the .Item

        One would think that Item would be the default property to enable you to use
        parenthasis directly after the object, but I had to consistently use item
        for good results.

        Regards - OHM


        "Alex Stevens" <AlexStevens_NO SPAMPLEASE@gcc. co.uk> wrote in message
        news:%23Deh185T EHA.204@TK2MSFT NGP10.phx.gbl.. .[color=blue]
        > 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.Se lectedIndices(i ) works just the same as
        > lstAvailable.Se lectedIndices.I tem(i)
        >
        > I wish I could invoice MS for what was a day or so of my time!!!
        >
        >
        > "One Handed Man ( OHM#)" <news.microsoft .com> wrote in message
        > news:ug%2377l5T EHA.2464@TK2MSF TNGP10.phx.gbl. ..[color=green]
        > > Remember, you are returning a collection of indicies so you should use[/color][/color]
        the[color=blue][color=green]
        > > follwing to access a particular element
        > >
        > > ListBox1.Select edIndicies.Item ( numOfElement )
        > >
        > > OHM
        > >
        > > "Alex Stevens" <AlexStevens_NO SPAMPLEASE@gcc. co.uk> wrote in message
        > > news:eUxgXQ5TEH A.2448@TK2MSFTN GP09.phx.gbl...[color=darkred]
        > > > I'd already posted this in[/color][/color]
        > microsoft.publi c.dotnet.framew ork.windowsform s[color=green][color=darkred]
        > > > and microsoft.publi c.dotnet.framew ork.windowsform s.controls to no[/color][/color][/color]
        avail[color=blue]
        > so[color=green][color=darkred]
        > > > apologies for the cross-posting.
        > > >
        > > > Hi,
        > > >
        > > > I'm writing a usercontrol which displays the typical two listboxes and[/color][/color]
        > the[color=green][color=darkred]
        > > > ability to move items from one to the other.
        > > >
        > > > The listboxes are populated with my custom objects (SwapItem), which[/color]
        > > simply[color=darkred]
        > > > 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.Se lectedIndices.C ount - 1
        > > > lstItem =
        > > > CType(lstAvaila ble.Items.Item( lstAvailable.Se lectedIndices(i )),
        > > > SwapListBoxItem )
        > > > lstItem.Selecte d = True
        > > > intIndex = i
        > > > Next
        > > >
        > > > Before execution gets to this, the lstAvailable.Se lectedIndices.c ount
        > > > returns 4.
        > > > Happy Days.
        > > >
        > > > However when I try to evaluate lstAvailable.Se lectedIndices(0 ) or
        > > > lstAvailable.Se lectedIndices(1 ) or lstAvailable.Se lectedIndices(2 ) or
        > > > lstAvailable.Se lectedIndices(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[/color]
        > > posted[color=darkred]
        > > > the custom object code below for reference)??
        > > >
        > > > Once the custom object is instantiated and then populated I use this[/color][/color]
        > line[color=green]
        > > to[color=darkred]
        > > > add it to the listbox:
        > > > lstAvailable.It ems.Add(SwapIte m)
        > > >
        > > > Also, when I use:
        > > > Dim lst As SwapListBoxItem
        > > > For Each lst In lstAvailable.Se lectedItems
        > > > lstItem.Selecte d = True
        > > > Next
        > > >
        > > > The lstAvailable.Se lectedItems.Cou nt returns 4, but the execution[/color][/color]
        > doesn't[color=green][color=darkred]
        > > > iterate, just jumps over the For statement.
        > > > However, I can use this and it iterates correctly:
        > > >
        > > > Dim lstItem As SwapListBoxItem
        > > > For Each lstItem In lstSelected.Ite ms
        > > > lstItem.Selecte d = True
        > > > Next
        > > >
        > > > So it seems that the items collection can iterate my customer objects[/color][/color]
        > but[color=green][color=darkred]
        > > > the selecteditems cannot????
        > > >
        > > > Thanks
        > > >
        > > > Alex
        > > >
        > > > *****Custom Object Code Start******
        > > > Public Class SwapListBoxItem
        > > >
        > > > Private m_objValueMembe r As Object = ""
        > > > Private m_strDisplayMem ber As String = ""
        > > > Private m_bolSelected As Boolean = False
        > > >
        > > > Public Property ValueMember() As Object
        > > > Get
        > > > Return m_objValueMembe r
        > > > End Get
        > > > Set(ByVal ValueMember As Object)
        > > > m_objValueMembe r = ValueMember
        > > > End Set
        > > > End Property
        > > >
        > > > Public Property DisplayMember() As String
        > > > Get
        > > > Return m_strDisplayMem ber
        > > > End Get
        > > > Set(ByVal DisplayMember As String)
        > > > m_strDisplayMem ber = 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.DisplayMembe r
        > > > End Function
        > > >
        > > > End Class
        > > >
        > > > *****Custom Object Code End******
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...