Listviewitem & ArrayList not being nice to each other

Arokh

Centurion
Joined
Apr 11, 2006
Messages
124
I'm beginning to get frustrated, since I'm working on an actually small problem a very long time.
I just noticed that when I add a Listviewitem to an ArrayList a subitem is applied before the actual Listviewitem,
it says something about an exception.
Here is an Image which shows what i mean:
Exception.png


The problem is easily reproduced with this:
Visual Basic:
Dim array As New System.Collections.ArrayList
Dim obj As New ListViewItem("dfh")
array.Add(obj)
(No error or whatsoever is thrown)

Any ideas?
 
I could not reproduce the problem. Are you doing any multithreading, control creating, or other such things that can cause IDE problems?
 
Since my project has fairly become more complex,
I aslo thought maybe other things are messing it up.
So I created a new Project (Windows Form) with a ListView (lstvw_test)
Then I added the following code to the form_load event:
Visual Basic:
Dim LstObjArray As New System.Collections.ArrayList
Dim I As Int16

For I = 0 To 100
	Dim LstObj As New ListViewItem
	LstObj.Text = "a" & Str(I)
	LstObj.SubItems.Add("b" & Str(I))
	LstObjArray.Add(LstObj)
Next

lstvw_test.Items.AddRange(LstObjArray.ToArray)

The wierd part is when I add the line Debug.print(LstObjArray.Item(0).text),
the correct text is printed (not the error as it should be when the Editor was correct).
Although it may seem as it was just an error on the editors behalf,
as soon as the AddRange Sub is called, an exception is thrown (System.InvalidCastException).
DirectCast(LstObjArray.ToArray, ListViewItem()) also throws the exception.

I attached the project of the scenario described above.

The following text is not necessary to understand the problem better but it might give some ideas on alternative solutions.
Background on what I'm trying to achieve:
Files/Folders (e.g. Exlorer) are dragged to the Form,
then the filenames should be added to the ListView.
The only condition: No double entries.
So at first I just made a Loop with the String array,
comparing all the items in the ListView(.IndexOfKey([String])) with the "ToBeAdded" String,
If its not found, add it and if it is don't.
But as I'm now painfully aware, this is extremly slow.

After thinking about it some time, I had an idea which could have saved the day,
but no, now I have this problem as described above.
The idea was, that the dropped files don't need to be checked against each other
since it is impossible(?) that there is a duplicate and
therefore only need to be compared against the initial Items in the ListView.
In order to do that I need to Buffer the ListViewItems (LstObjArray),
before adding them to the ListView.
 

Attachments

Back
Top