Catching Ctrl + C in a textbox

后端 未结 8 1734
孤城傲影
孤城傲影 2020-12-15 20:42

Despite me working with C# (Windows Forms) for years, I\'m having a brain fail moment, and can\'t for the life of me figure out how to catch a user typing Ctrl +

8条回答
  •  借酒劲吻你
    2020-12-15 21:18

    I had a problem catching Ctrl + C on a TextBox by KeyDown. I only got Control key when both Control and C were pressed. The solution was using PreviewKeyDown:

    private void OnLoad()
    {
        textBox.PreviewKeyDown += OnPreviewKeyDown;
        textBox.KeyDown += OnKeyDown;
    }
    
    private void OnPreviewKeyDown( object sender, PreviewKeyDownEventArgs e)
    {
        if (e.Control)
        {
            e.IsInputKey = true;
        }
    }
    
    private void OnKeyDown( object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.C) {
            textBox.Copy();
        }
    }
    

提交回复
热议问题