0

My application has several forms. What I want to do is to be able to switch between them (and only between them) using a pair of predefined keys (say Keys.A + Keys.B), just like ALT + TAB does for Windows (windows are shown in foreground). I tried getting the list of forms and then programmatically call Alt + Tab, but, as expected, this allows to switch between all open windows and not only the ones belonging to the application.

Thanks for any help!

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Mirel Vlad
  • 2,032
  • 3
  • 27
  • 35

2 Answers2

1

You could implement IMessageFilter and add it to your Application, then globally process the messages of your application.

Here is how you do that.

public class MessageFilter : IMessageFilter
{
    public bool PreFilterMessage(ref Message m)
    {
        switch ((WindowsMessage)m.Msg)
        {
            case WindowsMessage.WM_KEYDOWN:
            case WindowsMessage.WM_SYSKEYDOWN:
                {
                    if (((int)m.WParam | ((int)Control.ModifierKeys)) != 0)
                    {
                        Keys keyData = (Keys)(int)m.WParam;

                        var activeForm = Form.ActiveForm;
                        var forms = Application.OpenForms.Cast<Form>().Where(x => x.Visible).ToArray();
                        int active = Array.IndexOf(forms, activeForm);
                        if (keyData == Keys.A)
                        {
                            int next = (active + 1)%forms.Length;
                            forms[next].Activate();//Activate next
                        }
                        else if (keyData == Keys.B)
                        {
                            int prev = (active - 1) % forms.Length;
                            forms[prev].Activate();//Activate previous
                        }

                    break;
                }
        }

        return false;
    }
}

class MainForm : Form
{
    protected override void OnLoad(EventArgs e)
    {
        Application.AddMessageFilter(new MessageFilter());
    }
}

You can find WindowsMessage enumeration here.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • This only switches between the application forms, which is good. But the switch behavior must be identical to that of alt + tab on Windows (all application windows must be visible in foreground and the users should be able to selected the one he wants) – Mirel Vlad Jan 13 '14 at 08:34
  • @mirelvlad Well that requires additional code, you can create a form with number of picture boxes which displays a thumbnail of displayed forms. Then show that to user, let him select what he wants. – Sriram Sakthivel Jan 13 '14 at 08:53
0

If you have multiple windows (forms) opened and they have normal caption or belong to MDI, then Ctrl+F6 is a standard shortcut to switch between them.

Otherwise, making a hotkey, to switch between forms is pretty trivial task:

  • all forms have to have KeyPreview = true and KeyDown | KeyUp event;
  • all forms instances have to be accessible (one possibility is Application.OpenForms);
  • when hotkey is pressed, find next / previous window and make it active.
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • What I actually need is the switch behavior. It must be identical to that of alt + tab on Windows (all application windows must be visible in foreground and the users should be able to selected the one he wants) – Mirel Vlad Jan 13 '14 at 10:17
  • @Sinatr You probably missed to see my answer, I posted a working sample of how to do that. but what OP asking is a lot to answer in SO :p – Sriram Sakthivel Jan 13 '14 at 19:17
  • @Sinatr your solution only solves the switch between the windows. What I am mostly interested in is the manner in which I could display them just as Windows does with alt + tab – Mirel Vlad Jan 16 '14 at 13:07
  • @mirelvlad, I got it, but there are no *easy* solution for that. You could implement *switch panel* by yourself, but if you want it to be a part of explorer (thinking about *Aero* 3d), then well, I pass out ^^. – Sinatr Jan 16 '14 at 15:46
  • @Sinatr I am working on it (debugging the alt + tab source code) and I will post the answer once I have it. Thanks for your help! – Mirel Vlad Jan 16 '14 at 15:49