MDI Form and KeyUp Event

  • Thread starter Thread starter Tyler
  • Start date Start date
T

Tyler

Is it possible to capture keypress events (KeyDown, KeyPress, KeyUp) for an
MDI form? I have created a simple form where I only changed its name and
its IsMdiContainer property (set to true). I added a handler for a KeyUp
event, but the event is never fired?

How can I capture keypresses that occur at this level? I want to be able to
watch for CTRL-V/SHIFT-INSERT combination to handle a user's request to
paste into my MDI window.

Thanks, Tyler
 
Tyler,

This is not exactly what you want but may get you close.
Sub KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles
MyBase.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(13)
'Do Something
End If
End Sub
Be sure to set the Keypreview property on the form to True

Bob Ranck
 
Thanks Bob,

My problem is that I think I'm doing exactly what you outlined, but the
event is never getting fired. I set a breakpoint in my handler, run the
program and press all kinds of keys (even regular keys - i.e.: not CTRL-V)
and the breakpoint is never hit. It has to be something with the
IsMdiContainer property as, when I set it to false, the KeyUp events are
fired.

Attached is a quick code snipet that demonstrates my problem. I create a
new Windows Forms Application (C#). I set the IsMdiContainer property to
true. I then double-click on the KeyUp event to cause VS.Net to generate
the Form1_KeyUp handler. I set a breakpoint within the handler, run the
program, and press many keys - no KeyUp events are raised (I have also tried
KeyDown and KeyPress events and I do not get these either).

Can anyone explain how to get the KeyUp events fired to an MDI container
form?

Below is test application:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MDITest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

...

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
MessageBox.Show("KeyUp Handler!");
return;
}
}
}
 
Tyler,

First I work in VB.net so your code snippet doesn't help
me see your problem.

Second - be sure to check that the form that is receiving
Key codes has it's KeyPreview property set. It's easy to
miss this one.

Third. I'm actually receiving key code events on custom
controls that are placed on inherited forms that are
opened in an MDI, but I'm not actually use the key code
on the MDI itself.

Last- Why not set up "straw man" form to test that you
can raise key codes on a non MDI form. once you are
successful. Use the straw man form to throw an event to
the MDI. Once you have that working, then you should be
able to eliminate the straw man and "see" the key code
event on the form you want.

I hope this helps.

Bob Ranck
-----Original Message-----
Thanks Bob,

My problem is that I think I'm doing exactly what you outlined, but the
event is never getting fired. I set a breakpoint in my handler, run the
program and press all kinds of keys (even regular keys - i.e.: not CTRL-V)
and the breakpoint is never hit. It has to be something with the
IsMdiContainer property as, when I set it to false, the KeyUp events are
fired.

Attached is a quick code snipet that demonstrates my problem. I create a
new Windows Forms Application (C#). I set the IsMdiContainer property to
true. I then double-click on the KeyUp event to cause VS.Net to generate
the Form1_KeyUp handler. I set a breakpoint within the handler, run the
program, and press many keys - no KeyUp events are raised (I have also tried
KeyDown and KeyPress events and I do not get these either).

Can anyone explain how to get the KeyUp events fired to an MDI container
form?

Below is test application:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MDITest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

...

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
MessageBox.Show("KeyUp Handler!");
return;
}
}
}
Bob Ranck said:
Tyler,

This is not exactly what you want but may get you close.
Sub KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles
MyBase.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(13)
'Do Something
End If
End Sub
Be sure to set the Keypreview property on the form to True

Bob Ranck


.
 
Hi Tyler,
I think probably you forgot to set the KeyPreview property of the main form.
Here is my test code, you wil see the message in the KeyUp events in the
output box of VS.NET.
Please be free to let me know if you have anything unclear on this issue.
<code>
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WinForm_MDI_KeyEvent
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2});
this.menuItem1.Text = "File";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "New";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.IsMdiContainer = true;
this.KeyPreview = true;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs
e)
{
System.Diagnostics.Debug.WriteLine("Key_Up");
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
ChildForm frm = new ChildForm();
frm.MdiParent = this;
frm.Show();
}

private class ChildForm : System.Windows.Forms.Form
{
public ChildForm()
{
this.Text = "ChildForm";
this.KeyUp +=new KeyEventHandler(ChildForm_KeyUp);
}
private void ChildForm_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
System.Diagnostics.Debug.WriteLine("ChildForm_KeyUp");
}
}
}
}

