473,761 Members | 4,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error Using SelectedIndices of 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******
Jul 21 '05 #1
3 2417
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" <Al************ **********@gcc. co.uk> wrote in message
news:eU******** ******@TK2MSFTN GP09.phx.gbl...
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******

Jul 21 '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.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******** ********@TK2MSF TNGP10.phx.gbl. ..
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" <Al************ **********@gcc. co.uk> wrote in message
news:eU******** ******@TK2MSFTN GP09.phx.gbl...
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******


Jul 21 '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******** *******@TK2MSFT NGP10.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.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******** ********@TK2MSF TNGP10.phx.gbl. ..
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" <Al************ **********@gcc. co.uk> wrote in message
news:eU******** ******@TK2MSFTN GP09.phx.gbl...
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******



Jul 21 '05 #4

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

Similar topics

0
1316
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 objects to the list and then bind the ArrayList to the ListBox (and after setting the DisplayMemeber property to "Name") the names show up like that are supposed to. If I then add a new element to the ArrayList and call the Refresh method of the...
2
3028
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 released, some thought must be put into how to make sure the objects will all be closed and released in the correct order, even in the result of an error. This requirement can make our code really ugly, even following the best of commonly known best...
2
5801
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 -- not exactly usable :). So what I would like is to control the text that is displayed. Apparently, overridding the ToString method does not work. So what is the trick? I know that in Java, you can simply create a custom renderer that will...
1
1831
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
2892
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 bunched up at the right, i.e. squashed up! any idea why? The main bit of the code is as such // (in progressReporter.cs) protected struct LBRow //a row of the listbox, whether it be the title or a
1
2706
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 also put the same references in a Hashtable so my program can obtain and modify these objects without iterating through entire ListBox looking for what I want. The problem is though, when I update these objects the changes are not being reflected...
14
1278
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 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
2
19488
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 will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
0
2897
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 will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
0
9522
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9336
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9948
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9902
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8770
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.