0

VB.Net 2008 Express Edition

"Form1" has a ToolStripContainer1.TopToolStripPanel which contains a ToolStrip with buttons. The buttons work on ONE click when "Form1" is active. If I click on another window and then return to "Form1" the ToolStrip buttons take TWO clicks to activate. The first click returns focus to "Form1" and the subsequent click fires the button event. I want the buttons to work on the first click and not require two clicks.

Note that ordinary buttons on "Form1" that are not part of the ToolStrip work on the first click when returning from another window/form!!!!????

Big Wave
  • 26
  • 5

1 Answers1

3

That's standard behavior. You can see Microsoft Outlook does the same thing if it doesn't have focus and you click on a tool button that is visible on the screen.

But you can override that behavior with your own version:

Public Class ToolStripEx
  Inherits ToolStrip

  Private Const WM_MOUSEACTIVE As Int32 = &H21

  Public Sub New()
    MyBase.New()
  End Sub

  Protected Overrides Sub WndProc(ByRef m As Message)
    If m.Msg = WM_MOUSEACTIVE AndAlso Me.CanFocus AndAlso Not Me.Focused Then
      Me.Focus()
    End If
    MyBase.WndProc(m)
  End Sub

End Class
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Genius...! It's very rare to receive an answer that works first time! – Big Wave Aug 19 '11 at 21:32
  • would someone be able to give me an example of how you call this code to stop the behavior? Cheers! – ar.dll Aug 16 '13 at 10:50
  • 1
    @4rd2 You replace the standard ToolStrip control with this one. You create a new class file like in the example, rebuild your solution, and the new ToolStripEx control should be available in the ToolBox. – LarsTech Aug 16 '13 at 12:01