Results 1 to 2 of 2

Thread: [2005] Extended Class - Movable TableLayoutPanel

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    [2005] Extended Class - Movable TableLayoutPanel

    I copied the following code from a website and changed it to work for a tablelayout panel instead of picturebox. I extend the table layout panel to make it draggable at runtime but only if you log in to windows as an admin. I run into a bug on the clients machine, when the user is not an admin. If the mouse is accidentally clicked it offsets the position of the tablelayout panel. I know it has something to do with the way I am getting, setting the boolean value isadmin. Is there an easier way, where I can just return out of the overridden class without even overload the mouse constructors if the user doesnt have admin rights. I apologize for the question and realize it stems from my lack of knowing how classes work. Anyway, heres the code...
    Code:
      Public Class MovableTablePanel
            Inherits System.Windows.Forms.TableLayoutPanel
    
            ' 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 = False ' Oct
    
            ' 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 tablelayoutpanel 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
                If m_movable Then Me.Cursor = Cursors.Hand
            End Sub
    
            Private Sub MovableTablePanel_MouseDown( _
                        ByVal sender As Object, _
                        ByVal e As System.Windows.Forms.MouseEventArgs) _
                        Handles MyBase.MouseDown
                If m_movable Then
                    ' 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 If
            End Sub
    
            Private Sub MovableTablePanel_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 'jms
                        ' 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)
                        Debug.WriteLine("locb4: " & MyBase.Location.ToString)
                        ' Set the new position of our control
                        MyBase.Location = adjustedLocation
                        Debug.WriteLine("locaft: " & MyBase.Location.ToString)
                    End If
                End If
            End Sub
    
            Private Sub MovableTablePanel_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] Extended Class - Movable TableLayoutPanel

    anyone?

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