我对这个列表框/ arraylist的动作完全感到困惑 [英] I am TOTALLY baffled by action of this listbox/arraylist
问题描述
我正在创建一个从 - 到列表框的集合,其中左表示listbox有一个
的值列表,我希望能够一次选择1个这样的值,
并将它们移动到正确的列表中。列表框,从
左侧列表框中删除所选值。当您从左侧列表框中选择任何成员并单击
按钮将其移动到右侧列表框时,一切正常。如果你
选择左侧列表框中的最后一个成员,然后点击按钮移动
该成员,它将移动到右侧列表框,然后点击任意位置
左侧列表框,程序会因索引错误而崩溃。
为什么?
TIA,< br $>
Larry Woods
您可以通过创建VB.NET Windows应用程序来运行此程序。添加
以下控件:
listbox1(这将是左列表框)
listbox2(这将是正确列表框)
button1(将触发事件的命令按钮,将会员从左侧列表框移动到左侧列表框中)
然后将下面的代码复制到表单中。请注意,arraylists是
在表单级别定义。
Private al As New ArrayList
Private ar As New ArrayList
''注意:以上定义在表单级别
Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)Handles MyBase.Load
with al
.Add(" A")
.Add(" B" ;)
.Add(" C")
结束
''加载listbox1(左侧),带有两个成员的arraylist
ListBox1.DataSource = al
End Sub
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button1.Click
''button1将一个成员从listbox1移动到listbox2
''它将一个成员添加到ar arraylist(右列表框)来源)
''并删除al arraylist中的成员(左侧列表框源)
''然后重新启动ablishes al as as arraylist source for left
listbox
ListBox2.DataSource = Nothing
ar.Add(ListBox1.SelectedItem)
ListBox2.DataSource = ar
al.Remove(ListBox1.SelectedItem)
ListBox1.DataSource = Nothing
ListBox1.DataSource = al
End Sub
I am creating a "from-to" set of listboxes where the "left" listbox had a
list of values and I want to be able to select these values, 1 at a time,
and move them into a "right" listbox, removing the selected value from the
left listbox. When you select any member from the left listbox and click on
the button to move it to the right listbox, everything works fine. If you
select the LAST member in the left listbox, and click on the button to move
that member, it WILL move to the right listbox, BUT then click anywhere over
the left listbox and the program will crash with an indexing error.
WHY?
TIA,
Larry Woods
You can run this program by creating a VB.NET Windows application. Add the
following controls:
listbox1 (this will be the "left" listbox)
listbox2 (this will be the "right" listbox)
button1 (command button that will fire event that will "move" a member from
the left listbox to the right listbox)
Then copy the code below into the form. Note that the arraylists are
defined at a form level.
Private al As New ArrayList
Private ar As New ArrayList
'' NOTE: Above definitions are at the form level
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
With al
.Add("A")
.Add("B")
.Add("C")
End With
'' Load listbox1 (on left) with an arraylist with two members
ListBox1.DataSource = al
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'' button1 moves a member from listbox1 to listbox2
'' It adds a member to the ar arraylist (right listbox source)
'' and deletes a member from the al arraylist (left listbox source)
'' and then reestablishes al as the arraylist source for the left
listbox
ListBox2.DataSource = Nothing
ar.Add(ListBox1.SelectedItem)
ListBox2.DataSource = ar
al.Remove(ListBox1.SelectedItem)
ListBox1.DataSource = Nothing
ListBox1.DataSource = al
End Sub
推荐答案
拉里
我想直接删除所选项目并不好。
试试这个。
ListBox2.DataSource = Nothing
ar。添加(ListBox1.SelectedItem)
ListBox2.DataSource = ar
Dim o As Object
o = Me.ListBox1.SelectedItem
Dim i As Integer
i = Me.ListBox1.Items.IndexOf(o)
Me.ListBox1.SetSelected(i,False)
al.Remove(o)
ListBox1.DataSource = Nothing
ListBox1.DataSource = al
-
######################################### ########## ## ########
?
Hi,Larry
I think that it is not good to remove the selected item directly.
Try this.
ListBox2.DataSource = Nothing
ar.Add(ListBox1.SelectedItem)
ListBox2.DataSource = ar
Dim o As Object
o = Me.ListBox1.SelectedItem
Dim i As Integer
i = Me.ListBox1.Items.IndexOf(o)
Me.ListBox1.SetSelected(i, False)
al.Remove(o)
ListBox1.DataSource = Nothing
ListBox1.DataSource = al
--
################################################## ##########
B !!!!?(BYuichiro Ochifuji
我害怕我的英语不好。(^^;
################################### #########################
B!!!!(BYuichiro Ochifuji
I am afraid of my poor English.(^^;
################################################## ##########
2004年11月16日星期二05:27: 15-0700,Larry Woods写道:
On Tue, 16 Nov 2004 05:27:15 -0700, Larry Woods wrote:
该成员,它将移动到右侧列表框,但是然后点击左侧列表框上的任何地方,程序将崩溃索引错误。
为什么?
that member, it WILL move to the right listbox, BUT then click anywhere over
the left listbox and the program will crash with an indexing error.
WHY?
我没有为您提供解决方案,但我想确认当我尝试
你的测试,我得到同样的错误。移动列表中的最后一个
项目然后单击列表中的任何剩余项目后,我收到错误。
-
克里斯
dunawayc [AT] sbcglobal_lunchmeat_ [DOT] net
要给我发电子邮件,请删除[ ","]",下划线,lunchmeat和
替换我的电子邮件地址中的某些单词。
I don''t have a solution for you, but I wanted to confirm that when I try
your test, I get the same error. I get the error after moving the last
item in the list and then clicking on any remaining item in the list.
--
Chris
dunawayc[AT]sbcglobal_lunchmeat_[DOT]net
To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
这篇关于我对这个列表框/ arraylist的动作完全感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!