ArrayList worked unexpectedly...
-
I'm dealing with my course project. This method handles ListView.SelectedIndexChanged, and al is an ArrayList.
private void lvFiles_SelectedIndexChanged(object sender, EventArgs e) { al.Clear(); foreach (int i in lvFiles.SelectedIndices) al.Add(exTree.SelectedPath+lvFiles.Items[i].Text); }
I want to collect the selected items in ListView when event SelectedIndexChanged is triggered. But I always get an ArrayList that contains two copies of selected items, just like this: AAAAAA BBBBBB AAAAAA BBBBBB But what I want is: AAAAAA BBBBBB :confused:I'm totally confused....Can anyone explain this? Many thanx.... -
I'm dealing with my course project. This method handles ListView.SelectedIndexChanged, and al is an ArrayList.
private void lvFiles_SelectedIndexChanged(object sender, EventArgs e) { al.Clear(); foreach (int i in lvFiles.SelectedIndices) al.Add(exTree.SelectedPath+lvFiles.Items[i].Text); }
I want to collect the selected items in ListView when event SelectedIndexChanged is triggered. But I always get an ArrayList that contains two copies of selected items, just like this: AAAAAA BBBBBB AAAAAA BBBBBB But what I want is: AAAAAA BBBBBB :confused:I'm totally confused....Can anyone explain this? Many thanx....I'm not sure what could be wrong, the problem might be somewhere else. Try putting a break point on
al.Clear()
and step through theforeach
loop checking the contents of al each time in the debugger. If you find that by some strange reasoning everything is getting added twice in the loop, then you can try a slightly different loop, like:foreach(ListViewItem item in lvFile.SelectedItems)
al.Add(...);or something. Its always possible that items are getting doubled up somewhere else, so use the debugger to find out whats going on.
My current favourite word is: Bacon!
-SK Genius
-
I'm dealing with my course project. This method handles ListView.SelectedIndexChanged, and al is an ArrayList.
private void lvFiles_SelectedIndexChanged(object sender, EventArgs e) { al.Clear(); foreach (int i in lvFiles.SelectedIndices) al.Add(exTree.SelectedPath+lvFiles.Items[i].Text); }
I want to collect the selected items in ListView when event SelectedIndexChanged is triggered. But I always get an ArrayList that contains two copies of selected items, just like this: AAAAAA BBBBBB AAAAAA BBBBBB But what I want is: AAAAAA BBBBBB :confused:I'm totally confused....Can anyone explain this? Many thanx....Hi, I can't explain the doubles, every time you add or remove an item to the selection, the event will fire but since you always clear the ArrayList that should not cause wrong results. However I do suggest you use SelectedItems instead of SelectedIndices, it will yield simpler code, skipping all the index stuff. :)
Luc Pattyn [Forum Guidelines] [My Articles]
I dislike the black-and-white voting system on questions/answers. X|
-
I'm dealing with my course project. This method handles ListView.SelectedIndexChanged, and al is an ArrayList.
private void lvFiles_SelectedIndexChanged(object sender, EventArgs e) { al.Clear(); foreach (int i in lvFiles.SelectedIndices) al.Add(exTree.SelectedPath+lvFiles.Items[i].Text); }
I want to collect the selected items in ListView when event SelectedIndexChanged is triggered. But I always get an ArrayList that contains two copies of selected items, just like this: AAAAAA BBBBBB AAAAAA BBBBBB But what I want is: AAAAAA BBBBBB :confused:I'm totally confused....Can anyone explain this? Many thanx....natsuyaki, Add some Trace'ing so that you know how many items are selected, and then how many items are added to the arraylist and what those items are. Also, if you're using .NET 2.0 upwards, i'd change the arraylist to a generic list, just makes life easier. Regards, Gareth.
-
I'm dealing with my course project. This method handles ListView.SelectedIndexChanged, and al is an ArrayList.
private void lvFiles_SelectedIndexChanged(object sender, EventArgs e) { al.Clear(); foreach (int i in lvFiles.SelectedIndices) al.Add(exTree.SelectedPath+lvFiles.Items[i].Text); }
I want to collect the selected items in ListView when event SelectedIndexChanged is triggered. But I always get an ArrayList that contains two copies of selected items, just like this: AAAAAA BBBBBB AAAAAA BBBBBB But what I want is: AAAAAA BBBBBB :confused:I'm totally confused....Can anyone explain this? Many thanx....Where do you create a
new
arraylist
? Is there a chance that you are creating a new copy each time hence having more than one instance ofal
?Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
-
I'm dealing with my course project. This method handles ListView.SelectedIndexChanged, and al is an ArrayList.
private void lvFiles_SelectedIndexChanged(object sender, EventArgs e) { al.Clear(); foreach (int i in lvFiles.SelectedIndices) al.Add(exTree.SelectedPath+lvFiles.Items[i].Text); }
I want to collect the selected items in ListView when event SelectedIndexChanged is triggered. But I always get an ArrayList that contains two copies of selected items, just like this: AAAAAA BBBBBB AAAAAA BBBBBB But what I want is: AAAAAA BBBBBB :confused:I'm totally confused....Can anyone explain this? Many thanx.... -
I'm dealing with my course project. This method handles ListView.SelectedIndexChanged, and al is an ArrayList.
private void lvFiles_SelectedIndexChanged(object sender, EventArgs e) { al.Clear(); foreach (int i in lvFiles.SelectedIndices) al.Add(exTree.SelectedPath+lvFiles.Items[i].Text); }
I want to collect the selected items in ListView when event SelectedIndexChanged is triggered. But I always get an ArrayList that contains two copies of selected items, just like this: AAAAAA BBBBBB AAAAAA BBBBBB But what I want is: AAAAAA BBBBBB :confused:I'm totally confused....Can anyone explain this? Many thanx....