Author Topic: VB.net Help... binding keys to press on-screen buttons.  (Read 4287 times)

Jam0864

  • Contributor
  • Fanatic
  • **
  • Posts: 744
    • MSN Messenger - marmalade0864@hotmail.com
    • View Profile
    • Jam0864's Content Dump
    • Email
I'm making a calculator application for a school project, and I can't find anywhere how I can bind a key to press a button like what is done in the windows calculator? (I want to use the numpad to enter numbers into the calculator.)

Does anyone know how this can be done?


It's not a requirement, but would be a nice feature to get a few extra marks.



If you have an objection to helping me on a school project that I'm supposed to do myself, could you hint me in the right direction? :) (Teacher doesn't know how)

EDIT:// I'm limited to Visual basic .NET 2005. 2008 specific code cannot be used unfortunately.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: VB.net Help... binding keys to press on-screen buttons.
« Reply #1 on: 2009-07-28, 05:06:21 PM »
I don't think there's an automatic way to do it... with the possible exception of setting up menu shortcuts, but even if that is possible, I wouldn't recommend it.  I think you want the following functions:
1) NumberButton_Click - event handler for buttons -- you can route all buttons to this same event handler and use the "sender" parameter to determine which button was pressed, maybe even use the Tag or Text property of the button to store/retrieve the character associated with that button.
2) Form_KeyPressed - event handler for keypress events on the form.  If the KeyPreview property of the form is set to true, all key press events will go to the form as well and not just the control with focus.  This way you can catch all key presses.
3) HandleEntry - The above to functions call this function passing a character or other parameter indicating which button/key was pressed.  All the work is done here.  For example, NumberButton_Click might call HandleEntry("1") when the 1 button is pressed.  Form_KeyPressed might also call HandleEntry("1") when the 1 key is pressed.  Then you can handle the "1" button/key as a single function in a single place.