Welcome to Dream.In.Code
Become a VB Expert!

Join 149,547 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,568 people online right now. Registration is fast and FREE... Join Now!




Using a keypress to trigger a buttonclick event

 
Reply to this topicStart new topic

Using a keypress to trigger a buttonclick event

TuxBrewer
18 Nov, 2007 - 02:42 PM
Post #1

New D.I.C Head
*

Joined: 6 Oct, 2007
Posts: 9


My Contributions
Hi all,

I am trying to write a small calculator program for an assigment. I have a section of code that executes when the users clicks a button. I want the same section of code to execute when the user also hits a certain key, ie + - / * =. How can I trigger the code in my buttonclick event handler based on a keypress?

Searched all over and could not find what I am looking for. Can someone point me in the right direction?

Thanks

TB
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Using A Keypress To Trigger A Buttonclick Event
18 Nov, 2007 - 04:39 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,303



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You can call the button click event from inside the keypress event. Just pass it nothing for parameters. By calling the event, this will cause it to execute. Since a button didn't actually call the event there isn't any parameters to pass to the click event, so you just pass in a null value.

For example, if your button was named Button1, the call to the click event would look like this. Just replace "Button1_Click" with the name of your buttons click event.
CODE

Button1_Click(Nothing, Nothing)

User is offlineProfile CardPM
+Quote Post

Ken Halter
RE: Using A Keypress To Trigger A Buttonclick Event
18 Nov, 2007 - 10:18 PM
Post #3

New D.I.C Head
*

Joined: 18 Nov, 2007
Posts: 35



Thanked: 6 times
My Contributions
QUOTE(jayman9 @ 18 Nov, 2007 - 05:39 PM) *

You can call the button click event from inside the keypress event. Just pass it nothing for parameters. By calling the event, this will cause it to execute. Since a button didn't actually call the event there isn't any parameters to pass to the click event, so you just pass in a null value.

For example, if your button was named Button1, the call to the click event would look like this. Just replace "Button1_Click" with the name of your buttons click event.
CODE

Button1_Click(Nothing, Nothing)



Isn't this a VB Classic forum? The labels and menu choices are pretty clear.... but, if it's another dotNet forum, please let me know before I spend a lot of time answering questions here....

If it were VB Classic (ie pre-dotNet versions of VB), the best way to click a button using code would be to use syntax similar to....

Command1.Value = True 'Fires the button's click event.

Calling an event handler is, in my opinion, sloppy. If you need to run the same code from the button and elsewhere in your module, create a new procedure and call it from the button's click event.

Besides, directly calling any event handler makes it impossible (or nearly) to create libraries of reusable code.

Ken Halter
MS-MVP-VB
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 09:34PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month