How can I get the Accept button to activate the DataGridView function?

I am presenting a form as a dialog box. The form contains DataGridView, TextBox and OK / Cancel buttons as shown below:

enter image description here

  • I set the form's AcceptButton property to the OK button and the form's CancelButton property to the Cancel button.
  • I set the DialogResult property of the OK button to OK and the DialogResult property of the Cancel button to Cancel

If the text field has focus, then pressing Enter closes the form with DialogResult in OK. However, if the DataGridView has focus, then pressing Enter does not close the form.

However, pressing the Escape key always closes the form with the DialogResult of Cancel.

This is a two-part question:

  • What is the reason for the inconsistent behavior of the Enter key when focusing the DataGridView?
  • How can I call Enter to close the form using DialogResult on OK when the DataGridView has focus?
+3


source to share


4 answers


  • I suppose that input is a valid key to enter data, and also the tab and the one they want to keep for those users who are most used to the keyboard, as opposed to point and click.

  • Have you tried adding a PerformClick () call , perhaps in your key-down event handler?



+2


source


DataGridView

uses the enter key to move to the cell below the cell being edited. There is no single property to change this behavior, but you can override the keyboard's behavior on the grid:

dataGridView1.KeyDown += new KeyEventHandler(dataGridView1_KeyDown);

void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        button1.PerformClick();
        e.Handled = true;        
    }    
}

      



This still leaves you with arrows to navigate and allows users to add new rows (a new row appears as soon as data is entered at the bottom row of the grid).

+5


source


  • The cell DataGridView

    handles the event KeyDown

    internally if KeyCode

    - Enter (if the cell is in edit mode, pressing Enter means "I finished editing the cell". If the entire row is selected, pressing Enter means "Add a new row").
  • I am assuming that you do not want to add a new line when the user presses Enter. Set the property DataGridView.AllowUsersToAddNewRows

    to false first to prevent users from receiving any unexpected action, and then process the event DataGridView.KeyDown

    and manually click the button when the event was raised with KeyCode.Enter

    .

    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if(e.KeyCode == Keys.Enter)
        {
            button1.PerformClick();
        }
    }
    
          

+3


source


I'm not sure if I fully understand this question, but I will best go. Let me know if I am missing something.

The way you should do this is to add a Keypress event to the form to handle the Enter / Escape key presses.

This is for adding a keypress event to the code behind (can use a constructor).

this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);

      

An event handler could be something like this:

void SelectionPageForAutomation_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == Key.Escape)
        {
            btn_Cancel.PerformClick();
        }
        else if(e.KeyChar == Key.Enter){
            btn_Okay.PerformClick();
        }
    }

      

0


source







All Articles