Many time we come across situation where user need to reposition the Form's control at RunTime.Below is simple solution for that,For sanmple purpose i am using Picture Box control but same logic can be applied to any other WinForm Control
Dim isDragged As Boolean = False Dim ptStartPosition As Point 'Handled the MouseDown event,In that if User has clicked the Left button than Capture the Starting coordinate of mouse. Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown If e.Button = MouseButtons.Left Then isDragged = True ptStartPosition = New Point(e.X, e.Y) Else isDragged = False End If End Sub
'Once Mouse is Up Reset the boolen Flag Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp isDragged = False End Sub 'InMousemove event perform the actual Transition of Control. Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove If isDragged Then Dim ptEndPosition As Point 'Set the New Transition point for Picturebox based on current mouse position and add appropriate offset to reduce flickering ptEndPosition = PictureBox1.PointToScreen(New Point(e.X, e.Y)) ptEndPosition.Offset(-ptStartPosition.X, -ptStartPosition.Y - PictureBox1.Height) PictureBox1.Location = ptEndPosition End If End Sub
|
No responses found. Be the first to respond and make money from revenue sharing program.
|