How to Prevent Mouse Movement and Clicks From Reaching Control

  • Thread starter Thread starter Charles Law
  • Start date Start date
C

Charles Law

Sorry for reposting this question, but I did not get a single answer last
time, and I'm sure you guys must have some thoughts on the matter.

I have a user control which can be dragged and dropped onto a form in my
application when it is running. I allow it to be clicked and dragged to a
new location on the form. However, the user control has a check box on it,
and if the user clicks over the checkbox to drag the user control, the check
box changes state.

Is there a way that I can simulate the design mode effect of the VS IDE,
such that whilst designing a form controls do not behave in the normal way?
The same problem occurs if clicking over a dropdown list: the list drops
down and then the control can be dragged away from under it. It doesn't look
very good.

It would be nice to stop all mouse effects on the member controls of my user
control, for example, checkboxes with the popup style change their
appearance when the mouse moves over them, but I would like to suppress this
whilst my user control is in 'design mode'.

Just to clarify, although I use the term 'design mode' my application is
actually running. I am just referring to the state in which it is running,
that is I allow users to design a simple form and then turn design mode off,
whereupon the form should behave normally.

TIA

Charles
 
Me.Capture = True

This will keep all of your controls from receiving any mouse messages.
You'll have to move any controls in the Forms mouse move events.
 
Hi yEaH rIgHt

Thanks for the idea. I have just tried it and it all behaves the same, but
the concept seems to be pointing the way. I will read up on it.

I put the Capture = True in the base class of my user controls so that it is
set automatically for anything that inherits from it, unless I have missed
something and I need to put it somewhere else?

Charles
 
Try this out (this will do what you want):

Option Explicit On
Option Strict On

Public Class UserControl1
Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'UserControl1 overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.CheckBox1 = New System.Windows.Forms.CheckBox()
Me.SuspendLayout()
'
'CheckBox1
'
Me.CheckBox1.Location = New System.Drawing.Point(72, 56)
Me.CheckBox1.Name = "CheckBox1"
Me.CheckBox1.TabIndex = 1
Me.CheckBox1.Text = "CheckBox1"
'
'UserControl1
'
Me.Controls.AddRange(New System.Windows.Forms.Control()
{Me.CheckBox1})
Me.Name = "UserControl1"
Me.Size = New System.Drawing.Size(288, 160)
Me.ResumeLayout(False)

End Sub

#End Region


Dim drag As Boolean
Private Sub UserControl1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
If drag Then
drag = False
Me.Capture = False
End If
End Sub

Private Sub UserControl1_MouseMove(ByVal sender As Object, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If drag Then
CheckBox1.Location = New Point(e.X, e.Y)
End If

End Sub

Private Sub CheckBox1_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles CheckBox1.MouseMove
If e.Button = MouseButtons.Left Then
Me.Capture = True
drag = True
End If
End Sub


End Class
 
Thanks for the example. I have tried it and it certainly prevents the mouse
click from changing the state of the checkbox.

I then changed the FlatStyle to Popup. If you try this you will see that
when you wave the mouse over the checkbox the appearance changes. This is
also an effect that I am trying to avoid. If I stop the application and open
the user control designer I do not get this effect, so it makes me think
that there is something else that the IDE is doing to achieve this. Any
ideas?

Cheers

Charles
 
That's because the controls on the designer aren't running. When you run
your program they are. There's nothing you can do about it.
 
Back
Top