Form key events are not raised when you type in a DataGrid cell with the KeyPreview property set to true (815252)



The information in this article applies to:

  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

SYMPTOMS

When you set the KeyPreview property of the form to True, and you type in the cell of a DataGrid control, the Key events of the form (KeyUp, KeyDown) do not raise. However, when you press any key while focus is on the title, the column, or the lines of the DataGrid, the Key events of the form are raised as expected.

CAUSE

When the DataGrid cell has input focus, the Key events that were passed to the DataGrid control are first assigned to the DataGrid cell event handlers. The DataGrid cell does not support any Key events. The Key events are not enabled in the DataGrid to allow users to navigate inside the DataGrid control by using the keyboard. Therefore, Key events are not passed from the DataGrid cell to the parent Windows form, and the parent form Key event does raise.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Windows application project by using Visual Basic .NET or Visual C# .NET.

    By default Form1 is created.
  2. Right-click Form1, and then click Properties.
  3. In the Properties window, set the KeyPreview property to True.
  4. Drag a TextBox control and a DataGrid control to the form.
  5. Right-click DataGrid1, and then click Properties.
  6. Set the ReadOnly property to False.
  7. Add the following code in the Form1_KeyPress event handler:

    Visual Basic .NET Code:
    MessageBox.Show("In Form1_ KeyPress Event Handler")
    Visual C# .NET Code:
    MessageBox.Show("In Form1_ KeyPress Event Handler");
  8. Add the following code in the TextBox1_KeyPress event handler:

    Visual Basic .NET Code:
    MessageBox.Show("In TextBox1_ KeyPress Event Handler")
    Visual C# .NET Code:
    MessageBox.Show("In TextBox1_ KeyPress Event Handler");
  9. Add the following code in the DataGrid1_KeyPress event handler:

    Visual Basic .NET Code:
    MessageBox.Show("In DataGrid1_ KeyPress Event Handler")
    Visual C# .NET Code:
    MessageBox.Show("In DataGrid1_ KeyPress Event Handler");
  10. On the Data tab, double-click SqlDataAdapter, and then click Next.
  11. On the Choose your data connection page, click New Connection.

    The Data Link Properties window is displayed.
  12. Type the database server name, username, and password, and then click to select the Northwind database. Click OK.
  13. Click to select the data connection that you created in Step 12, and then click Next.
  14. Under Choose a Query Type, click Next. Type Select * from customers on the Generate the SQL Statements page, and then click Finish.
  15. Right-click SqlDataAdapter1, and then click Generate DataSet.

    By Default, DataSet11 is created.
  16. Click to select the DataGrid control, and then press the F4 key. In the Properties window, set the DataSource to the DataSet11.Customers that was generated in step 14.
  17. On the Build menu, click Build Solution to build the project.
  18. Run the application.
  19. In the TextBox, type any text.

    You receive the In Form1_ KeyPress Event Handler message and the In TextBox1_ KeyPress Event Handler message.
  20. Click to select a cell in the DataGrid, and then type the text in the cell.

    The message is not displayed.

REFERENCES

For more information, visit the following Microsoft Web site:

Form.KeyPreview Property
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsformsformclasskeypreviewtopic.asp

Modification Type:MajorLast Reviewed:1/25/2006
Keywords:kbvs2005swept kbvs2005doesnotapply kbControl kbWindowsForms kbEvent kbProgramming kbprb KB815252 kbAudDeveloper