[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
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
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?
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
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.
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
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 :p
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
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
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.
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.
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.
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. :D
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
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.
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
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
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.
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
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?
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.