0

Question 1: I have tried to add ToolStripMenuItem in ToolStripComboBox and added the shortcut keys for ToolStripMenuItem. It is not working. Can you please put me in right way.

ToolStripMenuItem item1 = new ToolStripMenuItem (){Text = "A", ShortcutKeys=Keys.Control | Keys.D0};
ToolStripMenuItem item2 = new ToolStripMenuItem (){Text = "B", ShortcutKeys=Keys.Control | Keys.D1};
toolStripComboBox1.Items.Add(item1);
toolStripComboBox1.Items.Add(item2);

Question 2: If i implement my shortcut using KeyDown event, the event is triggering twice while pressing right alt key. In first trigger, KeyEventArgs providing data of control key. So, unable to get whether pressed key is Alt or Control key. I have used below example code,

Code:

this.KeyPreview = true;
this.KeyDown += Form1_KeyDown;

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
     if(e.Control)
     {
        if(e.KeyCode == Keys.D0)
             toolStripComboBox1.SelectedIndex = 0;
        else if(e.KeyCode == Keys.D1)
             toolStripComboBox1.SelectedIndex = 1;
     }
}

Thanks in advance.

MOHANRAJ G
  • 75
  • 9

2 Answers2

0

ToolStripComboBox doesn't support shortcuts out of the box because its items don't have a click event. Actually, the Items property is an ObjectCollection so it doesn't really expect a collection of ToolStripMenuItem as you seem to be thinking. When you add a ToolStripMenuItem, it's treated like any other object.

If the purpose of the shortcuts is to execute some action when an item is clicked/selected, consider using a ToolStripDropDownButton instead.

Example:

var dropDownButton = new ToolStripDropDownButton() { Text = "Open me" };
toolStrip1.Items.Add(dropDownButton);

ToolStripMenuItem item1 = new ToolStripMenuItem() { Text = "A", ShortcutKeys = Keys.Control | Keys.D0 };
ToolStripMenuItem item2 = new ToolStripMenuItem() { Text = "B", ShortcutKeys = Keys.Control | Keys.D1 };
dropDownButton.DropDownItems.Add(item1);
dropDownButton.DropDownItems.Add(item2);

item1.Click += (obj, args) => MessageBox.Show("Item1 was clicked!");
item2.Click += (obj, args) => MessageBox.Show("Item2 was clicked!");
0

As explained in the other answer, ToolStripComboBox items don't support keyboard shortcuts out of the box. If you have to use ToolStripComboBox for some reason (e.g., you don't want to execute some action on click and just want to select the item), you'll have to implement the logic yourself.

Here's an example that uses a dictionary to store the shortcut keys for each index of the ComboBox. You may adjust it to store values or text instead of indexes if you like.

private Dictionary<Keys, int> comboBoxShortcutKeys = new Dictionary<Keys, int>();

public Form1()
{
    InitializeComponent();

    this.KeyPreview = true;
    this.KeyDown += (obj, args) =>
    {
        if (comboBoxShortcutKeys.ContainsKey(args.KeyData))
        {
            toolStripComboBox1.SelectedIndex = comboBoxShortcutKeys[args.KeyData];
        }
    };
}

private void AddComboBoxItems()
{
    toolStripComboBox1.Items.Add("A");
    comboBoxShortcutKeys.Add(Keys.Control | Keys.D0, 0);

    toolStripComboBox1.Items.Add("B");
    comboBoxShortcutKeys.Add(Keys.Control | Keys.D1, 1);
}

Note that in order for the shortcut to work from anywhere on the form, the KeyPreview property needs to be set to true as shown above. Alternatively, you may override the ProcessCmdKey() method as explained here.

  • Thanks for your suggestion. I have tried my implementation using **KeyDown** event. This event triggered twice while pressing right alt key. In first trigger, it is showing like control key pressed. How to differentiate that? – MOHANRAJ G Aug 11 '20 at 14:25
  • I'm not sure I follow. Anyway, make sure that you're using `KeyData` and not `KeyCode`. Also, make sure to check that the pressed `Keys` actually exist in the dictionary using `ContainsKey` as shown above. – 41686d6564 stands w. Palestine Aug 11 '20 at 14:29
  • Yes i ensured that. If i press the right alt + 0, i get the data **(Keys.Control | Keys.D0)** in key down event at first trigger. I do not have an idea how it is showing like this. Please let me know do you have any idea about this. – MOHANRAJ G Aug 11 '20 at 15:14
  • I can't tell you what went wrong without seeing your code. You may edit the question and include your updated code or you can create a paste on something like [Pastebin](https://pastebin.com/) and leave the link in a comment here so that I can have a look at it. – 41686d6564 stands w. Palestine Aug 11 '20 at 21:48
  • I have added my implemented code in Question2. Can please look into that and share your ideas. – MOHANRAJ G Aug 12 '20 at 04:03
  • @MOHANRAJG First of all, I specifically asked you to make sure that you're using `KeyData` and you said that's what you used but the code in your question says otherwise. Anyway, even with `e.Control` and `e.KeyCode`, your code [works as expected](https://i.imgur.com/t5CBzta.gif) (as you can see, pressing `Ctrl+Num` changes the Selected index, while pressing `Alt` or `Alt+Num` has no effect). Please make sure the code you include in the question is a [repro]. – 41686d6564 stands w. Palestine Aug 12 '20 at 04:24