Shortcut key problems
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Shortcut key problems

  1. #1
    Join Date
    Dec 2008
    Location
    Romania
    Posts
    28

    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;
    }

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    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!

  3. #3
    Join Date
    Dec 2008
    Location
    Romania
    Posts
    28

    Re: Shortcut key problems

    I already did that.

    Thanks anyway.

  4. #4
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Shortcut key problems

    What's the problem then?
    It's not a bug, it's a feature!

  5. #5
    Join Date
    Dec 2008
    Location
    Romania
    Posts
    28

    Re: Shortcut key problems

    I want it to work in the contextual menu also.
    I need to convert the ascii code to Key.

  6. #6
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    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!

  7. #7
    Join Date
    Dec 2008
    Location
    Romania
    Posts
    28

    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 04:30 AM.

  8. #8
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    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!

  9. #9
    Join Date
    Dec 2008
    Location
    Romania
    Posts
    28

    Re: Shortcut key problems

    Dude, thanks a lot. I would have never guessed it.

    You should really read again what I've asked.

  10. #10
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    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!

  11. #11
    Join Date
    Dec 2008
    Location
    Romania
    Posts
    28

    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.

  12. #12
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    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!

  13. #13
    Join Date
    Dec 2008
    Location
    Romania
    Posts
    28

    Re: Shortcut key problems

    Nope, but it does have many other wonderful characters.
    After 9 there's : ; < = > ? etc.

    http://www.asciitable.com/

  14. #14
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    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!

  15. #15
    Join Date
    Dec 2008
    Location
    Romania
    Posts
    28

    Re: Shortcut key problems

    Dude, I've got a List with the keys allowed, but using IF or case is kind of hellish.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center