L
Larry Woods
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
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