Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get the KeyDown event to fire in a custom container control?
    primarykey
    data
    text
    <p>I have a custom container control (deriving from <code>FlowLayoutPanel</code>) which contains zero or more child controls dragged there by the user. When a child control is clicked, it is "selected." (It is drawn with a colored border and options are available for altering its properties.)</p> <p>I would like to handle the <code>Delete</code> key so that, if the user is currently working in the container control (clicked within the control or on a child control, for instance) the currently selected control (if there is one) is deleted.</p> <p>I already have the delete functionality working using a right-click context menu on the child items. My problem is handling the <code>Delete</code> key. I cannot figure out how to get the KeyDown event to raise within my container control. I know it has something to do with focus, so that <code>Control.Select()</code> (or its equivalent) must be called, but what is the best way to go about this? Where does this focus logic reside? Or is there a better way?</p> <p>I do not want to handle the <code>KeyDown</code> event in the form and then sniff out where the focus is. This is a reuseable container control and I want the logic to reside there.</p> <p>What do I have to do to get the <code>KeyDown</code> event to fire on a custom control?</p> <pre><code>public class MyContainer : FlowLayoutPanel { protected override void OnKeyDown(KeyEventArgs e) { if (e.KeyCode == Keys.Delete) { MessageBox.Show("How do I get here?"); e.Handled = true; } base.OnKeyDown(e); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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