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. UNKNOWN PROBLEM (ListBox DataSource )

UNKNOWN PROBLEM (ListBox DataSource )

Scheduled Pinned Locked Moved C#
cssdatabasehelpquestiondiscussion
3 Posts 2 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.
  • R Offline
    R Offline
    Radgar
    wrote on last edited by
    #1

    I have a ListBox (CatList) and an ArrayList (Cats) bound to it. I'm removing the selected item in the ListBox from the ArrayList and rebinding the source after removing: private ArrayList Cats = new ArrayList(); //The arraylist source for the ListBox ... //some items are added here ... private void bRemoveCat_Click(object sender, System.EventArgs e) { int selIndex = this.CatList.SelectedIndex; this.CatList.DataSource = null; this.CatList.Items.Clear(); Cats.RemoveAt(selIndex); this.CatList.DataSource = Cats; this.CatList.DisplayMember = "Name"; this.CatList.ValueMember = "ID"; } No exception is thrown when the above code is executed but after the code is executed, if I click on the ListBox, the exception below is immediately thrown. An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll Additional information: Index was out of range. Must be non-negative and less than the size of the collection. What do you think? Thanks in advance. Radgar "Imagination is more important than knowledge." - Albert Einstein

    D 1 Reply Last reply
    0
    • R Radgar

      I have a ListBox (CatList) and an ArrayList (Cats) bound to it. I'm removing the selected item in the ListBox from the ArrayList and rebinding the source after removing: private ArrayList Cats = new ArrayList(); //The arraylist source for the ListBox ... //some items are added here ... private void bRemoveCat_Click(object sender, System.EventArgs e) { int selIndex = this.CatList.SelectedIndex; this.CatList.DataSource = null; this.CatList.Items.Clear(); Cats.RemoveAt(selIndex); this.CatList.DataSource = Cats; this.CatList.DisplayMember = "Name"; this.CatList.ValueMember = "ID"; } No exception is thrown when the above code is executed but after the code is executed, if I click on the ListBox, the exception below is immediately thrown. An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll Additional information: Index was out of range. Must be non-negative and less than the size of the collection. What do you think? Thanks in advance. Radgar "Imagination is more important than knowledge." - Albert Einstein

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      I'd verify the value of selIndex before you try and use it on the Cats arraylist. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      R 1 Reply Last reply
      0
      • D Dave Kreskowiak

        I'd verify the value of selIndex before you try and use it on the Cats arraylist. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        R Offline
        R Offline
        Radgar
        wrote on last edited by
        #3

        I already did it Dave. Actually the item is removed properly. But the error occurs after this process (when I click anywhere on the ListBox) Radgar "Imagination is more important than knowledge." - Albert Einstein

        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