</code>

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| From: "Tyler" <[email protected]>
| Subject: MDI Form and KeyUp Event
| Date: Tue, 14 Oct 2003 08:58:57 -0400
| Keywords: MDI KeyUp
| Lines: 12
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: g252.lweb.net 209.167.237.252
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:54393
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Is it possible to capture keypress events (KeyDown, KeyPress, KeyUp) for
an
| MDI form? I have created a simple form where I only changed its name and
| its IsMdiContainer property (set to true). I added a handler for a KeyUp
| event, but the event is never fired?
|
| How can I capture keypresses that occur at this level? I want to be able
to
| watch for CTRL-V/SHIFT-INSERT combination to handle a user's request to
| paste into my MDI window.
|
| Thanks, Tyler
|
|
|
 
Thanks Bob,

I got caught-up trying to figure out whether your VB.Net was doing the same
thing as my C# and missed the very obvious statement that I needed to enable
the KeyPreview property.

Tyler

Bob Ranck said:
Tyler,

First I work in VB.net so your code snippet doesn't help
me see your problem.

Second - be sure to check that the form that is receiving
Key codes has it's KeyPreview property set. It's easy to
miss this one.

Third. I'm actually receiving key code events on custom
controls that are placed on inherited forms that are
opened in an MDI, but I'm not actually use the key code
on the MDI itself.

Last- Why not set up "straw man" form to test that you
can raise key codes on a non MDI form. once you are
successful. Use the straw man form to throw an event to
the MDI. Once you have that working, then you should be
able to eliminate the straw man and "see" the key code
event on the form you want.

I hope this helps.

Bob Ranck
-----Original Message-----
Thanks Bob,

My problem is that I think I'm doing exactly what you outlined, but the
event is never getting fired. I set a breakpoint in my handler, run the
program and press all kinds of keys (even regular keys - i.e.: not CTRL-V)
and the breakpoint is never hit. It has to be something with the
IsMdiContainer property as, when I set it to false, the KeyUp events are
fired.

Attached is a quick code snipet that demonstrates my problem. I create a
new Windows Forms Application (C#). I set the IsMdiContainer property to
true. I then double-click on the KeyUp event to cause VS.Net to generate
the Form1_KeyUp handler. I set a breakpoint within the handler, run the
program, and press many keys - no KeyUp events are raised (I have also tried
KeyDown and KeyPress events and I do not get these either).

Can anyone explain how to get the KeyUp events fired to an MDI container
form?

Below is test application:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MDITest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

...

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
MessageBox.Show("KeyUp Handler!");
return;
}
}
}
Bob Ranck said:
Tyler,

This is not exactly what you want but may get you close.
Sub KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles
MyBase.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(13)
'Do Something
End If
End Sub
Be sure to set the Keypreview property on the form to True

Bob Ranck
-----Original Message-----
Is it possible to capture keypress events (KeyDown,
KeyPress, KeyUp) for an
MDI form? I have created a simple form where I only
changed its name and
its IsMdiContainer property (set to true). I added a
handler for a KeyUp
event, but the event is never fired?

How can I capture keypresses that occur at this level?
I want to be able to
watch for CTRL-V/SHIFT-INSERT combination to handle a
user's request to
paste into my MDI window.

Thanks, Tyler


.


.
 
Thank-you,

When I set the KeyPreview property, I now receive the keypress events. The
only problem I have using the KeyPreview is that it gives the MDI container
form the keystroke before any of the child windows receive it.

Ideally, I would like to have the MDI container get the keystroke if none of
its MDI child windows handle the keypress. Is there a nice/simple way to do
this, or must I write some special code to make my MDI container and child
windows interoperate to determine who handles the keypress? For example, I
could set KeyPreview to true and capture the KeyUp event in my MDI
container. Then I could always invoke a custom public method of my MDI
container's active MDI child to determine whether it wanted to handle the
keypress. I would work, but it does not seem as clean as it could be.

Thanks, Tyler


Ying-Shen Yu said:
Hi Tyler,
I think probably you forgot to set the KeyPreview property of the main form.
Here is my test code, you wil see the message in the KeyUp events in the
output box of VS.NET.
Please be free to let me know if you have anything unclear on this issue.
<code>
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WinForm_MDI_KeyEvent
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2});
this.menuItem1.Text = "File";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "New";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.IsMdiContainer = true;
this.KeyPreview = true;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs
e)
{
System.Diagnostics.Debug.WriteLine("Key_Up");
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
ChildForm frm = new ChildForm();
frm.MdiParent = this;
frm.Show();
}

