Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ internal static partial class LocalAppContextSwitches
internal const string ServicePointManagerCheckCrlSwitchName = "System.Windows.Forms.ServicePointManagerCheckCrl";
internal const string TrackBarModernRenderingSwitchName = "System.Windows.Forms.TrackBarModernRendering";
private const string DoNotCatchUnhandledExceptionsSwitchName = "System.Windows.Forms.DoNotCatchUnhandledExceptions";
internal const string DataGridViewUIAStartRowCountAtZeroSwitchName = "System.Windows.Forms.DataGridViewUIAStartRowCountAtZero";

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 FrameworkName? s_targetFrameworkName;

Expand Down Expand Up @@ -151,4 +153,12 @@ public static bool ServicePointManagerCheckCrl
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetCachedSwitchValue(ServicePointManagerCheckCrlSwitchName, ref s_servicePointManagerCheckCrl);
}

public static bool DataGridViewUIAStartRowCountAtZero
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetCachedSwitchValue(DataGridViewUIAStartRowCountAtZeroSwitchName, ref s_dataGridViewUIAStartRowCountAtZero);
}

internal static void SetDataGridViewUIAStartRowCountAtZero(bool value) => s_dataGridViewUIAStartRowCountAtZero = value ? 1 : 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms.Primitives;
using static Interop;

namespace System.Windows.Forms;
Expand Down Expand Up @@ -55,9 +56,9 @@ public override string? Name

int rowIndex = _owner.DataGridView is null
? -1
: _owner.DataGridView.Rows.GetVisibleIndex(_owner.OwningRow);
: _owner.DataGridView.Rows.GetVisibleIndex(_owner.OwningRow) + RowStartIndex;

string name = string.Format(SR.DataGridView_AccDataGridViewCellName, _owner.OwningColumn.HeaderText, rowIndex);
string name = string.Format(SR.DataGridView_AccDataGridViewCellName, _owner.OwningColumn.HeaderText, rowIndex).Trim();

if (_owner.OwningColumn.SortMode != DataGridViewColumnSortMode.NotSortable)
{
Expand Down Expand Up @@ -113,6 +114,8 @@ private AccessibleObject? ParentPrivate

public override AccessibleRole Role => AccessibleRole.Cell;

private static int RowStartIndex => LocalAppContextSwitches.DataGridViewUIAStartRowCountAtZero ? 0 : 1;

public override AccessibleStates State
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Drawing;
using System.Globalization;
using System.Text;
using System.Windows.Forms.Primitives;
using static Interop;

namespace System.Windows.Forms;
Expand Down Expand Up @@ -94,8 +95,7 @@ public override string Name
}

int index = _owningDataGridViewRow is { Visible: true, DataGridView: { } }
? _owningDataGridViewRow.DataGridView.Rows.GetVisibleIndex(_owningDataGridViewRow)
: -1;
? _owningDataGridViewRow.DataGridView.Rows.GetVisibleIndex(_owningDataGridViewRow) + RowStartIndex : -1;

return string.Format(SR.DataGridView_AccRowName, index.ToString(CultureInfo.CurrentCulture));
}
Expand Down Expand Up @@ -132,6 +132,8 @@ private AccessibleObject? ParentPrivate

public override AccessibleRole Role => AccessibleRole.Row;

private static int RowStartIndex => LocalAppContextSwitches.DataGridViewUIAStartRowCountAtZero ? 0 : 1;

internal override int[] RuntimeId
=> _runtimeId ??= new int[]
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Drawing;
using System.Windows.Forms.Primitives;
using Moq;
using Moq.Protected;
using static Interop;
Expand Down Expand Up @@ -207,6 +208,7 @@ public void DataGridViewCellAccessibleObject_Name_ReturnExpected_IfDataGridViewN
Assert.Equal(expected, accessibleObject.Name);
}

// Whether UIA row indexing is 1-based or 0-based, is controlled by the DataGridViewUIAStartRowCountAtZero switch
[WinFormsFact]
public void DataGridViewCellAccessibleObject_Name_ReturnExpected()
{
Expand All @@ -218,12 +220,13 @@ public void DataGridViewCellAccessibleObject_Name_ReturnExpected()
dataGridView.Rows.Add("3");

AccessibleObject accessibleObject = dataGridView.Rows[2].Cells[0].AccessibilityObject;
string expected = string.Format(SR.DataGridView_AccDataGridViewCellName, column.HeaderText, 2);
string expected = string.Format(SR.DataGridView_AccDataGridViewCellName, column.HeaderText, 3);

Assert.Equal(expected, accessibleObject.Name);
Assert.False(dataGridView.IsHandleCreated);
}

