数据列表作为LIstBox Bug的数据源? [英] Array List as DataSource For LIstBox Bug?

查看:53
本文介绍了数据列表作为LIstBox Bug的数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是:


我有一个列表框。我将数组列表设置为数据源。我从数组列表中删除了一个项目

。我将listbox数据源设置为空。我再次将

列表框数据源设置为数组列表以进行刷新。单击列表中

中的项目,会出现索引错误。 (在非用户代码中,它试图从数组中获取一个元素,该元素大于

数组中的项目数。)


重现:


1.使用以下代码创建一个WindowsForm项目。

2.运行项目

3.单击列表中的第三项。

4.单击按钮(删除后,选择''四''项目)

5.再次单击按钮。 (删除后,没有选择,但......)

6.单击列表中的任一项 - 生成错误。


帮助!


谢谢。


---------------------代码重现--------------------

公共类Form1

继承System.Windows.Forms.Form


#Region" Windows窗体设计器生成的代码


Public Sub New()

MyBase.New()


' 'Windows窗体设计器需要此调用。

InitializeComponent()

''在InitializeComponent()调用后添加任何初始化
< br $>
结束子


''表格覆盖处理以清理组件列表。

受保护的重载覆盖子处理(ByVal处理为布尔)

如果处置那么

如果不是(组件什么都没有)那么

components.Dispose()

结束如果

结束如果

MyBase.Dispose(处置)

结束子


'' Windows窗体设计器要求

私有组件As System.ComponentModel.IContainer


''注意:Windows窗体设计器需要以下过程

''可以使用Windows窗体设计器修改它。

''不要莫使用代码编辑器来区分它。

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 =新系统.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.ClientS ize = 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)
< br $>
结束子


#End Region

私人MyArrayList作为新阵列列表


私有类ItemClass

实现IComparable

私有myID为整数

公共财产ID()为整数

获取

返回myID

结束获取

设置(ByVal值为整数)

myID = Value

结束集

结束财产

私有myValue为字符串

公共属性值()为字符串

获取

返回myValue

结束获取

设置(ByVal值为字符串)

myValue = Value

结束集

结束属性


公共函数CompareTo(ByVal obj As Object)As Integer Implements

System.IComparable.CompareTo

如果TypeOf obj是ItemClass那么

如果是DirectCast(obj,ItemClass).ID> Me.ID然后返回-1

如果DirectCast(obj,ItemClass).ID< Me.ID然后返回1

返回0

结束如果

结束功能

结束班级


Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理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。添加(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)处理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

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

推荐答案

你好Zorpie,


您是否尝试过列表框的刷新方法?


HTH,


Bernie Yaeger


Zorpiedoman <无********* @ beatles.com>在消息中写道

news:8E ********************************** @ microsof t.com ...
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...
问题是:

我有一个列表框。我将数组列表设置为数据源。我从数组列表中删除了一个
项目。我将listbox数据源设置为空。我再次将
listbox数据源设置为数组列表以进行刷新。单击列表中的项目
,会出现索引错误。 (在非用户代码中,它正在尝试
从数组中获取一个大于
数组中项目数的元素。)

要复制:

1.使用以下代码创建WindowsForm项目。
2.运行项目
3.单击列表中的第三项。
4.单击按钮(删除后,选择''四''项目)
5.再次单击按钮。 (删除后,没有选择,
但是......)
6.单击列表中的任一项 - 生成错误。

帮助!

谢谢。

---------------------代码重现------------ --------
Public Class Form1
继承System.Windows.Forms.Form

#Region" Windows窗体设计器生成的代码

Public Sub New()
MyBase.New()

''Windows窗体设计器需要此调用。
InitializeComponent()
''在InitializeComponent()调用后添加任何初始化

End Sub

''表格覆盖处理清理组件列表。
受保护的重载覆盖子处理(ByVal处理为布尔值)
如果处理则
如果不是(组件是什么)那么
components.Dispose()结束如果
MyBase.Dispose(处理)
结束子

''Windows窗体设计器要求
私有组件作为系统.ComponentModel.IContainer

''注意:Windows窗体设计器需要以下步骤
''可以使用Windows窗体设计器修改它。
''不要使用代码编辑器修改它。
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 =新系统.Drawing.Point(0,0)
Me.ListBox1.Name =" ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(304,251)
我。 ListBox1.TabIndex = 0
''
''Button1
''
Me.Button1.Location = New System.Drawing.Point(208,264)
我.Button1.Name =" Button1"
Me.Button1.Size = New System.Drawing.Size(72,24)
Me.Button1.TabIndex = 1
Me.Button1.Text =" Button1"
''
''Form1
''
Me.AutoScaleBaseSize =新系统。 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
私有MyArrayList作为新的ArrayList

私有类ItemClass
实现IComparable
私有myID作为整数
公共属性ID()为整数
获取
返回myID
结束获取
设置(ByVal值为整数)
myID = Value
结束集
结束属性
私有myValue为字符串
公共属性值()为字符串
获取
返回myValue
结束获取
设置(ByVal值作为字符串)
myValue = Value
结束集
结束属性

公共函数CompareTo(ByVal obj As Object)作为整数
实现
System.IComparable.CompareTo
如果TypeOf obj是ItemClass然后
如果DirectCast(obj,ItemClass).ID> Me.ID然后返回-1
如果DirectCast(obj,ItemClass).ID< Me.ID然后返回1
返回0
结束如果
结束功能


私有子Form1_Load(ByVal发送者为System.Object,ByVal e As
System.EventArgs)处理MyBase.Load
Dim A As New ItemClass
A.ID = 1
A.Value =" One"
MyArrayList。添加(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
我.ListBox1.ValueMember =" ID"
Me.ListBox1.DisplayMember =" Value"
End Sub

Private Sub Button1_单击(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理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
结束课

-
- Zorpie
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



不刷新数据。到目前为止的解决方法是在从arraylist中删除项目之前将

SelectedIndex设置为-1。然后它

工作正常,但在现实生活中这可能并不总是可行。


-zorpy


Bernie Yaeger写道:
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:
你好Zorpie,

你有没有尝试过listbox'的刷新方法?

HTH,

Bernie Yaeger
Hi Zorpie,

Did you try the listbox''s refresh method?

HTH,

Bernie Yaeger





" Zorpiedoman" <无********* @ beatles.com>写了

"Zorpiedoman" <no*********@beatles.com> wrote
问题是:

我有一个列表框。我将数组列表设置为数据源。我从数组列表中删除了一个项目
。我将listbox数据源设置为空。我再次将
listbox数据源设置为数组列表以进行刷新。单击列表中的项目,将出现索引错误。


无处不在

Me.ListBox1.DataSource = MyArrayList


使用:

Me.ListBox1.DataSource = MyArrayList.Clone
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


HTH
LFS


这篇关于数据列表作为LIstBox Bug的数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