?
Solved

MDI Child & Key Preview...

Posted on 2006-07-13
12
Medium Priority
?
2,327 Views
Last Modified: 2008-01-09
Hi...

I need to catch, for example, a Ctrl+A key press on a form.
Usually I set the KeyPreview to true and handle the KeyDown Event... no problem.

Now I need to do this on an MDIChild form but the KeyDown event just doesn't fire.
Although it does fire on the MDI Parent.

Is there a way to work this out?

Thanks,
Alex
0
Comment
Question by:Alexandre Simões
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17098786
Handle keyboard event in MDI parent and call functions of MDI child using ActiveMdiChild propertty.
0
 
LVL 30

Author Comment

by:Alexandre Simões
ID: 17098997
Hi... Thanks for the reply...
That is not an option on my scenario.
This MDIParent holds 16 different projects that have an overall of 1000+ Forms...
It's not "doable" to hadle each case on the MDIParent...

Although that gave me a great idea...
I can create and Interface, implement it on the child forms, validate on the MDIParent KeyDown Event if there's an active MDIChild and if so if it also implements my Interface then finally if everything is true, call the method on the MDI Active Child.

Great!!
Thanks for putting my head to work...
Alex
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17102228
I did a small test with two forms, and this is what I discovered:

1) Parent has KeyPreview = True
2) Child has KeyPreview = True
3) Child Form_KeyPress:

    private void OnFormKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
       this.textBox1.AppendText(e.KeyChar);
      e.Handled = true;
    }

4) Parent Form_KeyPress:

    private void OnFormKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
       MessageBox.Show(e.KeyChar);
    }

5) Without the e.Handled = true in the child handler, the message would flow up to the parent, and I would get a message box.  With it I don't.

Bob
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 30

Author Comment

by:Alexandre Simões
ID: 17104641
I Bob...

I tried that but couldn't reproduce the behavior you said.
I tried some combinations with the KeyPreview set to true and false between MDI parent and child and nothing.

If I use the KeyPressEvent instead of the KeyDown I lose some informations on the eventargs and still the parent gets called.
It gets called before the child form...

Can you describe the configurations you use the get that behavior?

Thanks!
Alex
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1500 total points
ID: 17107903
Alex,

Here is a different test, with different code, and I am hoping for a better result:

Form1 (parent):

  KeyPreview = true;

  private void Form1_Load(object sender, System.EventArgs e)
  {

    this.IsMdiContainer = true;

    Form2 f = new Form2();
    f.MdiParent = this;
    f.Show();

  }

  private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  {
    if (this.ActiveMdiChild == null)
      MessageBox.Show("Form1 key down");
  }

Form2 (child):

  private void Form2_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  {
    MessageBox.Show("Form2 key down");
    e.Handled = true;
  }

Test #1:
   Run this code:  Get 'Form2 key down'

Test #2:
   Comment out 'f.Show();'.  Get 'Form1 key down'.

Bob
0
 
LVL 30

Author Comment

by:Alexandre Simões
ID: 17108889
Hi...
Thanks Bob... that worked but... :)

They came with some new ideas...
We use MenuStrips on both child and parent, and assign shortcut keys to them.
If the same shortcut exists on both parent and child, the child event will never raise...

Any ideas?!

Many thanks!
Alex
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17110535
Can you show me what the menus look like?  I bet you can't assign unique shortcut keys to the menu items, right?  That one doesn't sound quite so easy ;)

Bob
0
 
LVL 30

Author Comment

by:Alexandre Simões
ID: 17110748
I can't show you but I can guide you to reproduce my scenario.

Scenario:
1- Create 2 Forms, one MDIParent and one MDIChild
2- Add a MenuStrip to both
3- Add one MenuItem on both
4- Set the same ShortcutKey on both MenuItems (use the ShortcutKey property on the Menu item)
5- Add a messagebox to the MenuItemsClick so you can identify whitch was called

Test:
Having both forms visible (MDIParent with the MDIChild on it), try to use the shortcut key to trigger the MDIChild MenuItem.

Result:
The MDIParent is the one that allways gets called!!



Thanks!
Alex
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17129352
Alex,
That is a significantly more difficult proposition.  I haven't found a solution yet.

Bob
0
 
LVL 30

Author Comment

by:Alexandre Simões
ID: 17129587

The MDI structure itself is very limitted...
I think this is one more thing that is just no ment to be done.

As this is a Fox upgrade, we had some other things that had to be left behind an dsome others that we had to do strange
|stupid things to accomplish (some good examples are simply desable tab pages... and a lot of stuff related to the DataGridView).

Many thanks Bob,
Alex
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17129659
BTW, here is my failed attempt:

    private void TestToolStripMenuItem_Click(object sender, EventArgs e)
    {

        int shortCutValue = (int)((ToolStripMenuItem)sender).ShortcutKeys;

        if (this.ActiveMdiChild != null)
        {
            foreach (Control ctl in this.ActiveMdiChild.Controls)
            {
                if (ctl.GetType() == typeof(MenuStrip))
{
                    MenuStrip strip = (MenuStrip)ctl;
                    foreach (ToolStripItem item in strip.Items)
                    {
                        if (item.GetType() == typeof(ToolStripMenuItem) && ((int)((ToolStripMenuItem)item).ShortcutKeys) == shortCutValue)
                        {
                            return;
                        }
                    }
                }
            }
        }
        MessageBox.Show("Main form");
     }

Bob
0
 
LVL 3

Expert Comment

by:CarlVerret
ID: 20079055
I had the same kind of problem (shortcut in mdimain and client form with same key)  with VB.net 2005.  

I haven't found any solution yet.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question