Results 1 to 21 of 21

Thread: [RESOLVED] Moving Controls At run time

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2007
    Posts
    912

    Resolved [RESOLVED] Moving Controls At run time

    hi all,
    i have to move the controls of my form eg: CheckBox, Radio, button,Textbox.
    at run time from one place to another by mouse dragging as we do while form designing.
    how can i acheive this? i want to use Mouse Events for this. Dont want to use any API

  2. #2
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Moving Controls At run time

    not too hard. on the mousedown event, you store the current x and y position of the mouse on the form control (so your mouse offset on the control is proper)

    then the code in the mousemove is easy...
    Code:
    Dim storedx As Single, storedy As Single
    Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    storedx = X
    storedy = Y
    
    End Sub
    
    Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button > 0 Then
    Command1.Left = Command1.Left + X - storedx
    Command1.Top = Command1.Top + Y - storedy
    End If
    End Sub
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2007
    Posts
    912

    Re: Moving Controls At run time

    thanks for Reply Lord,
    code is very fine but when i try to move the controls their Click Event Is fired. what should i do?

  4. #4
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Moving Controls At run time

    Load, why this does not work ? i have some confusion

    Code:
    Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button > 0 Then
    Command1.Left = X - storedx
    Command1.Top = Y - storedy
    End If
    End Sub

  5. #5
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: Moving Controls At run time

    Chunk: Try changing the code to this instead:
    Code:
    Dim storedx As Single, storedy As Single
    
    Private Sub Command1_Click()
    Print "Hello"
    End Sub
    
    Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    storedx = X
    storedy = Y
    End Sub
    
    Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbRightButton Then
    Command1.Left = Command1.Left + X - storedx
    Command1.Top = Command1.Top + Y - storedy
    End If
    End Sub
    This way it will only click and drag with the right button.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2007
    Posts
    912

    Re: Moving Controls At run time

    this is working but when i try to move the control then its clicked event is fired. If i press left mouse button but if i press right mouse button then it works fine.
    is not there any solution it work for left button too without firing Click Event.
    Code:
    Private Sub Check1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button > 1 Then
    Check1(Index).Left = Check1(Index).Left + X - storedx
    Check1(Index).Top = Check1(Index).Top + Y - storedy
    End If
    
    End Sub

  7. #7
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Moving Controls At run time

    Quote Originally Posted by Fazi
    Load, why this does not work ? i have some confusion

    Code:
    Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button > 0 Then
    Command1.Left = X - storedx
    Command1.Top = Y - storedy
    End If
    End Sub
    OK, I was messedup with the form Mouse Move and Command1 Mouse Move

  8. #8
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Moving Controls At run time

    chunk i have a temprary solution to your problem. not sure it is the professional way. But works !!

    Code:
    Private storedx As Single, storedy As Single
    Private moving As Boolean
    
    Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    storedx = X
    storedy = Y
    End Sub
    
    Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button > 0 Then
       Command1.Left = Command1.Left + X - storedx
       Command1.Top = Command1.Top + Y - storedy
       moving = True
    Else
       moving = False
    End If
    End Sub
    Private Sub Command1_Click()
    If moving = False Then
       MsgBox "hai"
    End If
    End Sub
    Last edited by Fazi; Sep 13th, 2007 at 03:17 AM.

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Moving Controls At run time

    Try this. This does not fire the click event
    Code:
    Option Explicit
    
    Private Declare Function ReleaseCapture Lib "user32" () As Long
    
    Private Declare Function SendMessage Lib "user32" Alias _
    "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, lParam As Any) As Long
    
    Private Const WM_NCLBUTTONDOWN = &HA1
    Private Const HTCAPTION = 2
    
    Private Sub Check1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ReleaseCapture
    SendMessage Check1.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0
    End Sub
    
    Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ReleaseCapture
    SendMessage Command1.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0
    End Sub
    
    Private Sub Option1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ReleaseCapture
    SendMessage Option1.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0
    End Sub
    
    Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ReleaseCapture
    SendMessage Text1.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0
    End Sub

  10. #10
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Moving Controls At run time

    Nice work Hack, though not sure if you noticed he doesn't want to use API. It would be interesting to see which method is quicker though.

  11. #11
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Moving Controls At run time

    The API is quicker, and in this case, the best solution. It also requires the least amount of code and is the most stable and dependable.

    At least, in my opinion.

  12. #12
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Moving Controls At run time

    yeah ours took up 5 or six lines and yours took up only a whole page
    But to clarify:
    click event will be fired if you use left mouse button. it is fired upon release. Why would you WANT to use the left for everything? That's why you have 3 buttons. But it is simple to prevent the code from firing if it moved. Just put your click code in the mouseup and compare the current mouse position to the stored one.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  13. #13
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Moving Controls At run time

    Quote Originally Posted by Lord Orwell
    yeah ours took up 5 or six lines and yours took up only a whole page
    But thats because you were demonstrating only using one control.

    I gave examples using 4 different controls hence it is three times longer.

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2007
    Posts
    912

    Re: Moving Controls At run time

    Thanks 03myersd, fazi, lord and hack.
    thanks for your replies. i think i should go with the one the way Fazi and
    03myersd has written. i also thought the same way of moving the control by Right Or middle button.
    hack really i want to avoid API. But the example you gave me is very nice but did you noticed click event does not fire even if you want to fire it?
    thanks guys

  15. #15
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: Moving Controls At run time

    I think Lord Orwells idea is actually better than mine. Put the click code in the command1_mouseup event. Then have an if statement comparing if the coordinates where the button went down are the same and if they are run the code.

  16. #16
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: [RESOLVED] Moving Controls At run time

    Right this code definately works:

    Code:
    Dim storedx As Single, storedy As Single
    Dim newy As Single, newx As Single
    
    Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    storedx = X
    storedy = Y
    newy = Command1.Top + Y
    newx = Command1.Left + X
    End Sub
    
    Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button > 0 Then
    Command1.Left = Command1.Left + X - storedx
    Command1.Top = Command1.Top + Y - storedy
    End If
    End Sub
    
    Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Command1.Left + X = newx And Command1.Top + Y = newy Then
        Print "Hello"
    End If
    End Sub

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2007
    Posts
    912

    Re: [RESOLVED] Moving Controls At run time

    03myersd,
    Still the same problem did you notice with the code you have written above. There still Click Event Fires when you Release the left button after dragging?
    Code:
    Dim storedx As Single, storedy As Single
    Dim newy As Single, newx As Single
    
    Private Sub Command1_Click()
    MsgBox "??"
    End Sub
    
    Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    storedx = X
    storedy = Y
    newy = Command1.Top + Y
    newx = Command1.Left + X
    End Sub
    
    Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button > 0 Then
    Command1.Left = Command1.Left + X - storedx
    Command1.Top = Command1.Top + Y - storedy
    End If
    End Sub
    
    Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Command1.Left + X = newx And Command1.Top + Y = newy Then
        Print "Hello"
    End If
    End Sub
    i will use the code Fazi gave me in #8. or i will go with the code i wrote above.
    Thanks
    Last edited by chunk; Sep 13th, 2007 at 12:06 PM. Reason: wrote [vbcode] instead [code]

  18. #18
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: [RESOLVED] Moving Controls At run time

    Remove the code:

    Code:
    Private Sub Command1_Click()
    MsgBox "??"
    End Sub
    Put the code in the mouse up event instead.

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2007
    Posts
    912

    Re: [RESOLVED] Moving Controls At run time

    code is not working at MouseUp
    Code:
    Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Command1.Left + X = newx And Command1.Top + Y = newy Then
        Print "Hello"
        MsgBox "??"
    End If
    End Sub
    Thanks for your help bro

  20. #20
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: [RESOLVED] Moving Controls At run time

    Can you elaborate on not working? This works 100% for me:

    Code:
    Dim storedx As Single, storedy As Single
    Dim newy As Single, newx As Single
    
    Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    storedx = X
    storedy = Y
    newy = Command1.Top + Y
    newx = Command1.Left + X
    End Sub
    
    Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button > 0 Then
    Command1.Left = Command1.Left + X - storedx
    Command1.Top = Command1.Top + Y - storedy
    End If
    End Sub
    
    Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Command1.Left + X = newx And Command1.Top + Y = newy Then
        Print "Hello"
        MsgBox "??"
    End If
    End Sub
    I don't think it will make the slightest bit of difference but what version of VB are you using?

  21. #21
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [RESOLVED] Moving Controls At run time

    Then I would guess that Command1.Left + X does not equal newx and Command1.Top + Y does not equal newy

    Put a break at the top of the sub and step through it to see what it is actually doing.

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