-
September 26th, 2011, 03:03 AM
#1
Shortcut key problems
I've got a contextual menu in which elements are added dynamically.
I want to assign shortcut keys to those elements (1 - 9) but I don't know how.
Can anyone help me?
xem = 49; //(ASCII code for 1)
while (drCupoane.Read())
{
ToolStripMenuItem mnu = new ToolStripMenuItem();
mnu.Text = drC[1].ToString();
mnu.ToolTipText = "Cupon";
char c = (char)xem;
mnu.ShortcutKeyDisplayString = c.ToString();
contextMenuStripDatagridView.Items.Add(mnu);
xem = xem+1;
}
-
September 26th, 2011, 03:07 AM
#2
Re: Shortcut key problems
Check out the KeyDown event of your contextMenuStripDatagridView object.
I would try using this to capture when a key is pressed
It's not a bug, it's a feature!
-
September 26th, 2011, 03:08 AM
#3
Re: Shortcut key problems
I already did that.
Thanks anyway.
-
September 26th, 2011, 03:09 AM
#4
Re: Shortcut key problems
It's not a bug, it's a feature!
-
September 26th, 2011, 03:21 AM
#5
Re: Shortcut key problems
I want it to work in the contextual menu also.
I need to convert the ascii code to Key.
-
September 26th, 2011, 03:24 AM
#6
Re: Shortcut key problems
Then you'll need to assign an eventhandler to the KeyDown event of your contextmenu. In the handler, you will be able to tell which key was pressed using the KeyEventArgs parameter
It's not a bug, it's a feature!
-
September 26th, 2011, 03:28 AM
#7
Re: Shortcut key problems
xem = 49; // ascii code for 1
while (drC.Read())
{
ToolStripMenuItem mnu = new ToolStripMenuItem();
mnu.Text = drC[1].ToString();
mnu.ToolTipText = "Cupon";
// displays the Shortcut Key
char c = (char)xem;
mnu.ShowShortcutKeys = true;
mnu.ShortcutKeyDisplayString = c.ToString();
// the problem is that I want to assign the key to the menu item
mnu.ShortcutKey = xem (integer) -> convert to Keys. 1 - 9; (enum Keys)
contextMenuStripDatagridView.Items.Add(mnu);
xem = xem+1;
}
The itemclicked event of the contextual menu is already defined - I just need to dynamically assign shortcut key based on the integer (ASCII code) - 1 to 9 keys.
FROM INTEGER to Key
Last edited by arhicoc; September 26th, 2011 at 03:30 AM.
-
September 26th, 2011, 03:33 AM
#8
Re: Shortcut key problems
If you want to do it that way, why haven't you just followed the documentation ?
It's not a bug, it's a feature!
-
September 26th, 2011, 03:37 AM
#9
Re: Shortcut key problems
Dude, thanks a lot. I would have never guessed it.
You should really read again what I've asked.
-
September 26th, 2011, 03:45 AM
#10
Re: Shortcut key problems
Your first questions was "how do I assign a shortcut key to my contextmenu?". There a several ways of doing this. The one you have apparently already chosen is well documented.
If your problem lies in dynamically assigning shortcut keys, you could simply add a few lines of code that chooses between Keys.D1, Keys.D2 and so forth.
Code:
if(xem == 1)
mnu.ShortcutKeys = Keys.D1;
Really, there's nothing more to it.
It's not a bug, it's a feature!
-
September 26th, 2011, 03:47 AM
#11
Re: Shortcut key problems
I know how to do this. It's pretty straight forward.
The problem is that I want to do that dynamically and not just for a set of specific keys (there could be more than 9).
if(xem == 1)
mnu.ShortcutKeys = Keys.D1;
Instead of this, I want to be able to translate XEM to Keys.
It's that simple.
-
September 26th, 2011, 03:49 AM
#12
Re: Shortcut key problems
Well does your keyboard have a key that says '10' ? Mine doesn't
How would the user be expected to use the shortcut? As soon as he/she hits '1' the first menu item would be selected and the menu would (or at least, should) disappear.
It's not a bug, it's a feature!
-
September 26th, 2011, 03:50 AM
#13
Re: Shortcut key problems
Nope, but it does have many other wonderful characters.
After 9 there's : ; < = > ? etc.
http://www.asciitable.com/
-
September 26th, 2011, 03:54 AM
#14
Re: Shortcut key problems
In that case, I don't see any way of reliably converting any integer into one of the Keys enumeration. You will simply need to create your own "converter" method, something along the lines of:
Code:
public Keys GetKey(int num)
{
Keys k = Keys.D1;
switch(num)
{
case 1:
k = Keys.D1;
case 2:
k = Keys.D2;
}
return k;
}
You should also consider that most regional keyboard will have different configurations of the keys you mentioned, and you might run into problems if the user's keyboard does not match your own.
It's not a bug, it's a feature!
-
September 26th, 2011, 03:56 AM
#15
Re: Shortcut key problems
Dude, I've got a List with the keys allowed, but using IF or case is kind of hellish.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|