private class ChildForm : System.Windows.Forms.Form
{
public ChildForm()
{
this.Text = "ChildForm";
this.KeyUp +=new KeyEventHandler(ChildForm_KeyUp);
}
private void ChildForm_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
System.Diagnostics.Debug.WriteLine("ChildForm_KeyUp");
}
}
}
}

</code>

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| From: "Tyler" <[email protected]>
| Subject: MDI Form and KeyUp Event
| Date: Tue, 14 Oct 2003 08:58:57 -0400
| Keywords: MDI KeyUp
| Lines: 12
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: g252.lweb.net 209.167.237.252
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:54393
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Is it possible to capture keypress events (KeyDown, KeyPress, KeyUp) for
an
| MDI form? I have created a simple form where I only changed its name and
| its IsMdiContainer property (set to true). I added a handler for a KeyUp
| event, but the event is never fired?
|
| How can I capture keypresses that occur at this level? I want to be able
to
| watch for CTRL-V/SHIFT-INSERT combination to handle a user's request to
| paste into my MDI window.
|
| Thanks, Tyler
|
|
|
 
Hi Tyler,
I'm afraid you need do it by your self, Window message will always first
route to the parent window, it's by design.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| From: "Tyler" <[email protected]>
| References: <#[email protected]>
<[email protected]>
| Subject: Re: MDI Form and KeyUp Event
| Date: Wed, 15 Oct 2003 09:02:07 -0400
| Lines: 208
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: g252.lweb.net 209.167.237.252
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:54484
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Thank-you,
|
| When I set the KeyPreview property, I now receive the keypress events.
The
| only problem I have using the KeyPreview is that it gives the MDI
container
| form the keystroke before any of the child windows receive it.
|
| Ideally, I would like to have the MDI container get the keystroke if none
of
| its MDI child windows handle the keypress. Is there a nice/simple way to
do
| this, or must I write some special code to make my MDI container and child
| windows interoperate to determine who handles the keypress? For example,
I
| could set KeyPreview to true and capture the KeyUp event in my MDI
| container. Then I could always invoke a custom public method of my MDI
| container's active MDI child to determine whether it wanted to handle the
| keypress. I would work, but it does not seem as clean as it could be.
|
| Thanks, Tyler
|
|
| | > Hi Tyler,
| > I think probably you forgot to set the KeyPreview property of the main
| form.
| > Here is my test code, you wil see the message in the KeyUp events in the
| > output box of VS.NET.
| > Please be free to let me know if you have anything unclear on this
issue.
| > <code>
| > using System;
| > using System.Drawing;
| > using System.Collections;
| > using System.ComponentModel;
| > using System.Windows.Forms;
| > using System.Data;
| >
| > namespace WinForm_MDI_KeyEvent
| > {
| > /// <summary>
| > /// Summary description for Form1.
| > /// </summary>
| > public class Form1 : System.Windows.Forms.Form
| > {
| > private System.Windows.Forms.MainMenu mainMenu1;
| > private System.Windows.Forms.MenuItem menuItem1;
| > private System.Windows.Forms.MenuItem menuItem2;
| > /// <summary>
| > /// Required designer variable.
| > /// </summary>
| > private System.ComponentModel.Container components = null;
| >
| > public Form1()
| > {
| > //
| > // Required for Windows Form Designer support
| > //
| > InitializeComponent();
| >
| > //
| > // TODO: Add any constructor code after InitializeComponent call
| > //
| > }
| >
| > /// <summary>
| > /// Clean up any resources being used.
| > /// </summary>
| > protected override void Dispose( bool disposing )
| > {
| > if( disposing )
| > {
| > if (components != null)
| > {
| > components.Dispose();
| > }
| > }
| > base.Dispose( disposing );
| > }
| >
| > #region Windows Form Designer generated code
| > /// <summary>
| > /// Required method for Designer support - do not modify
| > /// the contents of this method with the code editor.
| > /// </summary>
| > private void InitializeComponent()
| > {
| > this.mainMenu1 = new System.Windows.Forms.MainMenu();
| > this.menuItem1 = new System.Windows.Forms.MenuItem();
| > this.menuItem2 = new System.Windows.Forms.MenuItem();
| > //
| > // mainMenu1
| > //
| > this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
| > this.menuItem1});
| > //
| > // menuItem1
| > //
| > this.menuItem1.Index = 0;
| > this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
| > this.menuItem2});
| > this.menuItem1.Text = "File";
| > //
| > // menuItem2
| > //
| > this.menuItem2.Index = 0;
| > this.menuItem2.Text = "New";
| > this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
| > //
| > // Form1
| > //
| > this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
| > this.ClientSize = new System.Drawing.Size(292, 273);
| > this.IsMdiContainer = true;
| > this.KeyPreview = true;
| > this.Menu = this.mainMenu1;
| > this.Name = "Form1";
| > this.Text = "Form1";
| > this.KeyUp += new
System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
| >
| > }
| > #endregion
| >
| > /// <summary>
| > /// The main entry point for the application.
| > /// </summary>
| > [STAThread]
| > static void Main()
| > {
| > Application.Run(new Form1());
| > }
| >
| > private void Form1_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs
| > e)
| > {
| > System.Diagnostics.Debug.WriteLine("Key_Up");
| > }
| >
| > private void menuItem2_Click(object sender, System.EventArgs e)
| > {
| > ChildForm frm = new ChildForm();
| > frm.MdiParent = this;
| > frm.Show();
| > }
| >
| > private class ChildForm : System.Windows.Forms.Form
| > {
| > public ChildForm()
| > {
| > this.Text = "ChildForm";
| > this.KeyUp +=new KeyEventHandler(ChildForm_KeyUp);
| > }
| > private void ChildForm_KeyUp(object sender,
| > System.Windows.Forms.KeyEventArgs e)
| > {
| > System.Diagnostics.Debug.WriteLine("ChildForm_KeyUp");
| > }
| > }
| > }
| > }
| >
| > </code>
| >
| > Best regards,
| >
| > Ying-Shen Yu [MSFT]
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| >
| > This posting is provided "AS IS" with no warranties and confers no
rights.
| > You should not reply this mail directly, "Online" should be removed
before
| > sending, Thanks!
| >
| > --------------------
| > | From: "Tyler" <[email protected]>
| > | Subject: MDI Form and KeyUp Event
| > | Date: Tue, 14 Oct 2003 08:58:57 -0400
| > | Keywords: MDI KeyUp
| > | Lines: 12
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <#[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.windowsforms
| > | NNTP-Posting-Host: g252.lweb.net 209.167.237.252
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| > microsoft.public.dotnet.framework.windowsforms:54393
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
| > |
| > | Is it possible to capture keypress events (KeyDown, KeyPress, KeyUp)
for
| > an
| > | MDI form? I have created a simple form where I only changed its name
| and
| > | its IsMdiContainer property (set to true). I added a handler for a
| KeyUp
| > | event, but the event is never fired?
| > |
| > | How can I capture keypresses that occur at this level? I want to be
| able
| > to
| > | watch for CTRL-V/SHIFT-INSERT combination to handle a user's request
to
| > | paste into my MDI window.
| > |
| > | Thanks, Tyler
| > |
| > |
| > |
| >
|
|
|
 
