问题描述
我有一个包含标签和datargridviews每个选项卡上简单的.NET应用程序。我已经通过使用标准的特性加到主菜单的形式和分配热键菜单项:
editMenuItem =新ToolStripMenuItem(复制,空,新System.EventHandler(onCopyCut_Click));
editMenuItem.ShortcutKeys = Keys.Control | Keys.C;
以上只是复制单元格的内容复制到剪贴板中显示的菜单项。这工作正常,但在 DGV的编辑模式按Ctrl + C和其他标准的热键不工作了!
我已经设置了 Form.Key preVIEW
属性为true,还试图关闭处理的我的形式的财产对象,但没有任何反应:
无效FileOrginizerForm_KeyDown(对象发件人,KeyEventArgs E)
{
...
如果(gridView.CurrentCell.IsInEditMode)
e.Handled = FALSE;
}
什么我失踪?我敢肯定这应该是简单的东西。
我发现了 MSDN帮助页面的评论:
解决方法:
而不是打开和关闭的菜单快捷我结束了从主窗体的KeyDown事件处理程序在调用菜单事件处理程序:
无效FileOrginizerForm_KeyDown(对象发件人,KeyEventArgs E)
{
如果(!gridView.CurrentCell.IsInEditMode)
{
如果(e.KeyData ==(Keys.Control | Keys.Z))
{
this.editToolStripMenuItem.DropDownItems [撤消] PerformClick()。
}
否则,如果(e.KeyData ==(Keys.Control | Keys.Y))
{
this.editToolStripMenuItem.DropDownItems [重做] PerformClick()。
}
否则,如果(e.KeyData ==(Keys.Control | Keys.X))
{
this.editToolStripMenuItem.DropDownItems [剪切] PerformClick()。
}
否则,如果(e.KeyData ==(Keys.Control | Keys.C))
{
this.editToolStripMenuItem.DropDownItems [复印] PerformClick()。
}
否则,如果(e.KeyData ==(Keys.Control | Keys.V))
{
this.editToolStripMenuItem.DropDownItems [粘贴] PerformClick()。
}
否则,如果(e.KeyData ==(Keys.Control | Keys.A))
{
this.selectToolStripMenuItem.DropDownItems [全选] PerformClick()。
}
}
}
如果您添加 SendKeys.Send(^ c的);
菜单的单击事件项目则无需从菜单项删除的快捷方式
I have simple .net application containing tabs and datargridviews on each tab. I've added main menu to the form and assigned hotkeys to menu items by using standard property:
editMenuItem = new ToolStripMenuItem("Copy", null, new System.EventHandler(onCopyCut_Click));
editMenuItem.ShortcutKeys = Keys.Control | Keys.C;
The menu item shown above just copies cell content to clipboard. This works fine but in DGV's editing mode Ctrl+C and other standard hotkeys don't work anymore!
I've set the Form.KeyPreview
property to true, also tried to turn off the Handled property of my Form object but nothing happens:
void FileOrginizerForm_KeyDown(object sender, KeyEventArgs e)
{
...
if (gridView.CurrentCell.IsInEditMode)
e.Handled = false;
}
What I'm missing? I'm sure this should be something simple.
I found some information on msdn help page's comments :
SOLUTION:
Instead of turning on and off the menu shortcuts I ended up by calling menu event handlers from the main Form's KeyDown event handler:
void FileOrginizerForm_KeyDown(object sender, KeyEventArgs e)
{
if (!gridView.CurrentCell.IsInEditMode)
{
if (e.KeyData == (Keys.Control | Keys.Z))
{
this.editToolStripMenuItem.DropDownItems["Undo"].PerformClick();
}
else if (e.KeyData == (Keys.Control | Keys.Y))
{
this.editToolStripMenuItem.DropDownItems["Redo"].PerformClick();
}
else if (e.KeyData == (Keys.Control | Keys.X))
{
this.editToolStripMenuItem.DropDownItems["Cut"].PerformClick();
}
else if (e.KeyData == (Keys.Control | Keys.C))
{
this.editToolStripMenuItem.DropDownItems["Copy"].PerformClick();
}
else if (e.KeyData == (Keys.Control | Keys.V))
{
this.editToolStripMenuItem.DropDownItems["Paste"].PerformClick();
}
else if (e.KeyData == (Keys.Control | Keys.A))
{
this.selectToolStripMenuItem.DropDownItems["Select All"].PerformClick();
}
}
}
If you add SendKeys.Send("^c");
to the click event of the menu item you don't need to remove the shortcut from the menuitem
这篇关于如何使工作标准热键(按Ctrl + C,Ctrl + Z键)在DataGridView的编辑模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!