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 :)
Re: Shortcut key problems
I already did that.
Thanks anyway.
Re: Shortcut key problems
Re: Shortcut key problems
I want it to work in the contextual menu also.
I need to convert the ascii code to Key.
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
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
Re: Shortcut key problems
If you want to do it that way, why haven't you just followed the documentation ?
Re: Shortcut key problems
Dude, thanks a lot. I would have never guessed it.
You should really read again what I've asked.
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.
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.
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.
Re: Shortcut key problems
Nope, but it does have many other wonderful characters.
After 9 there's : ; < = > ? etc.
http://www.asciitable.com/
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.
Re: Shortcut key problems
Dude, I've got a List with the keys allowed, but using IF or case is kind of hellish.