Link to home
Start Free TrialLog in
Avatar of manjeetbothra
manjeetbothra

asked on

&Next as button name makes the button work even when only 'N' is pressed.

I have an application where on a form theres a button with text on it as "&Next" meaning when we press alt+N then the button should function as if its clicked.
But If I press only 'N' then it should not do anything, which is not the case with my application. If I press 'N' only then also its responding as if its clicked( There is no function which is trapping the key movements and doing some manipulations.) I want to make sure that this does not happen. How to solve it?
Avatar of manjeetbothra
manjeetbothra

ASKER

The application is in C#
ASKER CERTIFIED SOLUTION
Avatar of dkloeck
dkloeck
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You have to capture event Keypress of the form (and all control if it can focus), check if it's key 'N' and do somethings.
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs
e)
  if (e.KeyData == Keys.A)
  {
      OnButtonNextPressed();
  }
}
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs
e)
  if (e.KeyData == Keys.N)
  {
      OnButtonNextPressed();
  }
}
The problem is that even if I write the KeyDown(pressed/ or whatever) function, the
"button click" event is being generated which works as if button is being clicked and hence calls the NEXT() function. Helppp
Have you tried setting the focus or Tab order to some other control? Sometimes if certain controls or parent controls have the focus then pressing the special key will activate that control.
can you show us your KeyPressed event funktion?

private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs
e)
{
  if ((e.Alt) && (e.KeyData == Keys.N))
  {
      //Here goes your button funktion
      OnButton1Pressed();
  }
}

You have to write e.KeyData == Keys.N instead of e.N in my last post :)  I didn't check it
and vinhnl you forgot the alt key :P
Yes pressing 'N' on the parent form also behaves as clicking the button.
Also the problem does not happen if the button text does not have "&". But if we dont have "&" then alt + N wont show and underline under the N when I press alt(ya ya i can make the text underlined by the function) but I dont want that.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
vinhnl ,
Can you give some KB article/ Link where this is said.
You can test this knowledge. You show a messagebox with YESNO question. If you press 'Y' or 'N', the dialog will close.
thats true, if you use mnemonic it will be clicked if u press the letter after the &, u don't need to hit the alt key
U can set of mnemonic and just make it with keyDown, if u want to have your N underlined  just add a OnPaint event to your button and use a funktion like e.graphics.drawLine() to paint the line under the text
Thanks for a quick and efficient job guys
de rien !
What does this(de rien) means and which language is it?(vietnamese?)