// Whether UIA row indexing is 1-based or 0-based, is controlled by the DataGridViewUIAStartRowCountAtZero switch
[WinFormsFact]
public void DataGridViewCellAccessibleObject_Name_ReturnExpected_IfOneRowHidden()
{
Expand All @@ -236,12 +239,13 @@ public void DataGridViewCellAccessibleObject_Name_ReturnExpected_IfOneRowHidden(
dataGridView.Rows[0].Visible = false;

AccessibleObject accessibleObject = dataGridView.Rows[2].Cells[0].AccessibilityObject;
string expected = string.Format(SR.DataGridView_AccDataGridViewCellName, column.HeaderText, 1);
string expected = string.Format(SR.DataGridView_AccDataGridViewCellName, column.HeaderText, 2);

Assert.Equal(expected, accessibleObject.Name);
Assert.False(dataGridView.IsHandleCreated);
}

// Whether UIA row indexing is 1-based or 0-based, is controlled by the DataGridViewUIAStartRowCountAtZero switch
[WinFormsFact]
public void DataGridViewCellAccessibleObject_Name_ReturnExpected_IfTwoRowsHidden()
{
Expand All @@ -255,7 +259,7 @@ public void DataGridViewCellAccessibleObject_Name_ReturnExpected_IfTwoRowsHidden
dataGridView.Rows[1].Visible = false;

AccessibleObject accessibleObject = dataGridView.Rows[2].Cells[0].AccessibilityObject;
string expected = string.Format(SR.DataGridView_AccDataGridViewCellName, column.HeaderText, 0);
string expected = string.Format(SR.DataGridView_AccDataGridViewCellName, column.HeaderText, 1);

Assert.Equal(expected, accessibleObject.Name);
Assert.False(dataGridView.IsHandleCreated);
Expand Down Expand Up @@ -1432,6 +1436,23 @@ private DataGridView CreateDataGridView(int columnCount, bool createControl = tr
return dataGridView;
}

// Unit test for https://github.com/dotnet/winforms/issues/7154
[WinFormsFact]
public void DataGridView_SwitchConfigured_AdjustsCellRowStartIndices()
{
LocalAppContextSwitches.SetDataGridViewUIAStartRowCountAtZero(true);

using DataGridView dataGridView = new();
dataGridView.Columns.Add(new DataGridViewTextBoxColumn());
dataGridView.Rows.Add(new DataGridViewRow());

Assert.Equal($"{string.Format(SR.DataGridView_AccRowName, 0)}, Not sorted.", dataGridView.Rows[0].Cells[0].AccessibilityObject.Name);

LocalAppContextSwitches.SetDataGridViewUIAStartRowCountAtZero(false);

Assert.Equal($"{string.Format(SR.DataGridView_AccRowName, 1)}, Not sorted.", dataGridView.Rows[0].Cells[0].AccessibilityObject.Name);
}

private class SubDataGridViewCell : DataGridViewCell
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Drawing;
using System.Windows.Forms.Primitives;
using static Interop;

namespace System.Windows.Forms.Tests;
Expand Down Expand Up @@ -71,6 +72,7 @@ public void DataGridViewRowAccessibleObject_Name_Get_ReturnsExpected_IfDataGridV
Assert.Equal(string.Format(SR.DataGridView_AccRowName, -1), accessibilityObject.Name);
}

// Whether UIA row indexing is 1-based or 0-based, is controlled by the DataGridViewUIAStartRowCountAtZero switch
[Fact]
public void DataGridViewRowAccessibleObject_Name_Get_ReturnsExpected()
{
Expand All @@ -84,9 +86,9 @@ public void DataGridViewRowAccessibleObject_Name_Get_ReturnsExpected()
AccessibleObject accessibleObject2 = dataGridView.Rows[1].AccessibilityObject;
AccessibleObject accessibleObject3 = dataGridView.Rows[2].AccessibilityObject;

Assert.Equal(string.Format(SR.DataGridView_AccRowName, 0), accessibleObject1.Name);
Assert.Equal(string.Format(SR.DataGridView_AccRowName, 1), accessibleObject2.Name);
Assert.Equal(string.Format(SR.DataGridView_AccRowName, 2), accessibleObject3.Name);
Assert.Equal(string.Format(SR.DataGridView_AccRowName, 1), accessibleObject1.Name);
Assert.Equal(string.Format(SR.DataGridView_AccRowName, 2), accessibleObject2.Name);
Assert.Equal(string.Format(SR.DataGridView_AccRowName, 3), accessibleObject3.Name);
Assert.False(dataGridView.IsHandleCreated);
}

Expand All @@ -105,11 +107,12 @@ public void DataGridViewRowAccessibleObject_Name_Get_ReturnsExpected_IfFirstRowH
AccessibleObject accessibleObject3 = dataGridView.Rows[2].AccessibilityObject;

Assert.Equal(string.Format(SR.DataGridView_AccRowName, -1), accessibleObject1.Name);
Assert.Equal(string.Format(SR.DataGridView_AccRowName, 0), accessibleObject2.Name);
Assert.Equal(string.Format(SR.DataGridView_AccRowName, 1), accessibleObject3.Name);
Assert.Equal(string.Format(SR.DataGridView_AccRowName, 1), accessibleObject2.Name);
Assert.Equal(string.Format(SR.DataGridView_AccRowName, 2), accessibleObject3.Name);
Assert.False(dataGridView.IsHandleCreated);
}

