473,779 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array List as DataSource For LIstBox Bug?

The problem is this:

I have a list box. I set an array list as the datasource. I remove an item
from the array list. I set the listbox datasource to nothing. I set the
listbox datasource to the array list again to refresh. Click on an item in
the list, and an Indexing error comes up. (in non-user code, it is trying to
get an element from the array that is greater than the number of items in the
array.)

To reproduce:

1. Create a WindowsForm Project with the following code.
2. Run the Project
3. Click on the third item in the list.
4. Click the Button (after the delete, the 'Four' item is selected)
5. Click the Button again. (after the delete, nothing is selected, but...)
6. Click on either item in the list - Error is generated.

HELP!

Thanks.

---------------------CODE to reproduce--------------------
Public Class Form1
Inherits System.Windows. Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents ListBox1 As System.Windows. Forms.ListBox
Friend WithEvents Button1 As System.Windows. Forms.Button
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.ListBox1 = New System.Windows. Forms.ListBox
Me.Button1 = New System.Windows. Forms.Button
Me.SuspendLayou t()
'
'ListBox1
'
Me.ListBox1.Doc k = System.Windows. Forms.DockStyle .Top
Me.ListBox1.Loc ation = New System.Drawing. Point(0, 0)
Me.ListBox1.Nam e = "ListBox1"
Me.ListBox1.Siz e = New System.Drawing. Size(304, 251)
Me.ListBox1.Tab Index = 0
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(208, 264)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing. Size(72, 24)
Me.Button1.TabI ndex = 1
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(304, 300)
Me.Controls.Add (Me.Button1)
Me.Controls.Add (Me.ListBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout (False)

End Sub

#End Region

Private MyArrayList As New ArrayList

Private Class ItemClass
Implements IComparable
Private myID As Integer
Public Property ID() As Integer
Get
Return myID
End Get
Set(ByVal Value As Integer)
myID = Value
End Set
End Property
Private myValue As String
Public Property Value() As String
Get
Return myValue
End Get
Set(ByVal Value As String)
myValue = Value
End Set
End Property

Public Function CompareTo(ByVal obj As Object) As Integer Implements
System.ICompara ble.CompareTo
If TypeOf obj Is ItemClass Then
If DirectCast(obj, ItemClass).ID > Me.ID Then Return -1
If DirectCast(obj, ItemClass).ID < Me.ID Then Return 1
Return 0
End If
End Function
End Class

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim A As New ItemClass
A.ID = 1
A.Value = "One"
MyArrayList.Add (A)
A = New ItemClass
A.ID = 2
A.Value = "Two"
MyArrayList.Add (A)
A = New ItemClass
A.ID = 3
A.Value = "Three"
MyArrayList.Add (A)
A = New ItemClass
A.ID = 4
A.Value = "Four"
MyArrayList.Add (A)
MyArrayList.Sor t()

Me.ListBox1.Dat aSource = MyArrayList
Me.ListBox1.Val ueMember = "ID"
Me.ListBox1.Dis playMember = "Value"
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
MyArrayList.Rem oveAt(2)
MyArrayList.Sor t()

Me.ListBox1.Dat aSource = Nothing

Me.ListBox1.Dat aSource = MyArrayList
Me.ListBox1.Val ueMember = "ID"
Me.ListBox1.Dis playMember = "Value"
End Sub
End Class

--
--Zorpie
Nov 21 '05 #1
11 8556
Hi Zorpie,

Did you try the listbox's refresh method?

HTH,

Bernie Yaeger

"Zorpiedoma n" <no*********@be atles.com> wrote in message
news:8E******** *************** ***********@mic rosoft.com...
The problem is this:

I have a list box. I set an array list as the datasource. I remove an
item
from the array list. I set the listbox datasource to nothing. I set the
listbox datasource to the array list again to refresh. Click on an item
in
the list, and an Indexing error comes up. (in non-user code, it is trying
to
get an element from the array that is greater than the number of items in
the
array.)

To reproduce:

1. Create a WindowsForm Project with the following code.
2. Run the Project
3. Click on the third item in the list.
4. Click the Button (after the delete, the 'Four' item is selected)
5. Click the Button again. (after the delete, nothing is selected,
but...)
6. Click on either item in the list - Error is generated.

HELP!

Thanks.

---------------------CODE to reproduce--------------------
Public Class Form1
Inherits System.Windows. Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents ListBox1 As System.Windows. Forms.ListBox
Friend WithEvents Button1 As System.Windows. Forms.Button
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.ListBox1 = New System.Windows. Forms.ListBox
Me.Button1 = New System.Windows. Forms.Button
Me.SuspendLayou t()
'
'ListBox1
'
Me.ListBox1.Doc k = System.Windows. Forms.DockStyle .Top
Me.ListBox1.Loc ation = New System.Drawing. Point(0, 0)
Me.ListBox1.Nam e = "ListBox1"
Me.ListBox1.Siz e = New System.Drawing. Size(304, 251)
Me.ListBox1.Tab Index = 0
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(208, 264)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing. Size(72, 24)
Me.Button1.TabI ndex = 1
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(304, 300)
Me.Controls.Add (Me.Button1)
Me.Controls.Add (Me.ListBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout (False)

End Sub

#End Region

Private MyArrayList As New ArrayList

Private Class ItemClass
Implements IComparable
Private myID As Integer
Public Property ID() As Integer
Get
Return myID
End Get
Set(ByVal Value As Integer)
myID = Value
End Set
End Property
Private myValue As String
Public Property Value() As String
Get
Return myValue
End Get
Set(ByVal Value As String)
myValue = Value
End Set
End Property

Public Function CompareTo(ByVal obj As Object) As Integer
Implements
System.ICompara ble.CompareTo
If TypeOf obj Is ItemClass Then
If DirectCast(obj, ItemClass).ID > Me.ID Then Return -1
If DirectCast(obj, ItemClass).ID < Me.ID Then Return 1
Return 0
End If
End Function
End Class

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim A As New ItemClass
A.ID = 1
A.Value = "One"
MyArrayList.Add (A)
A = New ItemClass
A.ID = 2
A.Value = "Two"
MyArrayList.Add (A)
A = New ItemClass
A.ID = 3
A.Value = "Three"
MyArrayList.Add (A)
A = New ItemClass
A.ID = 4
A.Value = "Four"
MyArrayList.Add (A)
MyArrayList.Sor t()

Me.ListBox1.Dat aSource = MyArrayList
Me.ListBox1.Val ueMember = "ID"
Me.ListBox1.Dis playMember = "Value"
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
MyArrayList.Rem oveAt(2)
MyArrayList.Sor t()

Me.ListBox1.Dat aSource = Nothing

Me.ListBox1.Dat aSource = MyArrayList
Me.ListBox1.Val ueMember = "ID"
Me.ListBox1.Dis playMember = "Value"
End Sub
End Class

--
--Zorpie

Nov 21 '05 #2
That does not refresh the data. The work-around so far is to Set the
SelectedIndex to -1 BEFORE removing the item from the arraylist. Then it
works fine, but this may not always be possible in real life.

-zorpy

"Bernie Yaeger" wrote:
Hi Zorpie,

Did you try the listbox's refresh method?

HTH,

Bernie Yaeger

Nov 21 '05 #3

"Zorpiedoma n" <no*********@be atles.com> wrote
The problem is this:

I have a list box. I set an array list as the datasource. I remove an item
from the array list. I set the listbox datasource to nothing. I set the
listbox datasource to the array list again to refresh. Click on an item in
the list, and an Indexing error comes up.
Everywhere you have
Me.ListBox1.Dat aSource = MyArrayList
Use:
Me.ListBox1.Dat aSource = MyArrayList.Clo ne

HTH
LFS

Nov 21 '05 #4

"Dennis" <De****@discuss ions.microsoft. com> wrote
Interesting! What is the logic (why does this work)? Any idea?


It may be that binding is an expensive operation such that they
included a test to see if the new object is the same as the old
object, and return early if they were the same.

So even though you changed the array, you assign the same
arraylist back to the DataSource property, which gets detected
as the same object as before. Since its the same object, they
skip tring to bind to it, because it is already bound to...

That is only my speculation.
LFS
Nov 21 '05 #5
Thanks Larry. It would be like Microsoft to think they know better than us
what we want to do.

"Larry Serflaten" wrote:

"Dennis" <De****@discuss ions.microsoft. com> wrote
Interesting! What is the logic (why does this work)? Any idea?


It may be that binding is an expensive operation such that they
included a test to see if the new object is the same as the old
object, and return early if they were the same.

So even though you changed the array, you assign the same
arraylist back to the DataSource property, which gets detected
as the same object as before. Since its the same object, they
skip tring to bind to it, because it is already bound to...

That is only my speculation.
LFS

Nov 21 '05 #6
However, my setting .datasource = nothing, then re-setting it to the array
list does the same thing, so this does not fix the problem.
Nov 21 '05 #7
This is a rather costly solution... also, if any docking is being used, you
have just changed the z-order of the control and it could appear in an
unwanted place on the form. why do you think this would change anything
anyway?

"Cor Ligthert" wrote:
Zorpiedoman,

In my opinion is this as you wrote a bug, try this
\\\
MyArrayList.Rem oveAt(2)
MyArrayList.Sor t()
Me.Controls.Rem ove(ListBox1)
Me.Controls.Add (ListBox1)
///
Than it works as it should work in my opinion without that remove and add.

I hope this gives an idea

Cor

Nov 21 '05 #8
> This is a rather costly solution... also, if any docking is being used,
you
have just changed the z-order of the control and it could appear in an
unwanted place on the form. why do you think this would change anything
anyway?


Because I tried it.

Cor
Nov 21 '05 #9
Do you see my point, however, that this is not a viable solution?

"Cor Ligthert" wrote:
This is a rather costly solution... also, if any docking is being used,
you
have just changed the z-order of the control and it could appear in an
unwanted place on the form. why do you think this would change anything
anyway?


Because I tried it.

Cor

Nov 21 '05 #10

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

Similar topics

1
1075
by: ad | last post by:
Is it necessary to use dataset as the datasource of a listbox? can I use an array as the datasource of a ListBox?
2
1959
by: Murt | last post by:
if i perform a sort on an array, how do i get the sorted array to be displayed in a listbox? thanks murt
3
1707
by: SStory | last post by:
For an owner drawn list box, I have a collection that represents some graphics objects in my app. I inherited the collection class from collectionbase. It would be nice to somehow just set the lstbox.datasource=mycollection and have it work, but this seemed to never work so I just run through my collection and popualate the list. This is a real waste because the collection contains classes that have one property that is a bitmap. ...
4
23440
by: Jim Shaffer | last post by:
Perhaps I have the wrong construct, or misunderstand arrays in vb (2003).... I've loaded a two-dimensional array (168 by 28) into memory as AcctArray. {Dim AcctArray (500,28) as string...} The AcctArray is loaded from a Quickbooks table, so there's no intrinsic dataset to assign as datasource.... I want to have a combo table (or list table or whatever), currently named cbAccounts, that can scroll through and maybe select items in the...
1
2780
by: A. Spiehler | last post by:
I'm trying to fill a listBox control with string members from an array of objects. I think using data binding is supposed to be the easiest way to do this. I've never used data binding before and am having trouble getting it to do anything. The relevant code is below, followed by a better explanation of what I'm trying to do. // pseudocode start public delegate string StepMethod();
6
2341
by: Paul | last post by:
Hi All, Framework 1.1 listbox control unable to DataBind I've been googling for an answer to this query that appears quite a lot, but none, it seem, answers my problem directly. I am populating a listbox with an array of very simple "Country" objects via a WebService. The Country class contains CountryID,TLDs and Name properties each of which have a getter and setter (I found that properties without
1
1516
by: pauled | last post by:
Hello all, Framework 1.1 VS 2003 Binding listbox. I have an array of objects that I am trying to use as the datasource for a listbox. The array is returned from a webservice and seems to be populated ok, see this text from my Autos pane in VS, once the datasource property has been set. - DataSource {System.Array} System.Object
2
1610
by: Ian Semmel | last post by:
If I have a List<and a ListBox.DataSource = List then it initially displays OK, but if I change the order of the List, the displayed text in the ListBox doesn't. How do you make this happen ?
3
2555
by: rn5a | last post by:
I am not very sure whether I should have continued with my earlier thread or started a new thread whose subject matter was somewhat similar to the subject matter in this thread. Anyway after much deliberation, I decided to start a new thread. So here it is. Pardon me if I should have continued with my earlier thread instead of starting this new thread. A ListBox lists all the directories & files existing in a directory on the server....
0
9474
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
10305
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10137
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
10074
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
8959
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
7483
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
6724
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();...
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.