Triggering Visual Basic Keydown events without a specific function - vba

I'm building on top of code that a previous developer has left me, and he left something that intrigued me quite a bit.
Basically on his menus, he has a TextBox to take in user input and a button next to it to submit the value of the TextBox (for example if the user wanted to select option 1, he would input 1 into the TextBox and click the button). However, the user could also press the Enter key while focusing the TextBox, and it would be treated as the submit button was clicked.
Now this is simple enough to do, but when I check the VB code behind the menu, there's no TextBox_Keydown(...) Handles TextBox.Keydown function anywhere, only the button click event. How is he doing this? He has several menus that are similar and I can't figure out how.

A standard dialog box, if not told to act otherwise, enter does default command button and escape does cancel. In VB look at the properties Default for the command button.

I discovered how he was doing it. He basically mapped the AcceptButton and CancelButton properties of the entire Windows Form to various button functions.

Related

Show on-screen keyboard when textbox clicked

I've built a on-screen keyboard for a surveillance system made in VBA. I need it so that when the user clicks on the textbox to enter the data, the on-screen keyboard I've made, shows up. Thanks!
Check out the _Enter() event that fires when a user clicks inside the textbox control.
Private Sub TextBox1_Enter()
frmKeyboard.Show
End Sub
For a more complete solution, you may want to wrap the keyboard showing/hiding events in a VBA class so you can more easily apply it to the different text boxes on your form. That way you don't have to repeat all of your code for each text box on your form. Just something to explore for down the road. :-)

Run macro by clicking away from any ActiveX textbox in a document

I'm looking for a VBA script that will run whenever I click away from any of ActiveX textboxes in the document. Another alternative would be to have it run whenever I click on any textbox (without clicking away).
How can it be done without assigning subs to each textbox individually?
Double click on your textbox and it will bring up the default method for that textbox, which in my testing is Textbox1_Change(). This method will run every time you type anything into the textbox.
You see two dropdown boxes at the top of the vba editor. Drop down on the one on the right and you'll see all the other available methods for the textbox, one of which is LostFocus which I reckon will suit your purposes. Clicking on that creates a sub that will execute every time the textbox loses focus. See how you go with that. Cheers

How to determine which form controls the keyboard input on VB.net

I'm developing a VB.net windows application and I have some issues with the keyboard input.
My application has different forms and I'm showing and hidding them with the user interaction. One of the inputs comes from the keyboard, and here is where I have a problem.
When I hide a form and show the next one, most of the times the new-shown form does not receive the keyboard input until I click somewhere on it.
I assume that the problem is that the new form I'm showing is not the "selected application" for windows until the user interacts with it by clicking on it, but I don't know how to set this "property" by code.
I tried with focus and select on the whole form (Me.select/focus) and in some form's control (me.lbl_xxx.select/focus), but I did not get any result.
Can anyone explain me how to control which application/form gets the keyboard input on windows?
Thanks
David
You can't really interact with a label so the input focus won't be set properly.
Focussing a specific textbox on your form on the other hand should just work fine.

Default Button on Form (VB 2008)

I want to find a way to make a specific button, the form's default button,
I.e. the button that is highlighted when the form opens for the first time.
I tried the AcceptButton property but when I run the program, that does not work.
Any idea?
Thank you in advance,
Tassos
You need to change the AcceptButton property of the containing form.
form1.AcceptButton = button1
Here form1 is the Form whose default button you need to set, and button1 is the name of the Button on that form.
The form's AcceptButton and CancelButton properties define the default behaviour for the Enter and Escape keys, rather than the highlighting.
To highlight the button use the Focus method, but when doing this in the form_load event you will need to call the Select method instead.
btnDefault.Select()
As mentioned in the comments, setting the control to the lowest taborder will achieve the same thing
The answer from 'chk' on 5/2/13 is the correct answer, but is shown as a string which of course is not the way to do it.
Also, in the form's property sheet you can find, under Misc, the property 'AcceptButton'. This will give you a list of buttons on the form - just select the one you want.
The button selected as the AcceptButton will behave as the 'default' button. It will be 'highlighted' with a darker border and will be clicked when you push the Enter key on your keyboard.
Setting up an AcceptButton is different than setting the button's focus. The AcceptButton's click event will be triggered by the Enter key no matter which control has the focus on the form.
You can also do this programmatically.
I have a maintenance form where initially I want the "search" button as the form accept button.
when I'm displaying the field maintenance area, I want the "ok" button to be the accept button.
You simply change this in the appropriate areas in your code to Me.AcceptButton = MyButtonName.

Visual Basic 2010 Event Listeners on a List

I have a list of buttons in VB2010.
What is the best way to assign a function to their click event.
So every button has the same function, e.g.:
On Button Click
FireFunction(1)
End On Button Click
Without having to add a click event for every button.
The goal is to produce something similar to what is done with the Control Array idea in Visual Basic 2006.
Define a click function as in:
http://visualbasic.about.com/od/learnvbnet/a/eventhandler.htm
and react based on Sender. There may be a cleaner way to setup delegates in VB.NET, but I use it not.