如何使工作标准热键(按Ctrl + C,Ctrl + Z键)在DataGridView的编辑模式?热键、编辑、模式、标准

2023-09-03 22:37:58 作者:痞子夺位

我有一个包含标签和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帮助页面的评论:

       

设置这些属性时,你需要记住的一件事是,如果     你有一个TextBox控件在窗体中,菜单项的ShortcutKeys会     截取这个组合键和文本框将永远不会收到它如如果你     有粘贴(Ctrl + V)快速键,您的文本框将永远不会收到贴     命令。据微软称,这是由设计。他们的解决办法是,     暂时清除菜单项的快速键属性,以使糊     命令(活动期间最有可能),然后重新设置后,该事件是     完蛋了。

  

解决方法:

而不是打开和关闭的菜单快捷我结束了从主窗体的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()。
            }
        }
    }
 
职场人提高PPT制作效率的9个小技巧

解决方案

如果您添加 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 :

One thing that you need to keep in mind when setting these properties is that if you have a textbox control in your form, the menu item's ShortcutKeys will intercept that key combination and the textbox will never receive it e.g. if you have a paste (ctrl + v) ShortcutKey, your textbox will never receive the paste command. According to Microsoft, this is by design. Their workaround is to temporarily clear the menu item's ShortCutKey property to permit the paste command (most likely during an event) and then reset it after the event is finished.

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