How to configure standard hot keys (Ctrl + C, Ctrl + Z) in the DataGridView edit mode?

I have a simple .net application containing tabs and datargridviews on each tab. I added the main menu to the form and assigned shortcuts for menu items using the standard property:

editMenuItem = new ToolStripMenuItem("Copy", null, new System.EventHandler(onCopyCut_Click));
editMenuItem.ShortcutKeys = Keys.Control | Keys.C;

The menu item shown above simply copies the contents of the cell to the clipboard. This works great, but in DGV editing mode, Ctrl + C and other standard hotkeys no longer work!

I set the property Form.KeyPreviewto true, also tried to disable the Processed property of my Form object, but nothing happens:

    void FileOrginizerForm_KeyDown(object sender, KeyEventArgs e)
    {
            ...
        if (gridView.CurrentCell.IsInEditMode)
            e.Handled = false;
    }

What am I missing? I am sure that this should be something simple.

msdn :

, , ,    , ShortcutKeys    , , .    (ctrl + v) ShortcutKey,   . Microsoft, .    ShortCutKey,   ( , ), reset   .

:

, Form KeyDown:

    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();
            }
        }
    }
+3
3

, , .

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;

public class Form1 : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    public Form1()
    {
        var dgv = new DataGridView
        {
            Dock = DockStyle.Fill,
            DataSource = new List<DummyObject>
            {
                new DummyObject { Name = "One", Value = 1 },
                new DummyObject { Name = "Two", Value = 2 },
                new DummyObject { Name = "Three", Value = 3 },
            }
        };
        dgv.EditingControlShowing += (s, e) => e.Control.VisibleChanged += DgvEditingControlVisibleChanged;
        Controls.Add(dgv);
    }

    void DgvEditingControlVisibleChanged(object sender, EventArgs e)
    {
        Control control = sender as Control;
        if (control.Visible)
        {
            // The editing control has become visible.

            Trace.WriteLine(String.Format("Editing control showing {0}", control));
        }
        else
        {
            // The editing control has been removed.

            // Remove the event handler because the DGV can use multiple
            //  editing controls if it has different column types. 
            control.VisibleChanged -= DgvEditingControlVisibleChanged;
            Trace.WriteLine(String.Format("Editing control removed {0}", control));
        }
    }
}

public class DummyObject
{
    public string Name { get; set; }
    public int Value { get; set; }
}
+1

SendKeys.Send("^c"); ,

+1

Just remove the shortcut from the menu item and place the text for it in the text of the menu item. This works for using a shortcut in forms, but you still have to code if the menu item itself is clicked

0
source

All Articles