Thank you,

That helps me understand what I need to do.

Tyler

Ying-Shen Yu said:
Hi Tyler,
I'm afraid you need do it by your self, Window message will always first
route to the parent window, it's by design.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| From: "Tyler" <[email protected]>
| References: <#[email protected]>
<[email protected]>
| Subject: Re: MDI Form and KeyUp Event
| Date: Wed, 15 Oct 2003 09:02:07 -0400
| Lines: 208
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: g252.lweb.net 209.167.237.252
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:54484
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Thank-you,
|
| When I set the KeyPreview property, I now receive the keypress events.
The
| only problem I have using the KeyPreview is that it gives the MDI
container
| form the keystroke before any of the child windows receive it.
|
| Ideally, I would like to have the MDI container get the keystroke if none
of
| its MDI child windows handle the keypress. Is there a nice/simple way to
do
| this, or must I write some special code to make my MDI container and child
| windows interoperate to determine who handles the keypress? For example,
I
| could set KeyPreview to true and capture the KeyUp event in my MDI
| container. Then I could always invoke a custom public method of my MDI
| container's active MDI child to determine whether it wanted to handle the
| keypress. I would work, but it does not seem as clean as it could be.
|
| Thanks, Tyler
|
|
| | > Hi Tyler,
| > I think probably you forgot to set the KeyPreview property of the main
| form.
| > Here is my test code, you wil see the message in the KeyUp events in the
| > output box of VS.NET.
| > Please be free to let me know if you have anything unclear on this
issue.
| > <code>
| > using System;
| > using System.Drawing;
| > using System.Collections;
| > using System.ComponentModel;
| > using System.Windows.Forms;
| > using System.Data;
| >
| > namespace WinForm_MDI_KeyEvent
| > {
| > /// <summary>
| > /// Summary description for Form1.
| > /// </summary>
| > public class Form1 : System.Windows.Forms.Form
| > {
| > private System.Windows.Forms.MainMenu mainMenu1;
| > private System.Windows.Forms.MenuItem menuItem1;
| > private System.Windows.Forms.MenuItem menuItem2;
| > /// <summary>
| > /// Required designer variable.
| > /// </summary>
| > private System.ComponentModel.Container components = null;
| >
| > public Form1()
| > {
| > //
| > // Required for Windows Form Designer support
| > //
| > InitializeComponent();
| >
| > //
| > // TODO: Add any constructor code after InitializeComponent call
| > //
| > }
| >
| > /// <summary>
| > /// Clean up any resources being used.
| > /// </summary>
| > protected override void Dispose( bool disposing )
| > {
| > if( disposing )
| > {
| > if (components != null)
| > {
| > components.Dispose();
| > }
| > }
| > base.Dispose( disposing );
| > }
| >
| > #region Windows Form Designer generated code
| > /// <summary>
| > /// Required method for Designer support - do not modify
| > /// the contents of this method with the code editor.
| > /// </summary>
| > private void InitializeComponent()
| > {
| > this.mainMenu1 = new System.Windows.Forms.MainMenu();
| > this.menuItem1 = new System.Windows.Forms.MenuItem();
| > this.menuItem2 = new System.Windows.Forms.MenuItem();
| > //
| > // mainMenu1
| > //
| > this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
| > this.menuItem1});
| > //
| > // menuItem1
| > //
| > this.menuItem1.Index = 0;
| > this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
| > this.menuItem2});
| > this.menuItem1.Text = "File";
| > //
| > // menuItem2
| > //
| > this.menuItem2.Index = 0;
| > this.menuItem2.Text = "New";
| > this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
| > //
| > // Form1
| > //
| > this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
| > this.ClientSize = new System.Drawing.Size(292, 273);
| > this.IsMdiContainer = true;
| > this.KeyPreview = true;
| > this.Menu = this.mainMenu1;
| > this.Name = "Form1";
| > this.Text = "Form1";
| > this.KeyUp += new
System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
| >
| > }
| > #endregion
| >
| > /// <summary>
| > /// The main entry point for the application.
| > /// </summary>
| > [STAThread]
| > static void Main()
| > {
| > Application.Run(new Form1());
| > }
| >
| > private void Form1_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs
| > e)
| > {
| > System.Diagnostics.Debug.WriteLine("Key_Up");
| > }
| >
| > private void menuItem2_Click(object sender, System.EventArgs e)
| > {
| > ChildForm frm = new ChildForm();
| > frm.MdiParent = this;
| > frm.Show();
| > }
| >
| > private class ChildForm : System.Windows.Forms.Form
| > {
| > public ChildForm()
| > {
| > this.Text = "ChildForm";
| > this.KeyUp +=new KeyEventHandler(ChildForm_KeyUp);
| > }
| > private void ChildForm_KeyUp(object sender,
| > System.Windows.Forms.KeyEventArgs e)
| > {
| > System.Diagnostics.Debug.WriteLine("ChildForm_KeyUp");
| > }
| > }
| > }
| > }
| >
| > </code>
| >
| > Best regards,
| >
| > Ying-Shen Yu [MSFT]
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| >
| > This posting is provided "AS IS" with no warranties and confers no
rights.
| > You should not reply this mail directly, "Online" should be removed
before
| > sending, Thanks!
| >
| > --------------------
| > | From: "Tyler" <[email protected]>
| > | Subject: MDI Form and KeyUp Event
| > | Date: Tue, 14 Oct 2003 08:58:57 -0400
| > | Keywords: MDI KeyUp
| > | Lines: 12
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <#[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.windowsforms
| > | NNTP-Posting-Host: g252.lweb.net 209.167.237.252
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| > microsoft.public.dotnet.framework.windowsforms:54393
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
| > |
| > | Is it possible to capture keypress events (KeyDown, KeyPress, KeyUp)
for
| > an
| > | MDI form? I have created a simple form where I only changed its name
| and
| > | its IsMdiContainer property (set to true). I added a handler for a
| KeyUp
| > | event, but the event is never fired?
| > |
| > | How can I capture keypresses that occur at this level? I want to be
| able
| > to
| > | watch for CTRL-V/SHIFT-INSERT combination to handle a user's request
to
| > | paste into my MDI window.
| > |
| > | Thanks, Tyler
| > |
| > |
| > |
| >
|
|
|
 
Back
Top