Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ArrayList worked unexpectedly...

ArrayList worked unexpectedly...

Scheduled Pinned Locked Moved C#
questionlearning
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    natsuyaki
    wrote on last edited by
    #1

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

    A L G G G 6 Replies Last reply
    0
    • N natsuyaki

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

      A Offline
      A Offline
      Anthony Mushrow
      wrote on last edited by
      #2

      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 the foreach 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

      1 Reply Last reply
      0
      • N natsuyaki

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

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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|


        1 Reply Last reply
        0
        • N natsuyaki

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

          G Offline
          G Offline
          Gareth H
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • N natsuyaki

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

            G Offline
            G Offline
            GuyThiebaut
            wrote on last edited by
            #5

            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 of al?

            Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
            1 Reply Last reply
            0
            • N natsuyaki

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

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              How do you determine what the ArrayList contains? Are you stuck with framework 1.1? Otherwise you should use a List<string> instead of an ArrayList.

              Despite everything, the person most likely to be fooling you next is yourself.

              1 Reply Last reply
              0
              • N natsuyaki

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

                N Offline
                N Offline
                natsuyaki
                wrote on last edited by
                #7

                Thx everyone... al is all right.The problem is: the main thread print this list, and then another thread tried again.... ;P

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups