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.
InitializeComponent()
'Add any initialization after the InitializeComponent() 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.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'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.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'ListBox1
'
Me.ListBox1.Dock = System.Windows.Forms.DockStyle.Top
Me.ListBox1.Location = New System.Drawing.Point(0, 0)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(304, 251)
Me.ListBox1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(208, 264)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(72, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = 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.IComparable.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(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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.Sort()
Me.ListBox1.DataSource = MyArrayList
Me.ListBox1.ValueMember = "ID"
Me.ListBox1.DisplayMember = "Value"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MyArrayList.RemoveAt(2)
MyArrayList.Sort()
Me.ListBox1.DataSource = Nothing
Me.ListBox1.DataSource = MyArrayList
Me.ListBox1.ValueMember = "ID"
Me.ListBox1.DisplayMember = "Value"
End Sub
End Class
--
--Zorpie 11 8432
Hi Zorpie,
Did you try the listbox's refresh method?
HTH,
Bernie Yaeger
"Zorpiedoman" <no*********@beatles.com> wrote in message
news:8E**********************************@microsof t.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. InitializeComponent()
'Add any initialization after the InitializeComponent() 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.Dispose() End If End If MyBase.Dispose(disposing) End Sub
'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer
'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.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ListBox1 = New System.Windows.Forms.ListBox Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout() ' 'ListBox1 ' Me.ListBox1.Dock = System.Windows.Forms.DockStyle.Top Me.ListBox1.Location = New System.Drawing.Point(0, 0) Me.ListBox1.Name = "ListBox1" Me.ListBox1.Size = New System.Drawing.Size(304, 251) Me.ListBox1.TabIndex = 0 ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(208, 264) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(72, 24) Me.Button1.TabIndex = 1 Me.Button1.Text = "Button1" ' 'Form1 ' Me.AutoScaleBaseSize = 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.IComparable.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(ByVal sender As System.Object, ByVal e As System.EventArgs) 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.Sort()
Me.ListBox1.DataSource = MyArrayList Me.ListBox1.ValueMember = "ID" Me.ListBox1.DisplayMember = "Value" End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MyArrayList.RemoveAt(2) MyArrayList.Sort()
Me.ListBox1.DataSource = Nothing
Me.ListBox1.DataSource = MyArrayList Me.ListBox1.ValueMember = "ID" Me.ListBox1.DisplayMember = "Value"
End Sub End Class -- --Zorpie
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
"Zorpiedoman" <no*********@beatles.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.DataSource = MyArrayList
Use:
Me.ListBox1.DataSource = MyArrayList.Clone
HTH
LFS
"Dennis" <De****@discussions.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
Thanks Larry. It would be like Microsoft to think they know better than us
what we want to do.
"Larry Serflaten" wrote: "Dennis" <De****@discussions.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
However, my setting .datasource = nothing, then re-setting it to the array
list does the same thing, so this does not fix the problem.
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.RemoveAt(2) MyArrayList.Sort() Me.Controls.Remove(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
> 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
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
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
"Zorpiedoman" <no*********@beatles.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 DataSourceChanged 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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?
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 ?
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |