473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Error Using SelectedIndicesof Listbox with custom objects

I'd already posted this in microsoft.public.dotnet.framework.windowsforms
and microsoft.public.dotnet.framework.windowsforms.con trols 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.SelectedIndices.Count - 1
lstItem =
CType(lstAvailable.Items.Item(lstAvailable.Selecte dIndices(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)

Also, when I use:
Dim lst As SwapListBoxItem
For Each lst In lstAvailable.SelectedItems
lstItem.Selected = True
Next

The lstAvailable.SelectedItems.Count 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.Items
lstItem.Selected = 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_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******
Nov 20 '05 #1
14 1239
Remember, you are returning a collection of indicies so you should use the
follwing to access a particular element

ListBox1.SelectedIndicies.Item( numOfElement )

OHM

"Alex Stevens" <Al**********************@gcc.co.uk> wrote in message
news:eU**************@TK2MSFTNGP09.phx.gbl...
I'd already posted this in microsoft.public.dotnet.framework.windowsforms
and microsoft.public.dotnet.framework.windowsforms.con trols 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.SelectedIndices.Count - 1
lstItem =
CType(lstAvailable.Items.Item(lstAvailable.Selecte dIndices(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)

Also, when I use:
Dim lst As SwapListBoxItem
For Each lst In lstAvailable.SelectedItems
lstItem.Selected = True
Next

The lstAvailable.SelectedItems.Count 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.Items
lstItem.Selected = 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_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******

Nov 20 '05 #2
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!!!
"One Handed Man ( OHM#)" <news.microsoft.com> wrote in message
news:ug****************@TK2MSFTNGP10.phx.gbl...
Remember, you are returning a collection of indicies so you should use the
follwing to access a particular element

ListBox1.SelectedIndicies.Item( numOfElement )

OHM

"Alex Stevens" <Al**********************@gcc.co.uk> wrote in message
news:eU**************@TK2MSFTNGP09.phx.gbl...
I'd already posted this in microsoft.public.dotnet.framework.windowsforms and microsoft.public.dotnet.framework.windowsforms.con trols 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.SelectedIndices.Count - 1
lstItem =
CType(lstAvailable.Items.Item(lstAvailable.Selecte dIndices(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)

Also, when I use:
Dim lst As SwapListBoxItem
For Each lst In lstAvailable.SelectedItems
lstItem.Selected = True
Next

The lstAvailable.SelectedItems.Count 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.Items
lstItem.Selected = 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_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******


Nov 20 '05 #3
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" <Al**********************@gcc.co.uk> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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!!!
"One Handed Man ( OHM#)" <news.microsoft.com> wrote in message
news:ug****************@TK2MSFTNGP10.phx.gbl...
Remember, you are returning a collection of indicies so you should use the
follwing to access a particular element

ListBox1.SelectedIndicies.Item( numOfElement )

OHM

"Alex Stevens" <Al**********************@gcc.co.uk> wrote in message
news:eU**************@TK2MSFTNGP09.phx.gbl...
I'd already posted this in
microsoft.public.dotnet.framework.windowsforms and microsoft.public.dotnet.framework.windowsforms.con trols 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.SelectedIndices.Count - 1
lstItem =
CType(lstAvailable.Items.Item(lstAvailable.Selecte dIndices(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)

Also, when I use:
Dim lst As SwapListBoxItem
For Each lst In lstAvailable.SelectedItems
lstItem.Selected = True
Next

The lstAvailable.SelectedItems.Count 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.Items
lstItem.Selected = 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_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******



Nov 20 '05 #4
* "Alex Stevens" <Al**********************@gcc.co.uk> scripsit:
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.Selecte dIndices(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
There is no 'SelectedIndices(4)' if count is 4. But the loop above
should only run up to index 3.
Also, when I use:
Dim lst As SwapListBoxItem
For Each lst In lstAvailable.SelectedItems
lstItem.Selected = True
Next


The code won't compile because there is no 'lstItem' variable...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5
Hi Herfried,

Thats not the issue, look at my last post on the subject.

Regards - OHM ( One Handed Man - Terry Burns )
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2j************@uni-berlin.de...
* "Alex Stevens" <Al**********************@gcc.co.uk> scripsit:
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.Selecte dIndices(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


There is no 'SelectedIndices(4)' if count is 4. But the loop above
should only run up to index 3.
Also, when I use:
Dim lst As SwapListBoxItem
For Each lst In lstAvailable.SelectedItems
lstItem.Selected = True
Next


The code won't compile because there is no 'lstItem' variable...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #6
Herfried,
The code won't compile because there is no 'lstItem' variable...


Have a look at this page

http://msdn.microsoft.com/library/de...sercontrol.asp

I hope this helps?

:-))

Cor
Nov 20 '05 #7
:)
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:Oj**************@TK2MSFTNGP11.phx.gbl...
Herfried,
The code won't compile because there is no 'lstItem' variable...
Have a look at this page

http://msdn.microsoft.com/library/de...sercontrol.asp
I hope this helps?

:-))

Cor

Nov 20 '05 #8
* "Cor Ligthert" <no**********@planet.nl> scripsit:
The code won't compile because there is no 'lstItem' variable...


Have a look at this page

http://msdn.microsoft.com/library/de...sercontrol.asp

I hope this helps?


It doesn't. I assume there is a typo in the OP's post...

;-)

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #9
Trust me there is no error in the OP post. I duplicated this myself. See my
post, and try it yourself, you will be suprised.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2j************@uni-berlin.de...
* "Cor Ligthert" <no**********@planet.nl> scripsit:
The code won't compile because there is no 'lstItem' variable...


Have a look at this page

http://msdn.microsoft.com/library/de...sercontrol.asp
I hope this helps?


It doesn't. I assume there is a typo in the OP's post...

;-)

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #10
* "One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> scripsit:
Trust me there is no error in the OP post. I duplicated this myself. See my
post, and try it yourself, you will be suprised.


I suggest you have a look at the OP's code and what I have written...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #11
I have and you still have not tried it have you ?

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2j************@uni-berlin.de...
* "One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> scripsit:
Trust me there is no error in the OP post. I duplicated this myself. See my post, and try it yourself, you will be suprised.


I suggest you have a look at the OP's code and what I have written...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #12
Hi Terry,

Forget it, we both know Herfried, young, brilliant, has still a lot to
learn, however we like him.

:-)

Cor
Nov 20 '05 #13
* "One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> scripsit:
I have and you still have not tried it have you ?


I never said that your suggestion is wrong...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #14
I wish I could be an MVP just like H.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:uB*************@tk2msftngp13.phx.gbl...
Hi Terry,

Forget it, we both know Herfried, young, brilliant, has still a lot to
learn, however we like him.

:-)

Cor

Nov 20 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex Stevens | last post by:
I'd already posted this in microsoft.public.dotnet.framework.windowsforms and microsoft.public.dotnet.framework.windowsforms.controls to no avail so apologies for the cross-posting. Hi, I'm...
0
by: Jason Callas | last post by:
I have a weird problem with a sorted ListBox which is bound to an ArrayList. My ArrayList is holding a collection of custom objects. One of the public properties is called Name. When I add a few...
2
by: Steve Jorgensen | last post by:
When writing VB or VBA code that works with databases or other external libraries that cannot be trusted to automatically do the right thing when references to their objects are arbitrarily...
2
by: Dave Rudolf | last post by:
Hi all, I have a System.Windows.Forms.ListBox object that I am putting objects into. These objects are of custom types that I am defining. The list box displays each object by its class name --...
1
by: Claire | last post by:
Is it possible to associate data/objects for each item in a list or combobox please (as you could with the objects member for tstrings in delphi) thanks
1
by: Patty O'Dors | last post by:
Hi I have some code to create an ownerdrawn listbox (derived), and when I add an item to it, the bold text of the first item (the title, 'Collections and Maturities') mysteriously seems to get...
1
by: MrNobody | last post by:
Hi, I'm doing something where I add custom objects to a ListBox which have aToString() method overriden so it displays what I want. When adding instances of these custom objects to the ListBox I...
0
by: Mark Smith | last post by:
hi i use an ownerdraw method for coloring some items in the list different then the others. code: private void ListBoxDrawItem(object sender, DrawItemEventArgs e) { ListBox lst =...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.