Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You may still use KeyPreview property but check which control is focused, if it is a textbox then do nothing, else if it is another control - say RichTextBox - then handle the pressed keys. To get the currently focused control, you may need to access Win32 API. Example: Created a new Windows forms application, add a text box and a richtext box in the form, set the KeyPreview property of the form to true, add an event handler for the KeyDown event of the form, the textbox, and the richtextbox. Also the following using statement:</p> <pre><code>using System.Runtime.InteropServices;//for DllImport </code></pre> <p>then replace the code of the form by the following code:</p> <pre><code>public partial class Form1 : Form { // Import GetFocus() from user32.dll [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)] internal static extern IntPtr GetFocus(); protected Control GetFocusControl() { Control focusControl = null; IntPtr focusHandle = GetFocus(); if (focusHandle != IntPtr.Zero) // returns null if handle is not to a .NET control focusControl = Control.FromHandle(focusHandle); return focusControl; } public Form1() { InitializeComponent(); } private void Form1_KeyDown(object sender, KeyEventArgs e) { Control focusedControl = GetFocusControl(); if (focusedControl != null &amp;&amp; !(focusedControl is TextBox) &amp;&amp; e.Control &amp;&amp; e.KeyCode == Keys.C)//not a textbox and Copy { MessageBox.Show("@Form"); e.Handled = true; } } private void richTextBox1_KeyDown(object sender, KeyEventArgs e) { if(e.Control &amp;&amp; e.KeyCode == Keys.C) MessageBox.Show("@Control"); } private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.Control &amp;&amp; e.KeyCode == Keys.C) MessageBox.Show("@Control"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload