Help please...
I'm new member and this is my 1st post..

problem :
I want to drag n drop an object (label n picturebox) in 1 form.
what i means is the object that I drag can be free moving in 1 form n drop it on anywhere on that form.
is it possible??

note:
the object that i want to drag drop is created after 1 run the application.

help please...
thanks, before...

Recommended Answers

All 3 Replies

Public Class Form2
    Private Dragging As Boolean = False
    Private Lblx As Label = Nothing
    Private LocX, LocY As Integer
    Dim jml As Integer = 0
    Dim lbl(100) As Object

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As _
  System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        Label2.Text = "X: " & e.Location.X & " Y: " & e.Location.Y
        LocX = e.Location.X
        LocY = e.Location.Y
        Label2.Refresh()
    End Sub

    Private Function CanDrop() As Boolean
        Dim ok As Boolean = False
        Return ok
    End Function

    Private Sub Lbl_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        Dragging = True
        Lblx = New Label
        Lblx.Text = lbl(jml).Text
        Dim loc As Point

        loc.X = e.Location.X
        loc.Y = e.Location.Y
        Lblx.Location = loc
        Me.Controls.Add(Lblx)
    End Sub

    Private Sub Lbl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        Dim loc As Point

        loc.X = LocX + e.X
        loc.Y = LocY + e.Y

        If Dragging Then

            Lblx.Location = loc
            Lblx.BringToFront()
            Label3.Text = "lbl X = " & e.Location.X & "   lbl Y = " & e.Location.Y
        End If

    End Sub

    Private Sub Lbl_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
        If Dragging Then
            Dragging = False
            'If CanDrop() Then
            Dim loc As Point
            loc.X = e.Location.X + LocX
            loc.Y = e.Location.Y + LocY
            lbl(jml).Location = loc
            'End If
            Label4.Text = "Mouse Location = " & e.Location.X & ", " & e.Location.Y
        End If
        Lblx.SendToBack()
        Lblx.Text = ""
        Lblx = Nothing
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        jml = jml + 1
        lbl(jml) = New Label
        With lbl(jml)
            .text = "love U"
            .autosize = True
            .Location = New Point(100, 100)
            .BackColor = Color.Transparent
            .visible = True
        End With
        Me.Controls.Add(lbl(jml))
       
    End Sub
End Class

thats my code,
it's still using "Me" as a handler.
I want using label that i create when i run the program as a handler, and I still can't find a way to do that.

thanks

I made some fixes to code and tried to comment what and why I did. This is probably still not what you actually want to do but it may be a step further

Public Class Form2
  Private Dragging As Boolean = False
  ' I dropped this variable because we use arrays
  'Private Lblx As Label = Nothing
  ' Don't know the reason for these variables?
  Private LocX, LocY As Integer
  ' jml points to last Label. Array indices start from zero and we increment jml before creating a new label
  Dim jml As Integer = -1 '0
  'Dim lbl(100) As Object

  ' We use labels so declare them as labels. I also dropped 100 and use dynamic size of the array
  Private lbl() As Label
  
  Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove ' Me.MouseMove actually since this is this form's event
    Label2.Text = "X: " & e.Location.X & " Y: " & e.Location.Y
    LocX = e.Location.X ' ?
    LocY = e.Location.Y ' ?
    Label2.Refresh()
    ' We have to do "moving" in here because this is MyBase.MouseMove i.e. Form1.MouseMove (=Me.MouseMove)
    ' NOTICE: I dropped LocX and LocY
    If Dragging Then
      Dim loc As Point
      loc.X = e.Location.X '+ LocX
      loc.Y = e.Location.Y '+ LocY
      lbl(jml).Location = loc
    End If
  End Sub

  Private Function CanDrop() As Boolean
    Dim ok As Boolean = False
    Return ok
  End Function

  ' Renamed (this is Form1)
  Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    '  Private Sub Lbl_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    Dragging = True

    ' I commented this out. You created a label with Button1_Click

    'Lblx = New Label
    'Lblx.Text = lbl(jml).Text
    'Dim loc As Point

    'loc.X = e.Location.X
    'loc.Y = e.Location.Y
    'Lblx.Location = loc
    'Me.Controls.Add(Lblx)
  End Sub

  ' This is same as Form1_MouseMove but "hides" Form1_MouseMove. Since all the code is in Form1_MouseMove, I commented this out
  'Private Sub Lbl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
  '  Dim loc As Point

  '  loc.X = LocX + e.X
  '  loc.Y = LocY + e.Y

  '  If Dragging Then

  '    'Lblx.Location = loc
  '    'Lblx.BringToFront()
  '    Label3.Text = "lbl X = " & e.Location.X & "   lbl Y = " & e.Location.Y
  '  End If

  'End Sub

  ' Renamed (this is Form1)
  Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
    'Private Sub Lbl_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
    If Dragging Then
      Dragging = False
      'If CanDrop() Then
      Dim loc As Point
      ' NOTICE: I dropped LocX and LocY
      loc.X = e.Location.X '+ LocX
      loc.Y = e.Location.Y '+ LocY
      lbl(jml).Location = loc
      'End If
      Label4.Text = "Mouse Location = " & e.Location.X & ", " & e.Location.Y
    End If
    'Lblx.SendToBack()
    'Lblx.Text = ""
    'Lblx = Nothing
  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    jml = jml + 1

    ' Dynamically resize the array
    ReDim Preserve lbl(jml)

    lbl(jml) = New Label
    With lbl(jml)
      .Text = "love U"
      .AutoSize = True
      .Location = New Point(100, 100)
      .BackColor = Color.Transparent
      .Visible = True
    End With
    Me.Controls.Add(lbl(jml))

  End Sub

End Class

The code (after creating a label) moves it around the form when the mouse is down.

A few points:
-"Dragging" starts when you click in the form area, not even close the label.
- If you click with the mouse over the label, the label catches the MouseDown event, not the form, and dragging doesn't start.
- You can't use actual DragEnter, DragOver and DragDrop events since you're controls (labels) are in the array and you can't declare an array with WithEvents.
- Only the lastly created label can be moved now

I hope this gets you a bit further. I'm still not quite sure what you're trying to achieve. If you can clarify it a bit I might be able to help you more. But check first the changes I made to see if there's any help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.