&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?
manjeetbothraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

manjeetbothraAuthor Commented:
The application is in C#
0
dkloeckCommented:
use the key down event like this:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vinhnlCommented:
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.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

vinhnlCommented:
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs
e)
  if (e.KeyData == Keys.A)
  {
      OnButtonNextPressed();
  }
}
0
vinhnlCommented:
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs
e)
  if (e.KeyData == Keys.N)
  {
      OnButtonNextPressed();
  }
}
0
manjeetbothraAuthor Commented:
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
0
strickddCommented:
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.
0
dkloeckCommented:
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
0
dkloeckCommented:
and vinhnl you forgot the alt key :P
0
manjeetbothraAuthor Commented:
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.
0
vinhnlCommented:
Ah Manjeetbothra,

I think that it's standard of bottom control of windows. If you focus on some buttom (&OK, &Cancel, &Next, &Pre,..) and click a lettre (not need alt), windows understand defaut that you chois this buttom
0
manjeetbothraAuthor Commented:
vinhnl ,
Can you give some KB article/ Link where this is said.
0
vinhnlCommented:
You can test this knowledge. You show a messagebox with YESNO question. If you press 'Y' or 'N', the dialog will close.
0
dkloeckCommented:
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
0
dkloeckCommented:
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
0
manjeetbothraAuthor Commented:
Thanks for a quick and efficient job guys
0
vinhnlCommented:
de rien !
0
manjeetbothraAuthor Commented:
What does this(de rien) means and which language is it?(vietnamese?)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.