473,789 Members | 2,740 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
11 8557
Zorpiedoman,

I told that it is in my opinion as well a bug, however I don't see your
problem. Do you really manage the order of the controls in the control
collection, I never saw that somebody do (except for the tabcontrol).

This means removing an reference of an object and adding it again, maybe it
does not even repaint, however my eyes are not that fast, maybe you can see
it..(This is as well used to overcome the tabcontrol bug by the way)

And with that not saying that it is a nice solution.

Cor
Nov 21 '05 #11

"Zorpiedoma n" <no*********@be atles.com> wrote
However, my setting .datasource = nothing, then re-setting it to the array
list does the same thing, so this does not fix the problem.


Add a debug statement to the DataSourceChang ed event and try them
both again. If that event does not fire, then the datasource has not changed
and no refresh of the data happens. You'll note that using the Clone, as
suggested, does cause that event to happen....

LFS
Nov 21 '05 #12

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
23445
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
2343
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
1517
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
10412
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...
1
10142
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,...
1
7529
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
6769
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
5422
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...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
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
3703
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
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.