Is Any person know how to set a hotkey for command button?

G

Guest

Dear all,

I have no idea to set a hot key for the command button in my VB window
application program. Do any person know how to do it?
Thank you

miyakejess
 
J

Justin Rogers

If you don't need a context menu in your application you can use one to add
shortcut keys... You can even still have a context menu, but you have to do
some extra work if you don't want the button items showing.

using System;
using System.Windows.Forms;

public class ShortcutButton : Form {
private Button button = new Button();
private ContextMenu ctxMenu = new ContextMenu();
private MenuItem shortcutButton = new MenuItem();

private ShortcutButton() {
this.shortcutButton.Shortcut = Shortcut.CtrlB;
this.shortcutButton.Click += new EventHandler(Button_Click);
this.ctxMenu.MenuItems.Add(shortcutButton);
this.ContextMenu = ctxMenu;
this.button.Click += new EventHandler(Button_Click);

this.Controls.Add(button);
}

protected override void WndProc(ref Message m) {
switch(m.Msg) {
case 0x7b:
break;
default:
base.WndProc(ref m);
break;
}
}

private void Button_Click(object sender, EventArgs e) {
MessageBox.Show(this, "Foo");
}

private static void Main(string[] args) {
Application.Run(new ShortcutButton());
}
}
 
H

Herfried K. Wagner [MVP]

miyakejess said:
I have no idea to set a hot key for the command button in my VB window
application program. Do any person know how to do it?

If you want to add an Alt+* mnemonic, simply add a "&" in front of the
character that should be the mnemonic key in the 'Text' property, for
example, "&Save".

For shortcut keys, add a MainMenu and add items with shortcuts, then make
the items invisible and add handlers to their 'Click' events. These
handlers will be called if the according shortcuts are pressed.
 
G

Guest

Actually, I want to make a shortcut key in Context menu
I have tried something similar to Justin Rogers' suggestion,
but it seems that, the computer dosn't know i want the short cut i press is
refer to my application.
Instead, it runs the short cut key defined in other applications.
so, how can i code to tell the computer to do the action on my application?
 
G

Guest

You might be able to capture the key events globally using DirectInput. I
dont recall exaclty how to do this, (did it in VB6 awhile back) but I know it
wasnt very straight forward. I used it for mouse and joystick events.

Hope that helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top