[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1433
  • Last Modified:

ToolStrip/MenuStrip Focus

I recently converted an application from NET 1.1 to NET 2.0.  Among the many changes that were expected is one that I am having a little challenge overcoming.  It is probably fairly simple to fix, but I cannot figure it out right now.  My application is NOT an MDI application, but is uses multiple forms.  One of the forms works as a ToolBar.  It had a ToolBar and MainMenu(now ToolStrip and MenuStrip).  Previously, if I was working on one form and go to click a menuitem or toolbar button on the other form, it would select on the first click.  Now it is like you have to click once to focus the form and another time to select you control.  I found a solution earlier that uses the mouse_hover event to set focus, but I do not want the focus transferred to the other form until the user clicks.  Any suggestions on how to accomplish the behavior I was getting in NET 1.1???

Thanks,
Kendal
0
gvector1
Asked:
gvector1
  • 2
  • 2
1 Solution
 
oobaylyCommented:
Override the form's WndProc method. If the message is ParentNotify and the form hasn't got focus, Activate it. Then call the base WndProc. This means that the form already has focus when it's clicked on and the ToolStrip should now accept the same click.

Information on the ParentNotify Windows message:
http://msdn.microsoft.com/en-us/library/ms632638(VS.85).aspx
    protected override void WndProc(ref Message m) {
      int WM_PARENTNOTIFY = 0x0210;
      if (!this.Focused && m.Msg == WM_PARENTNOTIFY) {
        this.Activate();
      }
 
      base.WndProc(ref m);
    }

Open in new window

0
 
gvector1Author Commented:
Excellent, worked wonderfully. I wonder why this behavior changed, and I guess I'll have to do this on every form for this behavior.  Also, have you run across this before, or did you have to do some searching, I don't know if I ever would have run across this solution without knowing what direction to look.

Thanks,
Kendal
0
 
oobaylyCommented:
Can't comment on the behaviour change. As for having to do this on every form, you could create a Basic Form (ie. AutoFocusForm), and make the other forms in the project inherit from AutoFocusForm.

I've never come across the behaviour, just have experience with WndProc and Windows Messages and recognised that in your case you need to know a click event is being sent to the form before it hits the control's event handler
public class AutoFocusForm{
  bool grabFocus = true;
 
  [DefaultValue(true)]
  public bool GrabFocus{
    get{ return grabFocus; }
    set{ grabFocus = value; }
  }
 
  protected override void WndProc(ref Message m) {
    int WM_PARENTNOTIFY = 0x0210;
    if (grabFocus && !this.Focused && m.Msg == WM_PARENTNOTIFY) {
      this.Activate();
    }
    base.WndProc(ref m);
  }
}

Open in new window

0
 
gvector1Author Commented:
Thanks again for the input.
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now