// Whether UIA row indexing is 1-based or 0-based, is controlled by the DataGridViewUIAStartRowCountAtZero switch
[Fact]
public void DataGridViewRowAccessibleObject_Name_Get_ReturnsExpected_IfSecondRowHidden()
{
Expand All @@ -124,12 +127,13 @@ public void DataGridViewRowAccessibleObject_Name_Get_ReturnsExpected_IfSecondRow
AccessibleObject accessibleObject2 = dataGridView.Rows[1].AccessibilityObject;
AccessibleObject accessibleObject3 = dataGridView.Rows[2].AccessibilityObject;

Assert.Equal(string.Format(SR.DataGridView_AccRowName, 0), accessibleObject1.Name);
Assert.Equal(string.Format(SR.DataGridView_AccRowName, 1), accessibleObject1.Name);
Assert.Equal(string.Format(SR.DataGridView_AccRowName, -1), accessibleObject2.Name);
Assert.Equal(string.Format(SR.DataGridView_AccRowName, 1), accessibleObject3.Name);
Assert.Equal(string.Format(SR.DataGridView_AccRowName, 2), accessibleObject3.Name);
Assert.False(dataGridView.IsHandleCreated);
}

// Whether UIA row indexing is 1-based or 0-based, is controlled by the DataGridViewUIAStartRowCountAtZero switch
[Fact]
public void DataGridViewRowAccessibleObject_Name_Get_ReturnsExpected_IfLastRowHidden()
{
Expand All @@ -144,8 +148,8 @@ public void DataGridViewRowAccessibleObject_Name_Get_ReturnsExpected_IfLastRowHi
AccessibleObject accessibleObject2 = dataGridView.Rows[1].AccessibilityObject;
AccessibleObject accessibleObject3 = dataGridView.Rows[2].AccessibilityObject;

Assert.Equal(string.Format(SR.DataGridView_AccRowName, 0), accessibleObject1.Name);
Assert.Equal(string.Format(SR.DataGridView_AccRowName, 1), accessibleObject2.Name);
Assert.Equal(string.Format(SR.DataGridView_AccRowName, 1), accessibleObject1.Name);
Assert.Equal(string.Format(SR.DataGridView_AccRowName, 2), accessibleObject2.Name);
Assert.Equal(string.Format(SR.DataGridView_AccRowName, -1), accessibleObject3.Name);
Assert.False(dataGridView.IsHandleCreated);
}
Expand Down Expand Up @@ -2364,6 +2368,23 @@ public void DataGridViewRowAccessibleObject_GetChildCount_ReturnsZero_IfRowHeade
Assert.False(dataGridView.IsHandleCreated);
}

// Unit test for https://github.com/dotnet/winforms/issues/7154
[WinFormsFact]
public void DataGridView_SwitchConfigured_AdjustsRowStartIndices()
{
LocalAppContextSwitches.SetDataGridViewUIAStartRowCountAtZero(true);

using DataGridView dataGridView = new();
dataGridView.Columns.Add(new DataGridViewTextBoxColumn());
dataGridView.Rows.Add(new DataGridViewRow());

Assert.Equal(string.Format(SR.DataGridView_AccRowName, 0), dataGridView.Rows[0].AccessibilityObject.Name);

LocalAppContextSwitches.SetDataGridViewUIAStartRowCountAtZero(false);

Assert.Equal(string.Format(SR.DataGridView_AccRowName, 1), dataGridView.Rows[0].AccessibilityObject.Name);
}

[WinFormsFact]
public void DataGridViewRowAccessibleObject_GetPropertyValue_ValueValuePropertyId_ReturnsExpected()
{
Expand Down