MDI: Change CTRL-Tab behaviour

Hi experts

How can I change the ctrl-tab behaviour of a MDI-Container.
Normally it just "loops" through all the mdi-children, but need to exclude some of them.
Where and how can I change this behaviour?

I'm using .NET 2.0, VS2008, c# and WinForms
LVL 3
ArikaelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Hi Arikael,

In your MdiParent Form, the gray area where the MdiChild forms appear is known as the MdiClient window.  Whenever you press Ctrl-Tab, this window will get a WM_MDINEXT message which causes it to select the next MdiChild.

See WM_MDINEXT:
http://msdn.microsoft.com/en-us/library/ms644918(VS.85).aspx

In my example below, whenever an MdiChild of Type Form2 is selected as a result of a Ctrl-Tab, I send another WM_MDINEXT message causing the next MdiChild to be selected.  This technically doesn't SKIP Form2 since it is actually selected for a split second before the next MdiChild gets selected.

Sorry that it is in VB.Net...I'm still coming to grips with coding in C#.  =\

Hopefully you can make sense of what is going on...
Public Class Form4
 
    Private client As MyMdiClient
 
    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is MdiClient Then
                client = New MyMdiClient(ctl, Me)
                Exit For
            End If
        Next
 
        Dim f1 As New Form1
        f1.MdiParent = Me
        f1.Show()
 
        Dim f2 As New Form2
        f2.MdiParent = Me
        f2.Show()
 
        Dim f3 As New Form3
        f3.MdiParent = Me
        f3.Show()
    End Sub
 
    Private Class MyMdiClient
        Inherits NativeWindow
 
        Private MyMdiParent As Form
        Private Const WM_MDINEXT As Integer = &H224
 
        Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" _
            (ByVal handle As IntPtr, ByVal wMsg As Integer, _
            ByVal wParam As Integer, ByVal lParam As Integer) As Integer
 
        Public Sub New(ByVal client As MdiClient, ByVal parent As Form)
            Me.AssignHandle(client.Handle)
            Me.MyMdiParent = parent
        End Sub
 
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            Select Case m.Msg
                Case WM_MDINEXT
                    Debug.Print("WM_MDINEXT")
                    Debug.Print("Before: " & Me.MyMdiParent.ActiveMdiChild.Name)
 
                    MyBase.WndProc(m)
                    Application.DoEvents()
 
                    Debug.Print("After: " & Me.MyMdiParent.ActiveMdiChild.Name)
 
                    ' Skip over to the next MdiChild if the current one is of Type Form2
                    If TypeOf Me.MyMdiParent.ActiveMdiChild Is Form2 Then
 
                        ' *** BE CAREFUL! ***
 
                        ' If all the MdiChildren are of Type Form2 then this
                        ' will cause an infinite loop...
 
                        SendMessage(Me.Handle, WM_MDINEXT, 0, 0)
                    End If
                    Exit Sub
 
            End Select
            MyBase.WndProc(m)
        End Sub
 
    End Class
 
 
End Class

Open in new window

0
ArikaelAuthor Commented:
thanks, I will have a look at it today
0
ArikaelAuthor Commented:
here is the code I used (c#)
0x0224 is 548 in the decimal system.
And I put the code into the MdiParent, into my class from which every MdiChild inherits but the code within

if(m.Msg == WM_MDINEXT)

never gets executed, because the m.Msg is never 548.
Any ideas why this could happen?
private const int WM_MDINEXT = 0x0224;
 
protected override void WndProc(ref Message m)
        {
            System.Diagnostics.Debug.WriteLine(m.Msg);
            if(m.Msg == WM_MDINEXT)
            {
                base.WndProc(ref m);
 
                if(this.ActiveMdiChild is MetaDetail)
                {
                    Application.DoEvents();
                    PostMessage(this.Handle, WM_MDINEXT, 0, 0);
                }
            }
 
            base.WndProc(ref m);
        }
 
        [DllImport("user32.dll")]
        public static extern System.IntPtr PostMessage(System.IntPtr hwnd, int msg, int wParam, int lParam);

Open in new window

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Mike TomlinsonMiddle School Assistant TeacherCommented:
As I said before, the WM_MDINEXT message gets sent to the MdiClient window (the gray area in the MdiParent).  This is a child window of the main MdiParent.  If you look in the Load() event, I am iterating over the Controls() collection looking for this control:

    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is MdiClient Then
                client = New MyMdiClient(ctl, Me)
                Exit For
            End If
        Next
    End Sub

I pass the MdiClient to an instance of the MyMdiClient() class...which Inherits from NativeWindow().  So in my example, WndProc() is actually for the MdiClient ONLY and not the entire MdiParent.

I'll code up a working example in C# 2005 Express today if you can't figure it out.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is a working C# version:  (C# 2005 Express)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
 
namespace WindowsApplication1
{
    public partial class frmMdiParent : Form
    {
        public frmMdiParent()
        {
            InitializeComponent();
        }
 
        private MyMdiClient client = null;
 
        private void frmMdiParent_Load(object sender, EventArgs e)
        {
            foreach (Control ctl in this.Controls)
            {
                if (ctl is MdiClient)
                {
                    this.client = new MyMdiClient((MdiClient)ctl, this);
                }
            }
 
            Form1 f1 = new Form1();
            f1.MdiParent = this;
            f1.Show();
 
            Form2 f2 = new Form2();
            f2.MdiParent = this;
            f2.Show();
 
            Form3 f3 = new Form3();
            f3.MdiParent = this;
            f3.Show();
        }
 
        private class MyMdiClient : NativeWindow
        {
 
            private Form MyMdiParent = null;
            private const int WM_MDINEXT = 0x224;
 
            [DllImport("user32.dll")]
            public static extern int PostMessage(IntPtr handle, int msg, int wParam, int lParam);
            
            public MyMdiClient(MdiClient mdiClient, Form mdiParent)
            {
                this.AssignHandle(mdiClient.Handle);
                this.MyMdiParent = mdiParent;
            }
 
            protected override void WndProc(ref Message m)
            {
                switch (m.Msg)
                {
                    case WM_MDINEXT:
                        Debug.Print("WM_MDINEXT");
                        Debug.Print("Before: " + this.MyMdiParent.ActiveMdiChild.Name);
 
                        base.WndProc(ref m);
                        Application.DoEvents();
 
                        Debug.Print("After: " + this.MyMdiParent.ActiveMdiChild.Name);
 
                        // Skip over Form2 when Ctrl-Tab is pressed
                        if (this.MyMdiParent.ActiveMdiChild is Form2)
                        {                            
                            PostMessage(this.Handle, WM_MDINEXT, 0, 0);
                        }
 
                        return;
 
                }
 
                base.WndProc(ref m);
            }
 
        }
 
    }
}

Open in new window

0
ArikaelAuthor Commented:
ah, sorry, I didn't unterstand the term MdiClient at first.
Now everything works as expected

I changed the code in MdiParent to (see code below), client was not defined in the c# code.

P.S. I raised the points to 500, thank you very much for your help :-)
  MyMdiClient client;
 
            foreach(Control ctl in this.Controls)
            {
                if(ctl is MdiClient)
                {
                    client = new MyMdiClient((MdiClient)ctl, this);
                    break;
                }
            }

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.