Skip to content

Use AddRange switch to preserve equal-node order#29

Open
Rick-Xu-WTG wants to merge 1 commit into
RX/WI01088201-Treeview-node-order-is-reversed-after-reopenfrom
RX/WI01088201-fix-addrange-switch-equal-nodes
Open

Use AddRange switch to preserve equal-node order#29
Rick-Xu-WTG wants to merge 1 commit into
RX/WI01088201-Treeview-node-order-is-reversed-after-reopenfrom
RX/WI01088201-fix-addrange-switch-equal-nodes

Conversation

@Rick-Xu-WTG

@Rick-Xu-WTG Rick-Xu-WTG commented Jun 18, 2026

Copy link
Copy Markdown

What

Use the existing WinForms AppContext switch to restore the legacy AddRange order for the demo scenario:

AppContext.SetSwitch("System.Windows.Forms.TreeNodeCollectionAddRangeRespectsSortOrder", false);

The demo now sets the switch at startup before the first sorted AddRange call.

Why

The reversal happens only when the Pallet nodes compare equal. AddRange iterates nodes backwards, and AddSorted inserts a new equal item after existing equal items. With equal comparisons, that turns the input order into a full reverse.

The switch from dotnet#11423 already controls whether AddRange sets FixedIndex, but Sorted=true still entered AddSorted before the fixed-index path could matter. This PR keeps the source change minimal: while AddRange is using FixedIndex, skip the sorted insertion/sort path so the switch can actually restore the legacy order.

Verification

==== --treeview-addrange-report ====
switch OFF ->
Input:  Pallet 1,Pallet 2,Pallet 3,Pallet 4,Pallet 5
Output: Pallet 1,Pallet 2,Pallet 3,Pallet 4,Pallet 5
Reversed: False

==== --treeview-addrange-report --switch:false ====
switch OFF ->
Input:  Pallet 1,Pallet 2,Pallet 3,Pallet 4,Pallet 5
Output: Pallet 1,Pallet 2,Pallet 3,Pallet 4,Pallet 5
Reversed: False

==== --treeview-addrange-report --switch:true ====
switch ON ->
Input:  Pallet 1,Pallet 2,Pallet 3,Pallet 4,Pallet 5
Output: Pallet 5,Pallet 4,Pallet 3,Pallet 2,Pallet 1
Reversed: True

Builds:

.\.dotnet\dotnet.exe build src\test\integration\WinformsControlsTest\WinformsControlsTest.csproj -c Debug --no-restore
.\.dotnet\dotnet.exe build src\test\unit\System.Windows.Forms\System.Windows.Forms.Tests.csproj -c Debug --no-restore

Set TreeNodeCollectionAddRangeRespectsSortOrder to false in the demo startup and make the existing AddRange switch affect the fixed-index path while AddRange is in progress.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Rick-Xu-WTG Rick-Xu-WTG force-pushed the RX/WI01088201-fix-addrange-switch-equal-nodes branch from b039efa to 3e57e9e Compare June 18, 2026 13:54
@Rick-Xu-WTG Rick-Xu-WTG changed the title Make AddRange switch preserve equal-node order Use AddRange switch to preserve equal-node order Jun 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant