Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Adding If logic inside custom control class

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    Resolved [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

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    Re: [2005] Adding If logic inside custom control class

    Ok just and my boolean (isAdmin) to
    If m_Moving And isAdmin Then

  3. #3
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    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
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    Re: [RESOLVED] [2005] Adding If logic inside custom control class

    Thanks. Better than my solution.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width