I am TOTALLY baffled by action of this listbox/arraylist

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
 
Y

Yuichiro Ochifuji

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
 
C

Chris Dunaway

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?

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.
 
C

Cor Ligthert

Larry,

Because it gives a very strange error, I am not even sure if this answer
fits completly.

I had to disconnect the arraylist from the datalist.datasource before I
could remove the items and than I could remove that item from the arraylist.

The error comes even direct when you hard coded it.

\\\
Dim al as new arraylist
al.Add("A")
al.Add("B")
ListBox1.DataSource = al
Listbox1.selectedindex = 1
al.removeat(1)
listbox1.clearselected
///

Therefore I did not give an answer because I have never used an arraylist as
datasource for a listbox. and I was curious if somebody would come with a
nice solution.

When you set before that remove listbox.datasource = nothing and after that
listbox1.datassource = al than it goes.

I typed it in here so maybe there is a typo, I tested it and than deleted
the code.

Cor
 
L

Larry Serflaten

Cor Ligthert said:
Because it gives a very strange error, I am not even sure if this answer
fits completly.

I had to disconnect the arraylist from the datalist.datasource before I
could remove the items and than I could remove that item from the arraylist.


I have no trouble with this code that uses the selected item to remove
an item from the data source:

Dim Source As New ArrayList

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Source.Add("AAA")
Source.Add("BBB")
Source.Add("CCC")
ListBox1.DataSource = Source
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Source.Remove(ListBox1.SelectedItem)
ListBox1.DataSource = Source.Clone
End Sub


So, from the earlier post:
"I think that it is not good to remove the selected item directly."

I wonder, why not?

LFS
 
L

Larry Serflaten

Larry Woods said:
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?

It may have something to do with the statement found in Help: (DataSourec property)

"When the DataSource property is set, a user cannot modify the items collection."

Try this:

Dim al As New ArrayList
Dim ar As New ArrayList

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
al.Add("AAA")
al.Add("BBB")
al.Add("CCC")
ListBox1.DataSource = al
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ar.Add(ListBox1.SelectedItem)
al.Remove(ListBox1.SelectedItem)
ListBox1.DataSource = al.Clone
ListBox2.DataSource = ar.Clone
End Sub
 
Y

Yuichiro Ochifuji

Hi,Larry

in message
I wonder, why not?

at a guess,
listbox remembers the selected item index,
but when another datasource is connected it is initialized.
again at a guess.

In the following code,
first push the button3,and push the button2
then the same error happens.

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

Source.Add("AAA")
Source.Add("BBB")
Source.Add("CCC")
ListBox1.DataSource = Source

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
ListBox1.DataSource = Source

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
Source.Remove(ListBox1.SelectedItem)
ListBox1.DataSource = Source.Clone
End Sub
 
C

Cor Ligthert

Larry,

Select C and push than the button, with me that gave directly a crazy error.

Cor
 
L

Larry Serflaten

Yuichiro Ochifuji said:
at a guess,
listbox remembers the selected item index,
but when another datasource is connected it is initialized.
again at a guess.

I do think there is something more to it than can be easily seen.
As I stated elsewhere, the Help file says the Items collection
can not be altered when the DataSource is used. That is what
lead me to use the Clone method. When the DataSource is
given a Clone, the programmer has no reference to that copy
of the ArrayList, so he can not change it. Assigning a new
clone fires the DataSourceChanged event which indicates it
gets a completely new copy of the ArrayList.

In the following code,
first push the button3,and push the button2
then the same error happens.

I tried that and got no error...

???
LFS
 
C

Cor Ligthert

Larry,

I have as well 2003 however I deleted the code, however I was a long time
busy with it, so I remember me that I got an error when I started selecting
the C, that goes, but the A and B where not selected. And when I than tried
to select B I got that error, maybe I explained it to short.

Cor
 
R

Richard Myers

Larry Serflaten said:
Why not?

LFS

My take is that when items are stored in a list control or anything else that accepts a datasource
that implements the IList interface, they are not connected via the collection type
(dataset/datatable/arraylist/etc) but via the IList interface. The IList interface implements
ICollection and IEnumerable. IEnumerable uses the For Each construct - which we all know implicitly
prevents the modification of the underlying collection because otherwise the enumerator gets
confused about its current location within the collection... and tends to throw some spectacularly
destructive exceptions.

hth
Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top