Skip to content

Commit

Permalink
Fixes dotnet#8768
Browse files Browse the repository at this point in the history
  • Loading branch information
elachlan committed Feb 4, 2024
1 parent c8b7ff4 commit 12bb875
Show file tree
Hide file tree
Showing 2 changed files with 22 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 TreeNodePrevNodeDoNotUseFixedIndexWithSortedTreeViewSwitchName = "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_treeNodePrevNodeDoNotUseFixedIndexWithSortedTreeView;

private static FrameworkName? s_targetFrameworkName;

Expand Down Expand Up @@ -160,5 +162,15 @@ public static bool DataGridViewUIAStartRowCountAtZero
get => GetCachedSwitchValue(DataGridViewUIAStartRowCountAtZeroSwitchName, ref s_dataGridViewUIAStartRowCountAtZero);
}

/// <summary>
/// Indicates whether TreeNode.PrevNode uses a fixed index to return its value.
/// Thus mirroring the behaviour of the TreeNodeCollection.
/// </summary>
public static bool TreeNodePrevNodeDoNotUseFixedIndexWithSortedTreeView
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetCachedSwitchValue(TreeNodePrevNodeDoNotUseFixedIndexWithSortedTreeViewSwitchName, ref s_treeNodePrevNodeDoNotUseFixedIndexWithSortedTreeView);
}

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 @@ -812,17 +813,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.TreeNodePrevNodeDoNotUseFixedIndexWithSortedTreeView || 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 12bb875

Please sign in to comment.