Why does SubItems.Clear() also delete the Name attribute?

后端 未结 3 1634
無奈伤痛
無奈伤痛 2021-01-19 04:57

I am using a WinForms ListView in details mode (.NET 4.0, running on Windows 7) and I have a function that needs to clear the subitems in a particular item. Unfortunately w

3条回答
  •  佛祖请我去吃肉
    2021-01-19 05:35

    Please note 1:
    The ListViewItem.Text (not the Name) is the ListViewItem.SubItems[0], but:
    the ListViewItem.SubItems.Clear() clears also the ListViewItem.Name!

    So, if you use SubItems.Clear, you then have to restore both Name and Text (if you need them).

    Please note 2:
    If you use *Key methods (eg. ListView.Items.ContainsKey() or ListView.Items.RemoveByKey()) to access the items (instead of *Index ones), take care of the ListViewItem.Name, which is the key you need to pass to these methods...

    What a smart logic...

    For the case anyone is forced or wants to use ListView I have discovered another problem described in question 23007388.

    As it might take very long time to determine all this stuff, I have posted this answer even this thread is a little bit old.

提交回复
热议问题