Shortcuts don't work.

  • Thread starter Thread starter PAzevedo
  • Start date Start date
P

PAzevedo

With ToolStripMenuItem objects i created a 'Tools' menu and from that
menu droped an Item to which i assigned a ShortcutKey of 'Keys.Control
| Keys.E', and the shortcut doesn't work.
This ToolStripMenuItem is inside a Form, i tryed with different
shortcuts and some work, some don't.
For instance, 'Keys.Control | Keys.R' doesn't work either.
Is anyone familiar with this problem?

MenuStrip msMenu = new MenuStrip();
msMenu.Parent = this;

ToolStripMenuItem miFile = new ToolStripMenuItem("&File");
ToolStripMenuItem miTools = new ToolStripMenuItem("&Tools");

ToolStripMenuItem miExit = new ToolStripMenuItem("E&xit", null, new
EventHandler(miExitOnClick));
ToolStripMenuItem miInsert = new ToolStripMenuItem("&Insert", null, new
EventHandler(miInsertOnClick));
miInsert.ShortcutKeys = Keys.Control | Keys.I;
ToolStripMenuItem miEdit = new ToolStripMenuItem("&Edit", null, new
EventHandler(miEditOnClick));
miEdit.ShortcutKeys = Keys.Control | Keys.E;
ToolStripMenuItem miDelete = new ToolStripMenuItem("&Delete", null, new
EventHandler(miDeleteOnClick));
miDelete.ShortcutKeys = Keys.Control | Keys.D;

miFile.DropDownItems.Add(miExit);
msMenu.Items.Add(miFile);
miTools.DropDownItems.Add(miInsert);
miTools.DropDownItems.Add(miEdit);
miTools.DropDownItems.Add(miDelete);
msMenu.Items.Add(miTools);
 
Hi,

I can't repro the issue. I pasted the code you supplied into a new Form right
after InitializeComponent() is called, and then created event handlers that
simply displayed a MessageBox.

Pressing any of the key combinations defined as shortcuts displayed a
MessageBox as expected. Ctrl+I, Ctrl+E, Ctrl+D and even Ctrl+R all worked
just fine. I didn't try any other combinations.

I suspect that there may be some shortcut conflict somewhere or another
control is preventing the key combination from working. Make sure that you
haven't coded any key handlers that might prevent them from working and try
removing components and controls from your Form until the issue goes away,
just to see which control is causing the conflict (if in fact it is a conflict
issue).
 
I had a TextBox object also on the same form, i removed it and the
problem went away, thank you for the sugestion.
I have been told that focus can sometimes trigger these problems, and
the TB did indeed have the focus, so some TB shortcut might be
conflicting with my own.
 
I disabled the shortcuts on the TextBox and the shortcuts previously
mentioned still wouldn't work. They work if i change Enabled to false.
 
The full code btw.

using System;
using System.Windows.Forms;

class GUDB : Form
{
private TextBox tbOutput = new TextBox();

public static void Main()
{
Application.Run(new GUDB());
}
public GUDB()
{
Text = "GUDB";

tbOutput.Parent = this;
tbOutput.Multiline = true;
tbOutput.ReadOnly = true;
tbOutput.Dock = DockStyle.Fill;
tbOutput.ScrollBars = ScrollBars.Vertical;
tbOutput.WordWrap = true;

MenuStrip msMenu = new MenuStrip();
msMenu.Parent = this;

ToolStripMenuItem miFile = new ToolStripMenuItem("&File");
ToolStripMenuItem miTools = new ToolStripMenuItem("&Tools");

ToolStripMenuItem miExit = new ToolStripMenuItem("E&xit", null, new
EventHandler(miExitOnClick));
ToolStripMenuItem miInsert = new ToolStripMenuItem("&Insert", null,
new EventHandler(miInsertOnClick));
miInsert.ShortcutKeys = Keys.Control | Keys.I;
ToolStripMenuItem miEdit = new ToolStripMenuItem("&Edit", null, new
EventHandler(miEditOnClick));
miEdit.ShortcutKeys = Keys.Control | Keys.E;
ToolStripMenuItem miDelete = new ToolStripMenuItem("&Delete", null,
new EventHandler(miDeleteOnClick));
miDelete.ShortcutKeys = Keys.Control | Keys.D;

miFile.DropDownItems.Add(miExit);
msMenu.Items.Add(miFile);
miTools.DropDownItems.Add(miInsert);
miTools.DropDownItems.Add(miEdit);
miTools.DropDownItems.Add(miDelete);
msMenu.Items.Add(miTools);
}

private void miExitOnClick(object sender, EventArgs ea)
{
Close();
}

private void miInsertOnClick(object sender, EventArgs ea)
{
Close();
}

private void miEditOnClick(object sender, EventArgs ea)
{
Close();
}

private void miDeleteOnClick(object sender, EventArgs ea)
{
Close();
}
}
 
Hi,

I wonder if that's a bug. Setting ShorcutsEnabled to false should bubble the
key press to the Parent, IMO. I was able to repro that it doesn't.

Setting Enabled to false makes sense, however, because the TextBox isn't
receiving the key input to begin with.

I guess you'll have to do some research on the web to see if there are any
existing solutions. I assume that some people override WndProc and try to
handle the specific shortcuts that aren't working by checking if the
ActiveControl is a TextBox and then switching over the shortcuts that are
required such as Ctrl+E and Ctrl+R. Then invoke the appropriate menu command
in code for the current shortcut.

HTH
 

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

Similar Threads


Back
Top