Good Morning Guys,

I have scoured the net for help with this problem of temporarily disabling mouse clicks on a VB Net form and have not been able to find a solution that works.

I'd like to prevent users form clicking on a form when i'm doing some background processing. To me it sounds simple in theory but apparently its not. Does anyone have any ideas?

Quite simple.
Add a Panel to your Form, place all the controls you would like not to be accessed in it, and disable the Panel. After your code is done processing, set it back to Panel1.Enabled=True.
If you disable the Form, you cannot access it, as to closing or moving the Form.

One option you have is to implement the IMessageFilter Interface to filter out mouse clicks. When the form loads set the filter, when your process is completed remove the filter. Here's an example.

First, when you implement IMessageFilter you have to have a PreFilterMessage functions that returns a Boolean

Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
	Select Case m.Msg
	    Case LButtonDown, LButtonUp, LButtonDoubleClick
	        Return True
	End Select
End Function

Here are the constant values for LButtonDown, LButtonUp, LButtonDoubleClick

Private Const LButtonDown As Integer = &H201
Private Const LButtonUp As Integer = &H202
Private Const LButtonDoubleClick As Integer = &H203

Now when my form loads I add the message filter by calling Application.AddMessageFilter

Like so

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Application.AddMessageFilter(Me)
End Sub

Then at the end of your process call Application.RemoveMessageFilter

Application.RemoveMessageFilter(Me)

And be sure to remove the filter once your form is closed as well

Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
    Application.RemoveMessageFilter(Me)
End Sub

Hope that helps :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.