Clear ListView

I have a simple Windows form containing, among other components, a ListView object called list . In the form, the button allows me to clear the list when I click on list.Items.Clear() . It works great.

Now I have a separate Test class, whose method update() is called on some events external to the form. When building the form, I pass the link to the list using the SetList method. In debug mode, update() is called on events that I fire, and its contents are executed, but my list is not cleared.

Why is this? The link is set correctly, I checked.

 class Test { private ListView list; public void setList(ListView list) { this.list = list; } public void update() { this.list.Items.Clear(); } } 

when I look closer, when my list changes, putting breakpoints in update (), the list is cleared and cleared. This seems to be another list that is changing, but I only have one, and I never do any new ones on it ... ????

+8
c # winforms
source share
2 answers

Use the update method below:

  public void update() { this.list.Items.Clear(); this.list.Update(); // In case there is databinding this.list.Refresh(); // Redraw items } 

If this does not work, it is obvious that you are modifying another instance of the list object. In this case, temporarily change the declaration of the object, as shown below, and see if something has changed. If so, you will need to look at your code to make sure that you clear the correct list instance:

 private static ListView list; 
+5
source share

In my case, I do not use data bindings, but add elements explicitly in a loop using Add (). It seemed to me that ListView.Clear() and ListView.Items.Clear() did not work.

Turns off ... The ListView was actually cleared, but I did not clear the input list, so I effectively cleared and then refilled the ListView with previously cleared items.

Sometimes these are the simplest things ... D'OH!

0
source share

All Articles