2006-04-30

ToolStrip and it's responsiveness

I had a problem with the new .NET 2.0 component "ToolStrip". The problem appeared when switching between forms within an application by clicking on one of the toolstrip buttions. The first click was not "catched" as an event. This was really annoying and I was near switching to the toolstrip components of Infragistics.

First I found a quick solution. I added the event "MouseHover" and then focused on the toolstrip like this:

Private Sub tolMain_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tolMain.MouseHover
tolMain.Focus()
End Sub

It worked but it seemed unnesseary. I did a quick search on the Internet and I found a post from one who had faced and solved the problem. Read more about it here: http://blogs.msdn.com/rickbrew/archive/2006/01/09/511003.aspx

He hade made a C# example and here is my code, converted to VB.NET:


Imports System.Windows.Forms.ToolStrip
Public Class ToolStripEx
Inherits ToolStrip
Private bolClickThrough As Boolean = False

Public Property ClickThrough() As Boolean
Get
Return bolClickThrough
End Get
Set(ByVal value As Boolean)
bolClickThrough = value
End Set
End Property

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

Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
If bolClickThrough Then
If m.Msg = NativeConstants.WM_MOUSEACTIVATE And m.Result = CType(NativeConstants.MA_ACTIVATEANDEAT, IntPtr) Then
m.Result = CType(NativeConstants.MA_ACTIVATE, IntPtr)
End If
End If
End Sub
End Class

Friend Class NativeConstants
Friend Const WM_MOUSEACTIVATE As UInt32 = 33
Friend Const MA_ACTIVATE As UInt32 = 1
Friend Const MA_ACTIVATEANDEAT As UInt32 = 2
Friend Const MA_NOACTIVATE As UInt32 = 3
Friend Const MA_NOACTIVATEANDEAT As UInt32 = 4
End Class

No comments: