Click here to Skip to main content
15,905,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What My Question is, in my Windows forms application i have task like i have textbox,
which will filled with Employee Name and that name should be already existin Employee name.
for that i have taken one button. when i click on that button, i display a popup window.In that window i display the employee details in datagridview when i click on the corresponding cell with mouse that popup will be closed.

But what my requirement is to reduce the mouse usage,i need to select the corresponding cell with Enterkey. but EnterKey default action will be when i click the key it will move to next row cell.
i want to restrict the default action of the Enter key and also assign the click event to enterKey to select the corresponding cell as like the mouseclick event...

i have tried it with KeyPreveiew property of the form But i didn't succeeded....

Please send me the answer if any body knows...what to do....
Posted
Updated 26-Sep-11 20:46pm
v3

1 solution

First, I think you are making a mistake by planning to over-ride the expected behavior of the DataGridView when it gets an Enter/Return keypress while a cell is selected.

But here's a 'sketch' for code to use:
C#
// this code has not been tested

// if using international fonts this may have
// to be modified to take into account Locale, etc. ? See MSDN.
private char EnterChar = (char)13;

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
    // note that 'SelectedCells is never equal to 'null
    if (e.KeyChar == EnterChar && dataGridView1.SelectedCells.Count == 1)
    {
        // close the pop-up code here
        //
        // ignore this keypress
        e.Handled = true;
    }
}
 
Share this answer
 
Comments
Simon Bang Terkildsen 2-Oct-11 20:29pm    
My 5. And I agree that it's a bad idea, in almost all cases, to change the standard behavior of controls, as it'll only serve to make the system harder to understand. And believe it or not but there has been done a lot of usability tests on the controls.
V G S Naidu A 2-Oct-11 23:41pm    
Thank you very much sir...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900