-
Jul 7th, 2013, 06:55 AM
#1
Thread Starter
Lively Member
[RESOLVED] Drag a button on a form
I just want to move a button on a form at run time. When I do a mousedown and drag the button I get an outline of the button. But when I do a mouseup the outline disapears and nothing happens. So where am I going wrong? Thanks.
Code:
Private Sub btnbutton_MouseDown(Index As Integer, Button As Integer, Shift As Integer, leftpos As Single, toppos As Single)
btnbutton(Index).Drag vbBeginDrag
End Sub
Private Sub btnbutton_MouseUp(Index As Integer, Button As Integer, Shift As Integer, leftpos As Single, toppos As Single)
btnbutton(Index).Left = leftpos
btnbutton(Index).Top = toppos
btnbutton(Index).Drag vbEndDrag
End Sub
-
Jul 7th, 2013, 07:03 AM
#2
Re: Drag a button on a form
Here, take a look at these examples.....
Code:
Option Explicit(uses a button, text, label and picturebox
'variables to store what coordinate within the control it is being dragged by
Dim iGrabX As Integer
Dim iGrabY As Integer
'remember what control is being dragged
Dim ControlZOrder As Long
Private Sub Command1_DragDrop(Source As Control, X As Single, Y As Single)
'control was dropped somewhere so move it to the point where it was dropped and offset it by the coordinates within the control where you are dragging
Source.Move Command1.Left + X - iGrabX, Command1.Top + Y - iGrabY
End Sub
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
'remember what part of the control you are dragging by
iGrabX = X
iGrabY = Y
'begin dragging the control
Command1.Drag vbBeginDrag
Else
ControlZOrder = Command1.hWnd
PopupMenu mnuPopUp
End If
End Sub
Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
'mouse button released so stop dragging
Command1.Drag vbEndDrag
End If
End Sub
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
'control was dropped somewhere so move it to the point where it was dropped and offset it by the coordinates within the control where you are dragging
Source.Move X - iGrabX, Y - iGrabY
End Sub
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
Private Sub Label1_DragDrop(Source As Control, X As Single, Y As Single)
'control was dropped somewhere so move it to the point where it was dropped and offset it by the coordinates within the control where you are dragging
Source.Move Label1.Left + X - iGrabX, Label1.Top + Y - iGrabY
End Sub
Private Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
'remember what part of the control you are dragging by
iGrabX = X
iGrabY = Y
'begin dragging the control
Label1.Drag vbBeginDrag
Else
PopupMenu mnuPopUp
End If
End Sub
Private Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
'mouse button released so stop dragging
Label1.Drag vbEndDrag
End If
End Sub
Private Sub mnuBack_Click()
Dim I As Control
For Each I In Form1.Controls
If I.hWnd = ControlZOrder Then
I.ZOrder 1
Exit For
End If
Next
End Sub
Private Sub mnuFront_Click()
Dim I As Control
For Each I In Form1.Controls
If I.hWnd = ControlZOrder Then
I.ZOrder
Exit For
End If
Next
End Sub
Private Sub Picture1_DragDrop(Source As Control, X As Single, Y As Single)
'control was dropped somewhere so move it to the point where it was dropped and offset it by the coordinates within the control where you are dragging
Source.Move Picture1.Left + X - iGrabX, Picture1.Top + Y - iGrabY
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
'remember what part of the control you are dragging by
iGrabX = X
iGrabY = Y
'begin dragging the control
Picture1.Drag vbBeginDrag
Else
ControlZOrder = Picture1.hWnd
PopupMenu mnuPopUp
End If
End Sub
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
'mouse button released so stop dragging
Picture1.Drag vbEndDrag
End If
End Sub
Private Sub Text1_DragDrop(Source As Control, X As Single, Y As Single)
'control was dropped somewhere so move it to the point where it was dropped and offset it by the coordinates within the control where you are dragging
Source.Move Text1.Left + X - iGrabX, Text1.Top + Y - iGrabY
End Sub
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
'remember what part of the control you are dragging by
iGrabX = X
iGrabY = Y
'begin dragging the control
Text1.Drag vbBeginDrag
Else
ControlZOrder = Text1.hWnd
PopupMenu mnuPopUp
End If
End Sub
Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
'mouse button released so stop dragging
Text1.Drag vbEndDrag
End If
End Sub
-
Jul 7th, 2013, 07:36 AM
#3
Thread Starter
Lively Member
Re: Drag a button on a form
I got it working. Thanks SamOscarBrown.
Code:
Dim newleft As Integer
Dim newtop As Integer
Private Sub btnbutton_MouseDown(Index As Integer, Button As Integer, Shift As Integer, leftpos As Single, toppos As Single)
If Button = vbLeftButton Then
btnbutton(Index).Drag vbBeginDrag
End If
End Sub
Private Sub btnbutton_MouseUp(Index As Integer, Button As Integer, Shift As Integer, leftpos As Single, toppos As Single)
btnbutton(Index).Drag vbEndDrag
End Sub
Private Sub Form_DragDrop(Source As Control, leftpos As Single, toppos As Single)
Source.Move leftpos - newleft, toppos - newtop
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|