winforms icon indicating copy to clipboard operation
winforms copied to clipboard

Do not use fixed index in the `TreeNode.PrevNode` if the `TreeView` is sorted

Open elachlan opened this issue 1 year ago • 12 comments

Fixes #8768

This is a re-roll of the PR in #8774, but with the switch added.

Microsoft Reviewers: Open in CodeFlow

elachlan avatar Dec 18 '23 00:12 elachlan

CC: @snnz

elachlan avatar Dec 18 '23 00:12 elachlan

@lonitra I added the xml comment and renamed the switch to be more specific to its behavior.

elachlan avatar Jan 02 '24 01:01 elachlan

But this re-roll, it is totally incorrect. The purpose is to make sure that fixed index is NOT used if the tree is sorted, not vice versa. See the original PR: there should be !TreeView.Sorted in the condition.

snnz avatar Jan 02 '24 05:01 snnz

@lonitra I added the xml comment and renamed the switch to be more specific to its behavior.

Thank you! The change looks good. Could you add tests to capture the behavior of the switch when it is on?

lonitra avatar Jan 03 '24 21:01 lonitra

This submission has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 14 days.

It will be closed if no further activity occurs within 7 days of this comment.

ghost avatar Jan 17 '24 22:01 ghost

Sorry! I'll try and get this sorted soon.

elachlan avatar Jan 17 '24 22:01 elachlan

@lonitra I am unsure how we can unit test this. As the behavior of affects the underlying win32 tree view control.

Edit: I think I found a solution. I will push through in a bit.

elachlan avatar Feb 05 '24 01:02 elachlan

I did some manual testing and was getting different results than what was specified in the #8768.

I then looked into the differences, we changed the behavior of the insert in #9078 by @gpetrou

Did we want to change the behavior back, or keep it as is? It used to add in "almost" reverse order, but now it adds it in the order provided (almost).

Before: 223826227-6cf6d1aa-3605-4e39-98a8-65116d5db21a

After: image

Method of adding:

  treeView1.Nodes.AddRange([
  new TreeNode("3"),
  new TreeNode("5"),
  new TreeNode("1"),
  new TreeNode("4")
]);

elachlan avatar Feb 05 '24 04:02 elachlan

Test is showing some weird behavior and I am not sure why. It doesn't line up with what I observed when manually testing.

Expected: 2,5,3,1,4 Actual: 2,3,4,5,1

elachlan avatar Feb 05 '24 06:02 elachlan

Did we want to change the behavior back, or keep it as is? It used to add in "almost" reverse order, but now it adds it in the order provided (almost).

Had you investigated what exactly caused the behavior change? From glancing at the PR it doesn't seem like the change was intentional.

Test is showing some weird behavior and I am not sure why. It doesn't line up with what I observed when manually testing.

Hm.. That is concerning. It seems like the state between our control and the underlying one is mismatched and the results you are getting are actually being sorted when it comes to the underlying control.. This looks a bit suspicious to me https://github.com/dotnet/winforms/blob/226a5b5d3782f40d8c84cdacfa3c49b3f7cc042e/src/System.Windows.Forms/src/System/Windows/Forms/Controls/TreeView/TreeNodeCollection.cs#L315-L318 this is where it looks like we eventually get to from AddRange. Could you maybe debug through this and see if somewhere along the line we are adding the tree node to a unexpected index for the underlying control? -- I cannot look into this too deepy right now, apologies!

lonitra avatar Feb 09 '24 01:02 lonitra

This submission has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 14 days.

It will be closed if no further activity occurs within 7 days of this comment.

Sorry I haven't sorted this yet. Hopefully soon.

elachlan avatar Feb 23 '24 08:02 elachlan