Skip to content

Commit 15bf890

Browse files
[main] Source code updates from dotnet/winforms (#8845)
[main] Source code updates from dotnet/winforms
1 parent 1ec061e commit 15bf890

3 files changed

Lines changed: 121 additions & 2 deletions

File tree

‎src/source-manifest.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@
121121
"commitSha": "8170e85d94e8c294b277b8b88b779b61baf8955b"
122122
},
123123
{
124-
"barId": 329255,
124+
"barId": 329402,
125125
"path": "winforms",
126126
"remoteUri": "https://github.com/dotnet/winforms",
127-
"commitSha": "699055a53e0398ac45602d7e5809db78ffa4c409"
127+
"commitSha": "874c4f67c015fcf1534311a498694b6ae0671624"
128128
},
129129
{
130130
"barId": 328041,

‎src/winforms/src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridView.Methods.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21929,6 +21929,14 @@ protected override bool ProcessKeyEventArgs(ref Message m)
2192921929

2193021930
protected override bool ProcessKeyPreview(ref Message m)
2193121931
{
21932+
if (m.MsgInternal == PInvokeCore.WM_KEYDOWN || m.MsgInternal == PInvokeCore.WM_SYSKEYDOWN || m.MsgInternal == PInvokeCore.WM_CHAR)
21933+
{
21934+
if (m.HWND != HWND && (EditingControl is null || m.HWND != EditingControl.HWND))
21935+
{
21936+
return base.ProcessKeyPreview(ref m);
21937+
}
21938+
}
21939+
2193221940
bool dataGridViewWantsInputKey;
2193321941
KeyEventArgs ke = new((Keys)(nint)m.WParamInternal | ModifierKeys);
2193421942

‎src/winforms/src/test/unit/System.Windows.Forms/System/Windows/Forms/DataGridViewTests.cs‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4084,4 +4084,115 @@ public void DataGridView_Dispose_KeyboardToolTip_Disposed()
40844084
dataGridView.Dispose();
40854085
toolTipDisposeCount.Should().Be(1);
40864086
}
4087+
4088+
[WinFormsFact]
4089+
public void ProcessKeyPreview_HostedChild_ArrowKey_DoesNotRouteToDataGridView()
4090+
{
4091+
using Form form = new();
4092+
using TestDataGridView dataGridView = CreateGrid();
4093+
using TextBox hostedTextBox = new();
4094+
4095+
form.Controls.Add(dataGridView);
4096+
dataGridView.Controls.Add(hostedTextBox);
4097+
4098+
form.Show();
4099+
dataGridView.CreateControl();
4100+
hostedTextBox.CreateControl();
4101+
hostedTextBox.Focus();
4102+
4103+
dataGridView.CurrentCell = dataGridView[0, 0];
4104+
4105+
Message message = Message.Create(
4106+
hostedTextBox.Handle,
4107+
(int)PInvokeCore.WM_KEYDOWN,
4108+
(IntPtr)Keys.Down,
4109+
IntPtr.Zero);
4110+
4111+
dataGridView.CallProcessKeyPreview(ref message);
4112+
4113+
Assert.False(dataGridView.ProcessDataGridViewKeyCalled);
4114+
Assert.Equal(0, dataGridView.CurrentCell.RowIndex);
4115+
}
4116+
4117+
[WinFormsFact]
4118+
public void ProcessKeyPreview_DataGridViewTarget_ArrowKey_StillRoutesToDataGridView()
4119+
{
4120+
using Form form = new();
4121+
using TestDataGridView dataGridView = CreateGrid();
4122+
4123+
form.Controls.Add(dataGridView);
4124+
form.Show();
4125+
dataGridView.CreateControl();
4126+
dataGridView.Focus();
4127+
4128+
dataGridView.CurrentCell = dataGridView[0, 0];
4129+
4130+
Message message = Message.Create(
4131+
dataGridView.Handle,
4132+
(int)PInvokeCore.WM_KEYDOWN,
4133+
(IntPtr)Keys.Down,
4134+
IntPtr.Zero);
4135+
4136+
dataGridView.CallProcessKeyPreview(ref message);
4137+
4138+
Assert.True(dataGridView.ProcessDataGridViewKeyCalled);
4139+
}
4140+
4141+
[WinFormsFact]
4142+
public void ProcessKeyPreview_HostedChild_NonArrowKey_DoesNotUseArrowSuppression()
4143+
{
4144+
using Form form = new();
4145+
using TestDataGridView dataGridView = CreateGrid();
4146+
using TextBox hostedTextBox = new();
4147+
4148+
form.Controls.Add(dataGridView);
4149+
dataGridView.Controls.Add(hostedTextBox);
4150+
4151+
form.Show();
4152+
dataGridView.CreateControl();
4153+
hostedTextBox.CreateControl();
4154+
hostedTextBox.Focus();
4155+
4156+
Message message = Message.Create(
4157+
hostedTextBox.Handle,
4158+
(int)PInvokeCore.WM_KEYDOWN,
4159+
(IntPtr)Keys.Enter,
4160+
IntPtr.Zero);
4161+
4162+
dataGridView.CallProcessKeyPreview(ref message);
4163+
4164+
// This test is weaker than the arrow-key tests, but still verifies
4165+
// that the arrow-specific logic is not applied to Enter.
4166+
Assert.False(dataGridView.ProcessDataGridViewKeyCalled);
4167+
}
4168+
4169+
private static TestDataGridView CreateGrid()
4170+
{
4171+
TestDataGridView grid = new()
4172+
{
4173+
Width = 300,
4174+
Height = 200
4175+
};
4176+
4177+
grid.Columns.Add("Col1", "Col1");
4178+
grid.Columns.Add("Col2", "Col2");
4179+
grid.Rows.Add("A1", "B1");
4180+
grid.Rows.Add("A2", "B2");
4181+
4182+
return grid;
4183+
}
4184+
4185+
private sealed class TestDataGridView : DataGridView
4186+
{
4187+
public bool ProcessDataGridViewKeyCalled { get; private set; }
4188+
4189+
public bool CallProcessKeyPreview(ref Message m)
4190+
=> ProcessKeyPreview(ref m);
4191+
4192+
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
4193+
{
4194+
ProcessDataGridViewKeyCalled = true;
4195+
return base.ProcessDataGridViewKey(e);
4196+
}
4197+
}
40874198
}

0 commit comments

Comments
 (0)