From 831082a364961c230e9faff704e55945c4e91a18 Mon Sep 17 00:00:00 2001 From: Lachlan Ennis <2433737+elachlan@users.noreply.github.com> Date: Mon, 18 Dec 2023 10:10:26 +1000 Subject: [PATCH] Fixes #8768 --- .../LocalAppContextSwitches.cs | 8 ++++++++ .../Windows/Forms/Controls/TreeView/TreeNode.cs | 16 ++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/System.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs b/src/System.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs index 858899fcb56..f3d3212281f 100644 --- a/src/System.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs +++ b/src/System.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs @@ -21,6 +21,7 @@ internal static partial class LocalAppContextSwitches internal const string TrackBarModernRenderingSwitchName = "System.Windows.Forms.TrackBarModernRendering"; private const string DoNotCatchUnhandledExceptionsSwitchName = "System.Windows.Forms.DoNotCatchUnhandledExceptions"; internal const string DataGridViewUIAStartRowCountAtZeroSwitchName = "System.Windows.Forms.DataGridViewUIAStartRowCountAtZero"; + internal const string TreeNodeDoNotUseFixedIndexWithSortedTreeViewSwitchName = "System.Windows.Forms.TreeNodeDoNotUseFixedIndexWithSortedTreeView"; private static int s_scaleTopLevelFormMinMaxSizeForDpi; private static int s_anchorLayoutV2; @@ -28,6 +29,7 @@ internal static partial class LocalAppContextSwitches private static int s_trackBarModernRendering; private static int s_doNotCatchUnhandledExceptions; private static int s_dataGridViewUIAStartRowCountAtZero; + private static int s_treeNodeDoNotUseFixedIndexWithSortedTreeView; private static FrameworkName? s_targetFrameworkName; @@ -165,5 +167,11 @@ public static bool DataGridViewUIAStartRowCountAtZero get => GetCachedSwitchValue(DataGridViewUIAStartRowCountAtZeroSwitchName, ref s_dataGridViewUIAStartRowCountAtZero); } + public static bool TreeNodeDoNotUseFixedIndexWithSortedTreeView + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => GetCachedSwitchValue(TreeNodeDoNotUseFixedIndexWithSortedTreeViewSwitchName, ref s_treeNodeDoNotUseFixedIndexWithSortedTreeView); + } + internal static void SetDataGridViewUIAStartRowCountAtZero(bool value) => s_dataGridViewUIAStartRowCountAtZero = value ? 1 : 0; } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/TreeView/TreeNode.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/TreeView/TreeNode.cs index d8db329a18e..aabdd7327e1 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/TreeView/TreeNode.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/TreeView/TreeNode.cs @@ -9,6 +9,7 @@ using System.Runtime.InteropServices; using System.Runtime.Serialization; using System.Text; +using System.Windows.Forms.Primitives; namespace System.Windows.Forms; @@ -815,17 +816,20 @@ public TreeNodeCollection Nodes } // fixedIndex is used for perf. optimization in case of adding big ranges of nodes - int currentInd = _index; - int fixedInd = _parent.Nodes.FixedIndex; + int currentIndex = _index; + int fixedIndex = _parent.Nodes.FixedIndex; - if (fixedInd > 0) + if (fixedIndex > 0) { - currentInd = fixedInd; + if (!LocalAppContextSwitches.TreeNodeDoNotUseFixedIndexWithSortedTreeView || TreeView is null || TreeView.Sorted) + { + currentIndex = fixedIndex; + } } - if (currentInd > 0 && currentInd <= _parent.Nodes.Count) + if (currentIndex > 0 && currentIndex <= _parent.Nodes.Count) { - return _parent.Nodes[currentInd - 1]; + return _parent.Nodes[currentIndex - 1]; } else {