Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
I've run into an issue when my application loses focus to another application and then when the user comes back to my application and their first action is to click on a toolstrip button then have to click it twice: once to activate the form and then second to execute the button's event.
 
If I go to another control first and that sets the focus (activates the form) then I can go to the toolstrip button and the button's click event fires.
 
I was wondering if anyone else has run into this issue? Known bug? Workaround? At this point it is annoying to the users.
 
This happens in XP (users) or in my dev. environment of Win7 x64. targetting .Net Framework 3.5 or 4.0.
Posted 28-Oct-10 9:29am
Pete BSC1.8K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Include this in your Form with ToolStrips... (;
 

private bool handleFirstClickOnActivated = false;
 
/// <summary>
/// Raises the <see cref="E:System.Windows.Forms.Form.Activated" /> event.
/// Handle WinForms bug for first click during activation
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
protected override void OnActivated(EventArgs e)
{
    base.OnActivated(e);
    if (this.handleFirstClickOnActivated)
    {
        var cursorPosition = Cursor.Position;
        var clientPoint = this.PointToClient(cursorPosition);
        var child = this.GetChildAtPoint(clientPoint);
        while (this.handleFirstClickOnActivated && child != null)
        {
            var toolStrip = child as ToolStrip;
            if (toolStrip != null)
            {
                this.handleFirstClickOnActivated = false;
                clientPoint = toolStrip.PointToClient(cursorPosition);
                foreach (var item in toolStrip.Items)
                {
                    var toolStripItem = item as ToolStripItem;
                    if (toolStripItem != null && toolStripItem.Bounds.Contains(clientPoint))
                    {
                        var tsMenuItem = item as ToolStripMenuItem;
                        if (tsMenuItem != null)
                        {
                            tsMenuItem.ShowDropDown();
                        }
                        else
                        {
                            toolStripItem.PerformClick();
                        }
                        break;
                    }
                }
            }
            else
            {
                child = child.GetChildAtPoint(clientPoint);
            }
        }
        this.handleFirstClickOnActivated = false;
    }
}
 

/// <summary>
/// Handle WndProc
/// </summary>
/// <param name="m">The Windows <see cref="T:System.Windows.Forms.Message" /> to process.</param>
protected override void WndProc(ref Message m)
{
    const int WM_ACTIVATE = 0x0006;
    const int WA_CLICKACTIVE = 0x0002;
    if (m.Msg == WM_ACTIVATE && Low16(m.WParam) == WA_CLICKACTIVE)
    {
        handleFirstClickOnActivated = true;
    }
    base.WndProc(ref m);
}
 
private static int GetIntUnchecked(IntPtr value)
{
    return IntPtr.Size == 8 ? unchecked((int)value.ToInt64()) : value.ToInt32();
}
 
private static int Low16(IntPtr value)
{
    return unchecked((short)GetIntUnchecked(value));
}
 
private static int High16(IntPtr value)
{
    return unchecked((short)(((uint)GetIntUnchecked(value)) >> 16));
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

That's because you need to get focus back (the 1st click) and then click the button (the 2nd click). All apps do this.
 
You could handle the focus event on the form, and reflect the mouse click event to the toolbar, or otherwise evaluate it's position in relation tot he toolbar and take the necessary actions manually.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

It's standard behavior. You could handle the focus event in the parent form and pass the click onto the child control (in this case, the toolstrip button). It's kinda hacky, since you may also have to figure out what control is underneath the mouse when the click occurs.
 
One potential problem with this is if the user switches focus to the form using the keyboard, then it'd be kinda tricky to figure out if the form gained focus due to a click or if it got focus otherwise.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 290
1 OriginalGriff 275
2 Sergey Alexandrovich Kryukov 195
3 BillWoodruff 140
4 ProgramFOX 140
0 OriginalGriff 6,524
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,218
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 13 Feb 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100