PC Review


Reply
Thread Tools Rate Thread

I am TOTALLY baffled by action of this listbox/arraylist

 
 
Larry Woods
Guest
Posts: n/a
 
      16th Nov 2004
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


 
Reply With Quote
 
 
 
 
Yuichiro Ochifuji
Guest
Posts: n/a
 
      16th Nov 2004
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.(^^;
############################################################
 
Reply With Quote
 
Chris Dunaway
Guest
Posts: n/a
 
      16th Nov 2004
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.
 
Reply With Quote
 
Larry Woods
Guest
Posts: n/a
 
      16th Nov 2004
That did it.

THANKS!!!!

Larry Woods

"Yuichiro Ochifuji" <(E-Mail Removed)> wrote in message
news:e5Frpk%(E-Mail Removed)...
> 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.(^^;
> ############################################################



 
Reply With Quote
 
Larry Serflaten
Guest
Posts: n/a
 
      16th Nov 2004

"Yuichiro Ochifuji" <(E-Mail Removed)> wrote
>
> I think that it is not good to remove the selected item directly.


Why not?

LFS

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      16th Nov 2004
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" <(E-Mail Removed)>

>
> "Yuichiro Ochifuji" <(E-Mail Removed)> wrote
>>
>> I think that it is not good to remove the selected item directly.

>
> Why not?
>
> LFS
>



 
Reply With Quote
 
Larry Serflaten
Guest
Posts: n/a
 
      16th Nov 2004

"Cor Ligthert" <(E-Mail Removed)> 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

 
Reply With Quote
 
Larry Serflaten
Guest
Posts: n/a
 
      16th Nov 2004

"Larry Woods" <(E-Mail Removed)> 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



 
Reply With Quote
 
Yuichiro Ochifuji
Guest
Posts: n/a
 
      17th Nov 2004
Hi,Larry

"Larry Serflaten" wrote in message
news:(E-Mail Removed)
>> "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
############################################################
 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      17th Nov 2004
Larry,

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

Cor

"Larry Serflaten" <(E-Mail Removed)>
...
>
> "Cor Ligthert" <(E-Mail Removed)> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Totally Totally baffled ordnance1 Microsoft Excel Programming 7 19th Apr 2010 07:02 PM
Totally Baffled =?Utf-8?B?QmlsbHk=?= Windows XP Hardware 8 15th Jun 2007 04:01 AM
TOTALLY BAFFLED =?Utf-8?B?c2FuZHJhNDQ1Mw==?= Windows XP Internet Explorer 2 10th Apr 2006 10:36 AM
naviagation bar - totally baffled =?Utf-8?B?SmFjcXVpZUND?= Microsoft Frontpage 1 25th Nov 2005 10:11 AM
listbox bound to arraylist no changing values when values in arraylist are added or deleted Dave Microsoft VB .NET 0 11th Jan 2005 02:48 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:46 AM.