|
-
Aug 8th, 2006, 04:03 AM
#1
[02/03] Drag and drop.
Hi All,
I want to learn more about drag and drop.
I've got this code, who's working very well, to drag and drop a Label over a form and with checking borders.
VB Code:
Private Sub Label1_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove
Static mousePosX As Single, mousePosY As Single
Dim currentx As Single, currenty As Single
If e.Button = 0 Then
mousePosX = e.X
mousePosY = e.Y
Else
currentx = Label1.Left + (e.X - mousePosX)
currenty = Label1.Top + (e.Y - mousePosY)
If Not (currentx < 0 Or currentx + Label1.Width > Me.Width) Then
Label1.Left = currentx
End If
If Not (currenty < 0 Or currenty + Label1.Height > Me.ClientSize.Height) Then
Label1.Top = currenty
End If
End If
End Sub
The problem I'm having is, what do I have to do that if I'm using several labels the Labelsborders will be checked (doesn't run over each other) and
the labels can be drag and dropped seperatly.
Thanks in advance,
sparrow1
-
Aug 8th, 2006, 09:49 AM
#2
Frenzied Member
Re: [02/03] Drag and drop.
I'm not exactly sure what you are asking, but I will hazzard a guess. You want to be able to move labels around on the form, but they can't overlap other labels?
First, the mouse move event you have, you can use for all the labels, add all the labels to the mousemove to this event. Then in the procedureinstead of using label1 use sender. Sender is the label that is beign moved.
Example
VB Code:
'used a click event for demonstration
Private Sub Labels_Click(sender as Object, e as EventArgs) Handles Label1.Click, Label2.Click, Label3.Click
Messagebox.Show(DirectCast(sender, Label).Text)
End Sub
Then to make sure the label isn't going to overlap another label, you may want to wait for the mouseup event after the mousemove. It may be too much work to do in the move. I would say create an array of all your labels that you are going to compare it to, every time you move a label you will have to update the array. Then it would be a matter of looping through the array.
VB Code:
Private Function SenderIsOverlaping(lbl As Label) As Boolean 'lbl is the moving label
'the array of labels is called ArrayLabels, and we will assume it is populated corectly
For l as Label in ArrayLabels
'loop through the array checking if the moving label overlaps anyother
Next
End Function
Hope this helps.
-
Aug 10th, 2006, 09:00 AM
#3
Re: [02/03] Drag and drop.
 Originally Posted by mpdeglau
I'm not exactly sure what you are asking, but I will hazzard a guess. You want to be able to move labels around on the form, but they can't overlap other labels?
First, the mouse move event you have, you can use for all the labels, add all the labels to the mousemove to this event. Then in the procedureinstead of using label1 use sender. Sender is the label that is beign moved.
Example
VB Code:
'used a click event for demonstration
Private Sub Labels_Click(sender as Object, e as EventArgs) Handles Label1.Click, Label2.Click, Label3.Click
Messagebox.Show(DirectCast(sender, Label).Text)
End Sub
Then to make sure the label isn't going to overlap another label, you may want to wait for the mouseup event after the mousemove. It may be too much work to do in the move. I would say create an array of all your labels that you are going to compare it to, every time you move a label you will have to update the array. Then it would be a matter of looping through the array.
VB Code:
Private Function SenderIsOverlaping(lbl As Label) As Boolean 'lbl is the moving label
'the array of labels is called ArrayLabels, and we will assume it is populated corectly
For l as Label in ArrayLabels
'loop through the array checking if the moving label overlaps anyother
Next
End Function
Hope this helps.
Hi mpdeglau,
Thanks for your reply, I'll check it later!
Wkr,
sparrow1
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
|