XAF WinForms – Focus the accordion navigation control with a Shortcut

Browsing the support center I found this nice trick to focus the accordion navigation control with a shortcut.

As always, I decided to (besides bookmark it 😎)  do a quick post to refer to it later on.

So, let’s see it in action:

public class AccordionNavigationFocusWindowController : WindowController, IMessageFilter
   {
       const int WM_KEYDOWN = 0x0100;
       AccordionControl accordion;
       public AccordionNavigationFocusWindowController()
       {
           TargetWindowType = WindowType.Main;
       }
       protected override void OnActivated()
       {
           base.OnActivated();
           System.Windows.Forms.Application.AddMessageFilter(this);
           Frame.GetController<ShowNavigationItemController>().ShowNavigationItemAction.CustomizeControl += ShowNavigationItemAction_CustomizeControl;
       }
       private void ShowNavigationItemAction_CustomizeControl(object sender, CustomizeControlEventArgs e)
       {
           if (e.Control is AccordionControl)
           {
               accordion = (AccordionControl)e.Control;
               ((AccordionSearchControl)accordion.GetFilterControl()).NullValuePrompt = "Search (Ctrl+Q)";
           }
       }
       public bool PreFilterMessage(ref Message m)
       {
           if (m.Msg == WM_KEYDOWN)
           {
               Keys key = (Keys)m.WParam.ToInt32();
               if (key == Keys.Q && Control.ModifierKeys == Keys.Control)
               {
                   ((AccordionSearchControl)accordion.GetFilterControl()).Focus();
               }
           }
           return false;
       }
   }

If we check the code we see a Windows Controller that implements the IMessageFilter interface with the PreFilterMessage method. The purpose of this is to add a message filter to monitor Windows messages and if they meet our shortcut then we focus the navigation control.

Also we added a more user friendly text:  “Search (Ctrl+Q)” (as an analogy to Visual Studio) to guide the user.

Source Code here.

See you next time. XAF out!

Posted in XAF

Leave a Reply

Your email address will not be published. Required fields are marked *