Results 1 to 3 of 3

Thread: [02/03] Drag and drop.

  1. #1

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    [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:
    1. Private Sub Label1_MouseMove(ByVal sender As Object, _
    2.             ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove
    3.         Static mousePosX As Single, mousePosY As Single
    4.         Dim currentx As Single, currenty As Single
    5.  
    6.         If e.Button = 0 Then
    7.             mousePosX = e.X
    8.             mousePosY = e.Y
    9.         Else
    10.             currentx = Label1.Left + (e.X - mousePosX)
    11.             currenty = Label1.Top + (e.Y - mousePosY)
    12.             If Not (currentx < 0 Or currentx + Label1.Width > Me.Width) Then
    13.                 Label1.Left = currentx
    14.             End If
    15.  
    16.             If Not (currenty < 0 Or currenty + Label1.Height > Me.ClientSize.Height) Then
    17.                 Label1.Top = currenty
    18.             End If
    19.         End If
    20.     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
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    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:
    1. 'used a click event for demonstration
    2. Private Sub Labels_Click(sender as Object, e as EventArgs) Handles Label1.Click, Label2.Click, Label3.Click
    3.      Messagebox.Show(DirectCast(sender, Label).Text)
    4. 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:
    1. Private Function SenderIsOverlaping(lbl As Label) As Boolean 'lbl is the moving label
    2.    'the array of labels is called ArrayLabels, and we will assume it is populated corectly
    3.    For l as Label in ArrayLabels
    4.        'loop through the array checking if the moving label overlaps anyother
    5.    Next
    6. End Function

    Hope this helps.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  3. #3

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [02/03] Drag and drop.

    Quote 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:
    1. 'used a click event for demonstration
    2. Private Sub Labels_Click(sender as Object, e as EventArgs) Handles Label1.Click, Label2.Click, Label3.Click
    3.      Messagebox.Show(DirectCast(sender, Label).Text)
    4. 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:
    1. Private Function SenderIsOverlaping(lbl As Label) As Boolean 'lbl is the moving label
    2.    'the array of labels is called ArrayLabels, and we will assume it is populated corectly
    3.    For l as Label in ArrayLabels
    4.        'loop through the array checking if the moving label overlaps anyother
    5.    Next
    6. End Function

    Hope this helps.
    Hi mpdeglau,

    Thanks for your reply, I'll check it later!

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

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