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. ListView remove item problem!! Help please

ListView remove item problem!! Help please

Scheduled Pinned Locked Moved C#
helpdatabasedebuggingquestion
5 Posts 3 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.
  • B Offline
    B Offline
    Brandon Parker
    wrote on last edited by
    #1

    I call this function to remove the last item in a listview in details view. The problem is that the control is on a tab page, and if i call this function before the control has been viewed(ie. before the tab page with this control is shown), this function fails with the inner exception of message "Object reference not set to an instance of an object", and, the regular exception of "unable to create handle". This only happens if the control is not viewed first. Can someone help me out here. public void RemoveLastItem() { int index = Items.Count; try { Items.RemoveAt(index-1); } catch(OutOfMemoryException e){ // Error is caught here!! Why ???? #if DEBUG MessageBox.Show(e.InnerException.Message); #endif } catch(NullReferenceException e){ #if DEBUG MessageBox.Show(e.Message); #endif } }

    O J 2 Replies Last reply
    0
    • B Brandon Parker

      I call this function to remove the last item in a listview in details view. The problem is that the control is on a tab page, and if i call this function before the control has been viewed(ie. before the tab page with this control is shown), this function fails with the inner exception of message "Object reference not set to an instance of an object", and, the regular exception of "unable to create handle". This only happens if the control is not viewed first. Can someone help me out here. public void RemoveLastItem() { int index = Items.Count; try { Items.RemoveAt(index-1); } catch(OutOfMemoryException e){ // Error is caught here!! Why ???? #if DEBUG MessageBox.Show(e.InnerException.Message); #endif } catch(NullReferenceException e){ #if DEBUG MessageBox.Show(e.Message); #endif } }

      O Offline
      O Offline
      Orion Buttigieg
      wrote on last edited by
      #2

      Brandon, have you tried instantiating the listView control. if your in the same class you should be able to use "this". if your in another class you should be able to get to the listView by creating a new instance of that class if you haven't already and get to the listView that way. MyClass cl = new MyClass() cl.MylistView.... when i add: here parameter is an ArrayList and this code sits in a foreach ListViewItem item = new ListViewItem(parameter); item.SubItems.Add(""); this.listView_Parameters.Items.Add(item); i'd expect Remove() would be the opposite. hope i didn't state the obvious Orion

      B 1 Reply Last reply
      0
      • O Orion Buttigieg

        Brandon, have you tried instantiating the listView control. if your in the same class you should be able to use "this". if your in another class you should be able to get to the listView by creating a new instance of that class if you haven't already and get to the listView that way. MyClass cl = new MyClass() cl.MylistView.... when i add: here parameter is an ArrayList and this code sits in a foreach ListViewItem item = new ListViewItem(parameter); item.SubItems.Add(""); this.listView_Parameters.Items.Add(item); i'd expect Remove() would be the opposite. hope i didn't state the obvious Orion

        B Offline
        B Offline
        Brandon Parker
        wrote on last edited by
        #3

        Yes you have stated the obvious;P Remember I said this only occures when the tabpage that hold the listview control isnt first viewed. The listview is created along with that tabpage in the forms constructor so it is instantiated. Im thinking somehow that the items in the view control arent actually created until they are needed(ie. when the control is viewed). But, the truth is im not sure what is going on.

        1 Reply Last reply
        0
        • B Brandon Parker

          I call this function to remove the last item in a listview in details view. The problem is that the control is on a tab page, and if i call this function before the control has been viewed(ie. before the tab page with this control is shown), this function fails with the inner exception of message "Object reference not set to an instance of an object", and, the regular exception of "unable to create handle". This only happens if the control is not viewed first. Can someone help me out here. public void RemoveLastItem() { int index = Items.Count; try { Items.RemoveAt(index-1); } catch(OutOfMemoryException e){ // Error is caught here!! Why ???? #if DEBUG MessageBox.Show(e.InnerException.Message); #endif } catch(NullReferenceException e){ #if DEBUG MessageBox.Show(e.Message); #endif } }

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          What happens if you call CreateControl() on the ListView in the constructor; this forces the control to create itself instead of delaying until it feels it is best to do so (ie when you view that tab for the first time). James "Java is free - and worth every penny." - Christian Graus

          B 1 Reply Last reply
          0
          • J James T Johnson

            What happens if you call CreateControl() on the ListView in the constructor; this forces the control to create itself instead of delaying until it feels it is best to do so (ie when you view that tab for the first time). James "Java is free - and worth every penny." - Christian Graus

            B Offline
            B Offline
            Brandon Parker
            wrote on last edited by
            #5

            Works great. Thanks.

            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