Move Controls With Mouse.

Hello Friends,
Here is a very simple procedure to move controls runtime. it is useful when we need to move a control while application is running. Control.MouseDown and Control.MouseMove are used to do this work done. And Controls will be movable runtime as smooth as it can be moved at design time.

To have this done, start a new project with VB
Project name by default will be WindowsApplication1
Now Create few controls from tool bar
Button1, TextBox1, ComboBox1, PictureBox1

and write code as below.


Public Class Form1

Dim cordinates As Point

Private Sub Control_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown, Button1.MouseDown, ComboBox1.MouseDown, TextBox1.MouseDown

cordinates = New Point(-e.X, -e.Y)

End Sub

Private Sub Control_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove, Button1.MouseMove, ComboBox1.MouseMove, TextBox1.MouseMove

If e.Button = Windows.Forms.MouseButtons.Left Then
Dim ControlPosition As Point = Me.PointToClient(MousePosition)
ControlPosition.Offset(cordinates.X, cordinates.Y)
sender.Location = ControlPosition
End If
End Sub

End Class


Now run the application. and you can move controls while application is running.


Attachments

  • Move Controls Runtime. (35686-291312-MoveControls.zip)
  • Comments

    Author: ABitSmart29 Dec 2009 Member Level: Gold   Points : 0

    Please format your resource.

    Author: Krish19 Jan 2010 Member Level: Bronze   Points : 1

    hello dami. how can we do the same with c#? because i am getting touble doing the same in c#

    Author: piragar30 Apr 2010 Member Level: Bronze   Points : 0

    like it thaks

    Author: sameer28 Oct 2010 Member Level: Bronze   Points : 1

    hi its very cool.. but i want to select many controls by mouse and wanna move simulntanesly .. is there any idea for it?



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: