473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

drag and drop

Hi

How do i drag and drop a command button using the mouse in runtime?

Its driving me mad

I am using Visual Basic 6

Thanx Dave
Jul 17 '05 #1
4 4889
> How do i drag and drop a command button using the mouse in runtime?

Give the following a try. To see it work, start a new project, place a
CommandButton (use the default Name) on the form, paste the following code
into the Form's code window and then Run the project. Clicking on the
CommandButton will work as before. However, if you press the Control Key
first (you can change that to the Shift or Alt key if you want) and then
click on the CommandButton, you will be able to drag it around. I put some
code in the MouseDown event which is currently commented out. If you
uncomment that code, the user will not be able to place the CommandButton
off of the Form.

Rick - MVP

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 Declare Function ReleaseCapture Lib "user32" () As Long

Private Const WM_NCLBUTTONDOWN = &HA1
Private Const HTCAPTION = 2

Private Sub Command1_Click()
Debug.Print "I still work!"
End Sub

Private Sub Command1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
If Shift = vbCtrlMask Then
If Button = vbLeftButton Then
Call ReleaseCapture
With Command1
Call SendMessage(.hWnd, WM_NCLBUTTONDOWN, _
HTCAPTION, ByVal 0&)
' If .Left < 0 Then .Left = 0
' If .Top < 0 Then .Top = 0
' If .Left + .Width > Me.ScaleWidth Then
' .Left = Me.ScaleWidth - .Width
' End If
' If .Top + .Height > Me.ScaleHeight Then
' .Top = Me.ScaleHeight - .Height
' End If
End With
End If
End If
End Sub
Jul 17 '05 #2

"dave" <da********@blueyonder.co.uk> wrote in message
news:rG*************@news-binary.blueyonder.co.uk...
Hi

How do i drag and drop a command button using the mouse in runtime?

Its driving me mad

I am using Visual Basic 6

Thanx Dave


You could also try manually coded dragging. You can restrict the motion
to left-right by changing only the .Left property, or to up-down by
changing only the .Top.

Option Explicit

Dim mMoving As Boolean
Dim mStartX As Single
Dim mStartY As Single

Private Sub command1_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)
'control left click to drag
If Button = vbLeftButton And Shift = vbCtrlMask Then
mMoving = True
mStartX = X
mStartY = Y
End If
End Sub

Private Sub command1_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
Dim xNew As Single
Dim yNew As Single

If mMoving Then

With Command1

xNew = .Left + X - mStartX
yNew = .Top + Y - mStartY

If xNew >= 0 And xNew + .Width <= Me.ScaleWidth Then
.Left = xNew
End If

If yNew >= 0 And yNew + .Height <= Me.ScaleHeight Then
.Top = yNew
End If

End With

End If

End Sub

Private Sub command1_MouseUp(Button As Integer, Shift As Integer, X As
Single, Y As Single)
mMoving = False
End Sub

Jul 17 '05 #3
"Steve Gerrard" <no*************@comcast.net> wrote
You could also try manually coded dragging.


Whoa, I just got an episode of Deja Vu!
<g>
LFS

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 17 '05 #4

"Larry Serflaten" <Ab***@SpamBusters.com> wrote in message
news:40********@corp.newsgroups.com...
"Steve Gerrard" <no*************@comcast.net> wrote
You could also try manually coded dragging.


Whoa, I just got an episode of Deja Vu!
<g>
LFS


Cool, I like it when that happens to me <g>.

Are you referring to my code looking like Rick's code, or that I posted
in microsoft.public.vb.general.discussion as well, or that my phrasing
is weird?

Actually the effect of moving the target in the mouse move event,
instead of calling ReleaseCapture in a mouse down event, is somewhat
different. Rick's code doesn't apply conditions to the position of the
target until the mouse is released, while the mouse move approach
doesn't move the target at all unless the conditions are met.
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: hemant_mishal | last post by:
Hi All, I am writting a java script library which will provide inter frame drag and drop operation. The more precise will be OLE drag and drop, which means only data will be transfered from frame...
0
by: Lauren Quantrell | last post by:
I'm trying to drop a file from Windows Explorer (or desktop, etc.) onto a field in Access2K and capture the full file path. I found an posting below that says this is possible but I cannot...
2
by: SamSpade | last post by:
There seems to be two ways to put things on the clipboard ( I don't mean different formats): SetClipboardData and OleSetClipboard If I want to get data off the clipboard do I care how it was put...
3
by: Ajay Krishnan Thampi | last post by:
I have a slight problem implementing 'drag and drop' from a datagrid to a tree-view. I have pasted my code below. Someone please advice me on what to do...pretty blur right now. ==code== ...
6
by: jojobar | last post by:
Hello, I look at the asp.net 2.0 web parts tutorial on the asp.net web site. I tried to run it under firefox browser but it did not run. If I want to use this feature in a commercial product...
3
by: VB Programmer | last post by:
In VB.NET 2005 (winform) any sample code to drag & drop items between 2 listboxes? Thanks!
0
by: ViRi | last post by:
I am currently experimenting a bit with AxMicrosoft.MediaPlayer.Intero­p.AxWindowsMediaPlayer and so far, most has gone well. Currently, i would like to add drag-and-drop functionality to the...
1
by: Darren | last post by:
I'm trying to create a file using drag and drop. I want to be able to select a listview item drag it to the shell and create a file. Each icon in the listview represents a blob in a database. When...
0
by: RHSFSS | last post by:
Hi, I have a Drag and Drop registration problem (See http://www.thescripts.com/forum/thread434707.html for similar problem post), can anyone out thereadvise on the best solution? I have a .NET 2.0 ...
5
by: Romulo NF | last post by:
Greetings, I´m back here to show the new version of the drag & drop table columns (original script ). I´ve found some issues with the old script, specially when trying to use 2 tables with...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.