12 Replies - 478 Views - Last Post: 16 December 2010 - 12:35 PM

#1 claudiolins  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-September 07

How to move a control at run-time

Posted 23 September 2007 - 09:56 AM

Description: Declare the constants :

Public Const WM_NCLBUTTONDOWN = &HA1
Public Const HTCAPTION = 2


put a textbox called textbox1 and paste the code below...
this code can be used with any type of control. Just pass its handle to the message.create function.

Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
    If e.Button = Windows.Forms.MouseButtons.Left Then
        TextBox1.Capture = False
        Dim msg As Message = _
            Message.Create(TextBox1.Handle, WM_NCLBUTTONDOWN, _
                New IntPtr(HTCAPTION), IntPtr.Zero)
        Me.DefWndProc(msg)
    End If
End Sub

This post has been edited by AdamSpeight2008: 11 January 2015 - 08:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to move a control at run-time

#2 claudiolins  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-September 07

Re: How to move a control at run-time

Posted 23 September 2007 - 09:56 AM

Description: Declare the constants : Public Const WM_NCLBUTTONDOWN = &HA1 Public Const HTCAPTION = 2 put a textbox called textbox1 and paste the code below into the form code... This code can be used with any type of control. Just pass its handle to the message.create function. Before draging another control, confirm the location properties for the last control dragged at MouseUp event. This is simple and prevents labels and images from getting back the original position.
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
    If e.Button = Windows.Forms.MouseButtons.Left Then
        TextBox1.Capture = False
        Dim msg As Message = _
            Message.Create(TextBox1.Handle, WM_NCLBUTTONDOWN, _
                New IntPtr(HTCAPTION), IntPtr.Zero)
        Me.DefWndProc(msg)
    End If
End Sub

Was This Post Helpful? 0
  • +
  • -

#3 jagatworld  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 11-May 08

Re: How to move a control at run-time

Posted 16 May 2008 - 08:27 PM

Amazing.... never expected it to be soo simple.....
Was This Post Helpful? 0
  • +
  • -

#4 redzer0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 29-June 08

Re: How to move a control at run-time

Posted 28 June 2008 - 11:04 PM

hi im using the above snippet for textbox but it wont work with label and image control, i mean it does move the control to a new location but when i change the location of a textbox the label and image control moves to its original location.. how can i resolve this? ive just change the message.create handle.. here what ive change..

    Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
        If e.Button = System.Windows.Forms.MouseButtons.Left Then
            Label1.Capture = False
            Dim msg As Message = _
                Message.Create(Label1.Handle, WM_NCLBUTTONDOWN, _
                    New IntPtr(HTCAPTION), IntPtr.Zero)
            Me.DefWndProc(msg)
        End If
    End Sub

This post has been edited by AdamSpeight2008: 11 January 2015 - 08:25 PM

Was This Post Helpful? 0
  • +
  • -

#5 redzer0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 29-June 08

Re: How to move a control at run-time

Posted 28 June 2008 - 11:04 PM

    Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
        If e.Button = System.Windows.Forms.MouseButtons.Left Then
            Label1.Capture = False
            Dim msg As Message = _
                Message.Create(Label1.Handle, WM_NCLBUTTONDOWN, _
                    New IntPtr(HTCAPTION), IntPtr.Zero)
            Me.DefWndProc(msg)
        End If
    End Sub

Was This Post Helpful? 0
  • +
  • -

#6 claudiolins  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-September 07

Re: How to move a control at run-time

Posted 29 June 2008 - 09:13 AM

Well i tried your sample code and it works exactly like mine. Try to set the location properties ata mouseup event of each control.
Was This Post Helpful? 0
  • +
  • -

#7 santhics  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 11-November 08

Re: How to move a control at run-time

Posted 11 November 2008 - 12:41 AM

Great work I am having the same problem that for the label, we can drag it but when i change the location of another control, the label goes to its original position. Can anyone help me in solving this issue
Was This Post Helpful? 0
  • +
  • -

#8 claudiolins  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-September 07

Re: How to move a control at run-time

Posted 19 February 2009 - 08:42 AM

santhics , Please send your code and ill help you solving the issue. Best regards, Claudio Lins
Was This Post Helpful? 0
  • +
  • -

#9 oonchai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 04-June 09

Re: How to move a control at run-time

Posted 04 June 2009 - 05:14 PM

The code helped me alot, thx! For santhics and redzer0, just turn off the Autosize for the label control and the code will work flawlessly.
Was This Post Helpful? 0
  • +
  • -

#10 mradulas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 03-July 09

Re: How to move a control at run-time

Posted 03 July 2009 - 10:21 PM

You guys rock !! I was trying to move a picture box when press the up button on the form and then try and move it down pressing the down button.
Was This Post Helpful? 0
  • +
  • -

#11 daveofgv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 06-November 08

Re: How to move a control at run-time

Posted 13 December 2009 - 08:38 PM

How do you confirm the location properties for the last control dragged at MouseUp event?
Was This Post Helpful? 0
  • +
  • -

#12 daveofgv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 06-November 08

Re: How to move a control at run-time

Posted 13 December 2009 - 09:13 PM

Also, When you use this code on a button then it appears that it deactivates the click option. Any suggestions? BTW - you guys rock for this snippet
Was This Post Helpful? 0
  • +
  • -

#13 UserNameHere  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 16-December 10

Re: How to move a control at run-time

Posted 16 December 2010 - 12:35 PM

Hello, Just want to thank you first for the code. Elegant and simple, I have used it several times with success. I now, however, have a program that will be making controls dynamically at runtime. Those controls need to be able to be moved by the user. I see two options for this code, neither of which I am capable of writing myself.

1) Add code similar to the code you provided above to a custom control within its Protected Overrides Sub onmousedown procedure. Then the window just dynamically creates instances of this control and they do all the work.

2) Create an Event Handler within the window (as done before) that can take these runtime created controls and do the same thing with them.

Thank you ahead of time for any time and effort you put into this (successful or not).

This post has been edited by AdamSpeight2008: 11 January 2015 - 08:25 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1