Skip to content

Updating ListView.ListViewItem.ListViewSubItem Text or Name values updates them all, why? is this normal? #10963

Closed

Description

@creizlein

.NET version

Tested and Checked on .NET Runtime 7.0 and 8.0

Did it work in .NET Framework?

Not tested/verified

Did it work in any of the earlier releases of .NET Core or .NET 5+?

n/a.

Issue description

Talking about WinForms, ListView Control, ListViewItem and ListViewSubItems

when you have a ListViewItem , lets say, with 10 ListViewSubItems, on a Details view, and you change the text, it triggers a update text on ALL of the subitems.

If you check on the Text Setter, it reads as follows:

ListViewSubItem class

        public string Text {
            get => text ?? string.Empty;
            set {
                text = value;
                _owner?.UpdateSubItems(-1);
            }
        }

_owner being the ListViewItem, so if we go there, we find this:

internal void UpdateSubItems(int index) => UpdateSubItems(index, SubItemCount);
internal void UpdateSubItems(int index, int oldCount) {
    // trim down, not modified, just formated.
    if (_listView is not null && _listView.IsHandleCreated) {
        int subItemCount = SubItemCount;
        int itemIndex = Index;

        if (index != -1) {
            _listView.SetItemText(itemIndex, index, _subItems[index].Text);
        } else {
            for (int i = 0; i < subItemCount; i++) {
                _listView.SetItemText(itemIndex, i, _subItems[i].Text);
            }
        }

        for (int i = subItemCount; i < oldCount; i++) {
            _listView.SetItemText(itemIndex, i, string.Empty);
        }
    }
}

the ListView itself is then the one in charge of triggering the PInvoke.SendMessage with each of the subitems actual text.

So my question is, why does this happen? and even worst, why does changing the NAME of the item also triggers an update on the text?

Seems a little bit weird and overwhelming, but then again I am just checking and making sure this is not a problem..

I have found some laggy and performance issues and found myself that the ListView was killing the UI Thread with message loops because I am updating a text on a subitem on column 20 , of course, it then updates all 30 of them which are all the columns I have.

Steps to reproduce

Create a WinForms app with a ListView on it, add a couple of ListViewItems with several subitems, and try to change the text of Nth subitem only and check how many of them get triggered to update.

Activity

  1. added
    untriaged
    on Feb 28, 2024
  2. creizlein commented on Feb 28, 2024

    @creizlein
    Author

    I believe the problem comes from the fact that the ListViewSubItem calls UpdateSubItems(-1) , with -1 of course, but then the ListViewItem itself does not correct that and calls the ListView to only update that specific item with the index of itself.

  3. added
    tenet-performance
    on Feb 28, 2024
  4. elachlan commented on Feb 28, 2024

    @elachlan
    Contributor

    @creizlein I'd suggest you look into the code history and see. Generally small mistakes like this happen in a large code base. I imagine the team would be happy to review a PR if you submitted one.

  5. elachlan commented on Feb 28, 2024

    @elachlan
    Contributor

    Maybe something like:

    [Localizable(true)]
    [AllowNull]
    public string Text
    {
        get => text ?? string.Empty;
        set
        {
            text = value;
            _owner?.UpdateSubItems(_owner.SubItems.IndexOf(this));
        }
    }
    
    [Localizable(true)]
    [AllowNull]
    public string Name
    {
        get => name ?? string.Empty;
        set
        {
            name = value;
            _owner?.UpdateSubItems(_owner.SubItems.IndexOf(this));
        }
    }
  6. creizlein commented on Feb 28, 2024

    @creizlein
    Author

    Idk I am up to the task, but I will try to find all the possibilities and submit something.
    I still don't understand why it updates the TEXT when you change the NAME, I believe it has something to do for when there is no text at all, it takes the name and put it as text, but if that's the case then it should IF it at least.

    Also I an wondering why there is no check on if the name/text is changed first before sending the messages, like if (name!=value) just return. I will put together a PR for it.

    Was first wondering if it was on purpose at all, maybe there is a solid reason behind updating them all.

  7. elachlan commented on Feb 28, 2024

    @elachlan
    Contributor

    I'd just try it and see. Same with the name update triggering the change. I figure its probably got something to do with the designer. There aren't much references to the Name though.

  8. paul1956 commented on Feb 28, 2024

    @paul1956
    Contributor
  9. merriemcgaw commented on Feb 28, 2024

    @merriemcgaw
    Contributor

    @paul1956 's theory makes a lot of sense. As @elachlan said, we would be happy to review a PR should you submit one. Given that this would be an actual behavior change, we would need to put it behind a quirk so that it doesn't impact existing applications.

  10. removed
    untriaged
    on Feb 28, 2024
  11. added this to the Help wanted milestone on Feb 28, 2024
  12. elachlan commented on May 12, 2024

    @elachlan
    Contributor

    @merriemcgaw, would you accept a PR without the quirk if the behavior is only changed at Text and not Name?

  13. merriemcgaw commented on May 17, 2024

    @merriemcgaw
    Contributor

    @elachlan I think we would actually, thank you!

  14. added a commit that references this issue on May 19, 2024
    cb65f53
  15. elachlan commented on May 20, 2024

    @elachlan
    Contributor

    @creizlein I have a pr up if you would like to review it. We can't seem to reproduce your performance issue, but it should remove one of the loops.

  16. added a commit that references this issue on May 22, 2024
    af38079
  17. creizlein commented on May 22, 2024

    @creizlein
    Author

    Sorry I am late to the party, i was some days out of town and was also watching @merriemcgaw presentation today, I would love to give this a try, let me setup my dev platform so i can run this last code and provide some feedback hopefully this week.

    Btw, great presentation @merriemcgaw , kudos !

  18. locked and limited conversation to collaborators on Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-controls-ListViewtenet-performance

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions