I have a UserControl
that has inside some other controls and then a Panel
that I use to paint custom shapes on mouse events. I would like to add some other functionalities but for that I would need to detect KeyDown
and KeyUp
while the mouse is drawing shapes on the Panel (in this situation UserControl.KeyDown/KeyUp
never fire). How can be this achieved?
EDIT
Here is a minimum version for the UserControl where the problem can be seen:
public partial class UserControl1 : UserControl
{
TextBox textBox1;
Panel panel1;
public UserControl1()
{
InitializeComponent();
textBox1 = new System.Windows.Forms.TextBox();
textBox1.Location = new System.Drawing.Point(0, 0);
this.Controls.Add(this.textBox1);
panel1 = new System.Windows.Forms.Panel();
panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
panel1.Location = new System.Drawing.Point(0, 25);
panel1.Size = new System.Drawing.Size(300, 150);
panel1.MouseEnter += new System.EventHandler(this.panel1_MouseEnter);
this.Controls.Add(this.panel1);
this.Size = new System.Drawing.Size(300, 200);
this.KeyDown += UserControl1_KeyDown;
}
private void panel1_MouseEnter(object sender, EventArgs e)
{
panel1.Focus();
}
void UserControl1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Key down");
}
}
When this control is inside a Form the "Key down" message is never displayed.