winforms icon indicating copy to clipboard operation
winforms copied to clipboard

TreeNodeCollection.AddRange works incorrectly for non-empty collection of the sorted TreeView

Open snnz opened this issue 1 year ago • 12 comments

.NET version

6.0

Did it work in .NET Framework?

No

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

No

Issue description

When the tree nodes are appended to the non-empty TreeNodeCollection owned by the sorted TreeView with already created underlying window, the resulting order of items in the actual window does not match the one in the collection (which is correct).

The cause is how method TreeNode.PrevNode works: when called from within TreeNodeCollection.AddRange of non-empty collection, it always uses fixed index as the position of the previous node, assuming the normal backward insertion of the nodes in the unsorted tree. Yet in case of the sorted TreeView, insertions are made at various positions according to a sort order.

There obviously should be a check for the sorted state of the tree in TreeNode.PrevNode to avoid this.

Steps to reproduce

namespace TreeViewTest
{
  internal static class Program
  {
    [STAThread]
    static void Main()
    {
      ApplicationConfiguration.Initialize();

      var treeView = new TreeView();
      treeView.Nodes.Add(new TreeNode("2"));
      treeView.Sort();

      var form = new Form();
      form.Controls.Add(treeView);
      form.Load += (sender, e) => treeView.Nodes.AddRange(new TreeNode[] {
        new TreeNode("3"),
        new TreeNode("5"),
        new TreeNode("1"),
        new TreeNode("4")
      });
      Application.Run(form);
    }
  }
}

This results in: Clipboard01

snnz avatar Mar 08 '23 19:03 snnz

Thanks for filing this issue. I will hand this over to the community if someone has ideas for how to fix this and put it under a configuration switch. Given that this has been the behvavior since .NET Framework days, the team will not be able to prioritize it on our end.

merriemcgaw avatar Mar 09 '23 21:03 merriemcgaw

This issue is now marked as "help wanted", and we’re looking for a community volunteer to work on this issue. If we receive no interest in 180 days, we will close the issue. To learn more about how we handle feature requests, please see our documentation.

Happy Coding!

ghost avatar Mar 09 '23 21:03 ghost

I've submitted a possible fix as PR #8774; it is very simple.

snnz avatar Mar 10 '23 02:03 snnz

See #8556 for examples of context switches.

elachlan avatar Nov 03 '23 04:11 elachlan

@lonitra Here is my PR. Can you please review it.

https://github.com/dotnet/winforms/pull/11300

Proof of testing image

RutulPatel8 avatar May 01 '24 08:05 RutulPatel8

@Tanya-Solyanik Can you please review my PR. I can see the issue in XUnit Test but It is not related to the my code.

RutulPatel8 avatar May 06 '24 17:05 RutulPatel8

@RutulPatel8 - is this issue fixed by PR #11253?

Tanya-Solyanik avatar May 06 '24 18:05 Tanya-Solyanik

I'm not sure, but I can say one thing: There have been lots of changes made with these PRs, and I've made the same change by altering a single 'if' condition. Could you please review my PR?"

RutulPatel8 avatar May 06 '24 18:05 RutulPatel8

I had verified again, this is reproing in .NET Framework, my experiment was wrong because I re-sorted the TreeView after adding the unsorted range

Tanya-Solyanik avatar May 06 '24 19:05 Tanya-Solyanik

@Tanya-Solyanik It means still you have facing the same issue and my changes wasn't working with.net framework?

RutulPatel8 avatar May 06 '24 20:05 RutulPatel8

@RutulPatel8 - your change is good, except for the failing tests. All failing tests are related to treeviews and that they are not failing for other PRs.

Tanya-Solyanik avatar May 07 '24 17:05 Tanya-Solyanik

Moving to triage to confirm that we don't want an opt-in switch for this change.

Tanya-Solyanik avatar May 07 '24 17:05 Tanya-Solyanik

This has been present since .NET Framework so we will need an opt-out switch for this behavior. @Tanya-Solyanik suggests TreeNodeCollectionAddRangeRespectsSortOrder

This will potentially be a breaking change, but the current behavior is not what we'd expected.

merriemcgaw avatar May 15 '24 20:05 merriemcgaw

@RutulPatel8, are you planning on picking this one up, or should we have a team member wrap up the work? Thanks!

merriemcgaw avatar May 15 '24 20:05 merriemcgaw

Verified this issue on 9.0.100-preview.6.24312.9 with dlls built from winforms repo of main branch, it was fixed: the tree nodes added by using TreeNodeCollection.AddRange are sorted correctly image

Liv-Goh avatar Jun 13 '24 02:06 Liv-Goh

Verified the issue with .NET 9.0.100-preivew.6.24325.8 TP build. the issue has been fixed that have the same results as above.

Philip-Wang01 avatar Jun 28 '24 08:06 Philip-Wang01