Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle Copy for multiple controls when adding a ShortcutKey to menu item?
    text
    copied!<p>If I do <strong>not</strong> create an "Edit->Copy" menu item and assign it the shortcut keys "CTRL+C", then I can select a control (RichTextBox, DataGridView, etc..) and hit "CTRL+C" and the control itself will handle the copy. I can copy text out, and paste it into notepad, etc..</p> <p>Now throughout my whole form, I have a lot of controls. But I have a custom control that I want to make clear that I handle Copy functionality for. So I added the ShortcutKey CTRL+C to Edit->Copy, and by default it is set to Enabled.</p> <p>Now, I have to implement an event handler for the 'click' event on that menu item. If I explicitly put in code to handle the copy, then it works:</p> <pre><code>public void menuEditCopy_Click(object sender, EventArgs e) { myCustomControl.Copy(); } </code></pre> <p>However, now Copy does not work on any other type of control. My first inclination was to find out the type of control that has focus, and implement a limited set of copy code for each of them: </p> <pre><code>public void menuEditCopy_Click(object sender, EventArgs e) { if (this.ActiveControl is MyCustomControl) { ((MyCustomControl)this.ActiveControl).Copy(); } else if (this.ActiveControl is RichTextBox) { ((RichTextBox)this.ActiveControl).Copy(); } } </code></pre> <p>etc...</p> <p>However, my controls are added to a SplitContainer, and debugging shows that this.ActiveControl is set to the splitcontainer instance, not the control, even though I know that control is selected.</p> <p>So my last thought is to literally check if every control has focus:</p> <pre><code>public void menuEditCopy_Click(object sender, EventArgs e) { if (myCustomControl.Focused) { myCustomControl.Copy(); } else if (richTextBox1.Focused) { richTextBox1.Copy(); } } </code></pre> <p>I would like to avoid this if possible, it is a lot of controls, and if I add a new control, I would need to update it. Is there a better way of doing this?</p> <p>Thanks</p>
 

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