I have a simple Windows form containing among other components, a ListView
object named list
. On the form, a button enables me to empty the list on click with list.Items.Clear()
. This works fine.
Now I have a separate class Test
, whose method update()
is called on some events external to the form. At construction of the form, I pass a reference to the list using the SetList
method. In debug mode, update()
is called on the events that I trigger, and its content executed, but my list isn't cleared.
Why is this? The reference is properly set, 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 at my list being modified by putting breakpoints in update(), list is cleaned and stays cleaned. It really seems like it is another list being modified, but I have only one and never do any new on it... ????
Use the below modified update
method:
public void update()
{
this.list.Items.Clear();
this.list.Update(); // In case there is databinding
this.list.Refresh(); // Redraw items
}
If this doesn't work, it is evident that you're modifying another instance of the list
object. In this case, temporarily modify the declaration of the object like below and see if changes anything. If it does, you'll need to review your code to make sure that you're clearing the right instance of the list:
private static ListView list;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With