Making child dialogs consume keyUp events.
Is it possible to force a child dialog to consume KeyUp events so they are not passed to the parent?
Here is a sample program:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Tester2
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.listBox1.SelectedItem = 0;
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.ContextMenu = this.contextMenu1;
this.listBox1.Items.AddRange(new object[] {
"Test String 1"});
this.listBox1.Location = new System.Drawing.Point(8, 24);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(184, 173);
this.listBox1.TabIndex = 0;
this.listBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.listBox1_KeyUp);
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "Dialog Popup...";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
// using the keyboard to close the message box will exhibit the problem
MessageBox.Show("Message Box", "Caption", MessageBoxButtons.OK);
}
private void listBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
MessageBox.Show("Why is the dialog passing the keyup event to the parent control?");
}
}
}
Use the right-click menu to open a message box. Use the keyboard to close the box and the key event is passed to the parent.