@@ -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