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