Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

key label

Status
Not open for further replies.

safo2000

Programmer
Jul 9, 2002
46
LB
Hi

i made a form with many controls, tab control, buttons,.....i am trying to invoke procedures using the F1,F2,F3.....

before the VB.NET era i used to program in foxpro, we had the command
ON KEY LABEL F1 DO proc_name

is there something to this effect in VB.NET??????????

thanks for the tips
 
Do you want to capture the key presses just whilst the user has your form in focus or do you want to capture and override them at any time (e.g. when your application is minimised/in the system tray and the user clicks F1 whilst looking at the desktop for example)?

If it is just for your form then you could you use one of the events such as KeyDown. e.g.
Code:
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Select Case e.KeyCode.ToString
            Case "F1", "F2", "F3"
                ' Do Something...
        End Select
    End Sub

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
If there are other controls on the form the key{up|down|press} won't be captured. That's because the form has not the focus. Even if write: "Me.Focus()" after every last command that is executed (such as button press ...), the form still hasn't the focus.

I think you should make a menu to use F1 etc.
 
i tried ca8msm's suggestion before i posted my thread (key_down on the form) and got what bclt was saying (the form does not have focus all the time because there r a lot of controls.) is there a option other than the menu???

thanks agian for the help guys
 
Make a menu so as to put all the shortcuts you want there. Then click on the main node of the menu and set its visibility to false.

Example:

menu MyShortCuts
-aa1 F1
-aa2 Ctrl+F1
etc ...

The "MyShortCuts" will be invisible, but when pressing F1 the commands under "aa1_Click(ByVal ...)" will be executed.
 
try ca8msm's solution and set the keypreview of the form to true. This is what MSDN says about keypreview.

When this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp events. After the form's event handlers have completed processing the keystroke, the keystroke is then assigned to the control with focus. For example, if the KeyPreview property is set to true and the currently selected control is a TextBox, after the keystroke is handled by the event-handling methods of the form the TextBox control will receive the key that was pressed. To handle keyboard events only at the form level and not allow controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form's KeyPress event-handling method to true.

You can use this property to process all keystrokes in your application and either handle the keystroke or call the appropriate control to handle the keystroke. For example, when an application uses function keys, you might want to process the keystrokes at the form level rather than writing code for each control that might receive keystroke events.

Note If a form has no visible or enabled controls, it automatically receives all keyboard events.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Holy & " " & StrReverse("tihs") !
Didn't see the property "KeyPreview
 
thanks for the tips
i used context menu with short cuts and visibility=false
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top