Skip to content

Commit

Permalink
Fixes dotnet#8768
Browse files Browse the repository at this point in the history
  • Loading branch information
elachlan committed Dec 18, 2023
1 parent fc705dc commit 831082a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ 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;
private static int s_servicePointManagerCheckCrl;
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;

Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Text;
using System.Windows.Forms.Primitives;

namespace System.Windows.Forms;

Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit 831082a

Please sign in to comment.