diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/TreeView/TreeNodeCollection.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/TreeView/TreeNodeCollection.cs index 8e1b6d1beea..72981652702 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/TreeView/TreeNodeCollection.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/TreeView/TreeNodeCollection.cs @@ -316,7 +316,7 @@ private int AddInternal(TreeNode node, int delta) } } - if (tv is not null && tv.Sorted) + if (tv is not null && tv.Sorted && _owner.Nodes.FixedIndex == -1) { return _owner.AddSorted(tv, node); } @@ -344,7 +344,7 @@ private int AddInternal(TreeNode node, int delta) tv.SelectedNode = node; // communicate this to the handle } - if (tv is not null && tv.TreeViewNodeSorter is not null) + if (tv is not null && tv.TreeViewNodeSorter is not null && _owner.Nodes.FixedIndex == -1) { tv.Sort(); } diff --git a/src/test/integration/WinformsControlsTest/Program.cs b/src/test/integration/WinformsControlsTest/Program.cs index 704e121ae65..b1edca41890 100644 --- a/src/test/integration/WinformsControlsTest/Program.cs +++ b/src/test/integration/WinformsControlsTest/Program.cs @@ -7,6 +7,9 @@ // Set STAThread Thread.CurrentThread.SetApartmentState(ApartmentState.Unknown); Thread.CurrentThread.SetApartmentState(ApartmentState.STA); + +AppContext.SetSwitch(TreeViewSortAddRangeTest.SwitchName, false); + ApplicationConfiguration.Initialize(); Application.SetColorMode(SystemColorMode.Classic); diff --git a/src/test/integration/WinformsControlsTest/TreeViewSortAddRangeTest.cs b/src/test/integration/WinformsControlsTest/TreeViewSortAddRangeTest.cs index 607f7cdd75c..b9f2cf7c1ec 100644 --- a/src/test/integration/WinformsControlsTest/TreeViewSortAddRangeTest.cs +++ b/src/test/integration/WinformsControlsTest/TreeViewSortAddRangeTest.cs @@ -117,16 +117,16 @@ private void ShowScenario(bool useAddRange) _treeView.EndUpdate(); string method = useAddRange ? "AddRange(nodes)" : "Add(node) in a loop"; - string order = string.Join(", ", _treeView.Nodes.Cast().Select(n => n.Text)); - string input = string.Join(", ", nodes.Select(n => n.Text)); + string output = JoinNodeText(_treeView.Nodes.Cast()); + string input = JoinNodeText(nodes); StringBuilder sb = new(); sb.AppendLine(DescribeEffectiveSwitch()); sb.AppendLine(); sb.AppendLine($"Insert method : {method}"); - sb.AppendLine($"Input order : {input}"); - sb.AppendLine($"Result order : {order}"); - sb.AppendLine($"Reversed? : {(order == input ? "no (stable)" : "YES - equal nodes reversed")}"); + sb.AppendLine($"Input: {input}"); + sb.AppendLine($"Output: {output}"); + sb.AppendLine($"Reversed? : {(output == input ? "no (stable)" : "YES - equal nodes reversed")}"); _output.Text = sb.ToString(); } @@ -143,9 +143,8 @@ private void CompareSwitchInChildProcesses() sb.AppendLine("Each line below is produced by a SEPARATE child process of this exact build,"); sb.AppendLine("so the AppContext switch is applied cleanly before the first AddRange call."); sb.AppendLine(); - sb.AppendLine(RunChild(self, ReportArg)); - sb.AppendLine(RunChild(self, $"{ReportArg} --switch:true")); sb.AppendLine(RunChild(self, $"{ReportArg} --switch:false")); + sb.AppendLine(RunChild(self, $"{ReportArg} --switch:true")); _output.Text = sb.ToString(); } @@ -224,13 +223,21 @@ internal static bool TryRunConsoleReport(string[] args, out int exitCode) treeView.CreateControl(); treeView.Nodes.AddRange(nodes); - string order = string.Join(", ", treeView.Nodes.Cast().Select(n => n.Text)); - string input = string.Join(", ", nodes.Select(n => n.Text)); - string state = AppContext.TryGetSwitch(SwitchName, out bool v) ? v.ToString() : ""; - Console.Out.WriteLine($"switch={state,-12} input=[{input}] result=[{order}] reversed={(order != input)}"); + string output = JoinNodeText(treeView.Nodes.Cast()); + string input = JoinNodeText(nodes); + string state = AppContext.TryGetSwitch(SwitchName, out bool v) + ? (v ? "ON" : "OFF") + : ""; + + Console.Out.WriteLine($"switch {state} ->"); + Console.Out.WriteLine($"Input: {input}"); + Console.Out.WriteLine($"Output: {output}"); + Console.Out.WriteLine($"Reversed: {output != input}"); return true; } + private static string JoinNodeText(IEnumerable nodes) => string.Join(",", nodes.Select(n => n.Text)); + // A sorter that treats every node as equal, like several identically described items. private sealed class AlwaysEqualComparer : IComparer {