By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 1,871 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

I am TOTALLY baffled by action of this listbox/arraylist

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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
--
################################################## ##########
  Yuichiro Ochifuji
I am afraid of my poor English.(^^;
################################################## ##########
Nov 21 '05 #2

P: n/a
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?


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.
Nov 21 '05 #3

P: n/a
That did it.

THANKS!!!!

Larry Woods

"Yuichiro Ochifuji" <oc******@japan.interq.or.jp> wrote in message
news:e5****************@TK2MSFTNGP15.phx.gbl...
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
--
################################################## ##########
  Yuichiro Ochifuji
I am afraid of my poor English.(^^;
################################################## ##########

Nov 21 '05 #4

P: n/a

"Yuichiro Ochifuji" <oc******@japan.interq.or.jp> wrote

I think that it is not good to remove the selected item directly.


Why not?

LFS

Nov 21 '05 #5

P: n/a
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

"Larry Serflaten" <se*******@usinternet.com>

"Yuichiro Ochifuji" <oc******@japan.interq.or.jp> wrote

I think that it is not good to remove the selected item directly.


Why not?

LFS

Nov 21 '05 #6

P: n/a

"Cor Ligthert" <no************@planet.nl> wrote
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

Nov 21 '05 #7

P: n/a

"Larry Woods" <la***@NOSPAMlwoods.com> wrote
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

Nov 21 '05 #8

P: n/a
Hi,Larry

"Larry Serflaten" wrote in message
news:uh**************@tk2msftngp13.phx.gbl
"I think that it is not good to remove the selected item directly."


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
--
################################################## ##########
??Yuichiro Ochifuji
################################################## ##########
Nov 21 '05 #9

P: n/a
Larry,

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

Cor

"Larry Serflaten" <se*******@usinternet.com>
...

"Cor Ligthert" <no************@planet.nl> wrote
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

Nov 21 '05 #10

P: n/a

"Cor Ligthert" <no************@planet.nl> wrote

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


Interesting, I get no error! (VS 2003)

LFS
Nov 21 '05 #11

P: n/a

"Yuichiro Ochifuji" <oc******@japan.interq.or.jp> wrote
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.


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

Nov 21 '05 #12

P: n/a
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
Nov 21 '05 #13

P: n/a

"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:eN****************@tk2msftngp13.phx.gbl...

"Yuichiro Ochifuji" <oc******@japan.interq.or.jp> wrote

I think that it is not good to remove the selected item directly.


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
Nov 21 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.