[RESOLVED] [2005] Adding If logic inside custom control class
I have this class that extends a picturebox to be movable at runtime. As an amateur, I didnt write it, just modified code I found at codeproject.
I have a boolean value that I would like to check with an if statement to disable the movability if false.
Being that this class overrides many methods I am not sure how to integrate the if logic into the class. Please advise :)
Code:
Public Class MovablePictureBox
Inherits System.Windows.Forms.PictureBox
' Used to store the current cursor shape when we start
' to move the control
Private m_CurrentCursor As Cursor
' Holds the mouse position relative to the inside of
' our control when the mouse button goes down
Private m_CursorOffset As Point
' Used by the MoveMove event handler to show that the
' setup to move the control has completed
Private m_Moving As Boolean
Public Sub New()
' Call the constructor of the base picturebox class to properly
' initialize our control
MyBase.New()
' Setup the Cursor property so that, by default, when the
' mouse passes over our control, the cursor changes to a hand
Me.Cursor = Cursors.Hand
End Sub
Private Sub MovablePictureBox_MouseDown( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseDown
' Check to see if the correct button has been pressed
If e.Button = Windows.Forms.MouseButtons.Left Then
' If so, the save the current cursor and
m_CurrentCursor = MyBase.Cursor
' replace it with our new image that says were in Movable mode
MyBase.Cursor = Cursors.SizeAll
' Save the location of the mouse pointer relative to the top-left
' corner of our control
m_CursorOffset = e.Location
' Set the mode flag to signal the MouseMove event handler that it
' needs to now calculate new positions for our control
m_Moving = True
End If
End Sub
Private Sub MovablePictureBox_MouseMove( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseMove
' Check which mode we're in. If we're supposed to be moving
' our control
If m_Moving Then
' get the screen position of the mouse pointer and map it
' to the position relative to the top-left corner of our
' parent container
Dim clientPosition As Point = _
MyBase.Parent.PointToClient(System.Windows.Forms.Cursor.Position)
' Calculate the new position of our control, maintaining
' the relative position stored by the MoveDown event
Dim adjustedLocation As New Point( _
clientPosition.X - m_CursorOffset.X, _
clientPosition.Y - m_CursorOffset.Y)
' Set the new position of our control
MyBase.Location = adjustedLocation
End If
End Sub
Private Sub MovablePictureBox_MouseUp( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseUp
' The button was released, so we're going back to Static mode.
m_Moving = False
' Restore the cursor image to the way we found it when the mouse
' button was pressed
MyBase.Cursor = m_CurrentCursor
End Sub
End Class
Re: [2005] Adding If logic inside custom control class
Ok just and my boolean (isAdmin) to
If m_Moving And isAdmin Then
Re: [2005] Adding If logic inside custom control class
in the mousemove method you can do this
you would need to add a property to set the movability then check it in the mousemove method
Code:
Public Class MovablePictureBox
Inherits System.Windows.Forms.PictureBox
' Used to store the current cursor shape when we start
' to move the control
Private m_CurrentCursor As Cursor
' Holds the mouse position relative to the inside of
' our control when the mouse button goes down
Private m_CursorOffset As Point
' Used by the MoveMove event handler to show that the
' setup to move the control has completed
Private m_Moving As Boolean
' The private variable to determine if the box is movable
Private m_movable As Boolean = True
' The public movable property
Public Property IsMovable() As Boolean
Get
Return m_movable
End Get
Set(ByVal value As Boolean)
m_movable = value
End Set
End Property
Public Sub New()
' Call the constructor of the base picturebox class to properly
' initialize our control
MyBase.New()
' Setup the Cursor property so that, by default, when the
' mouse passes over our control, the cursor changes to a hand
Me.Cursor = Cursors.Hand
End Sub
Private Sub MovablePictureBox_MouseDown( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseDown
' Check to see if the correct button has been pressed
If e.Button = Windows.Forms.MouseButtons.Left Then
' If so, the save the current cursor and
m_CurrentCursor = MyBase.Cursor
' replace it with our new image that says were in Movable mode
MyBase.Cursor = Cursors.SizeAll
' Save the location of the mouse pointer relative to the top-left
' corner of our control
m_CursorOffset = e.Location
' Set the mode flag to signal the MouseMove event handler that it
' needs to now calculate new positions for our control
m_Moving = True
End If
End Sub
Private Sub MovablePictureBox_MouseMove( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseMove
' Check which mode we're in. If we're supposed to be moving
' our control
If m_movable Then
If m_Moving Then
' get the screen position of the mouse pointer and map it
' to the position relative to the top-left corner of our
' parent container
Dim clientPosition As Point = _
MyBase.Parent.PointToClient(System.Windows.Forms.Cursor.Position)
' Calculate the new position of our control, maintaining
' the relative position stored by the MoveDown event
Dim adjustedLocation As New Point( _
clientPosition.X - m_CursorOffset.X, _
clientPosition.Y - m_CursorOffset.Y)
' Set the new position of our control
MyBase.Location = adjustedLocation
End If
End If
End Sub
Private Sub MovablePictureBox_MouseUp( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseUp
' The button was released, so we're going back to Static mode.
m_Moving = False
' Restore the cursor image to the way we found it when the mouse
' button was pressed
MyBase.Cursor = m_CurrentCursor
End Sub
End Class
Re: [RESOLVED] [2005] Adding If logic inside custom control class
Thanks. Better than my solution.