I'm writing a program that generates labels at runtime, and then has them be "dragged and dropped" by the user. I have the drag and drop code working, but right now I can only get it to work on labels that I explicitly name in the code, like this:
Private Sub Control_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown Label2.MouseDown Label3.MouseDown
and so on...

But how can I get the event handler to handle every generated Label?
Please excuse my noobiness, the only instruction I've had in VB.NET is a one semester basichigh school course. Thanks in advance! :)

Recommended Answers

All 9 Replies

    Dim NewLabel As Label = New Label
    Me.Controls.Add(NewLabel)
    AddHandler NewLabel.Click, AddressOf MyLabelClick

    Private Sub MyLabelClick(ByVal sender As Object, ByVal e As EventArgs)
        'code
    End Sub

I have something similar, but it's not working.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Static x As Integer
        Dim lbl As New Label
        lbl.Name = "lbl" & ((x / 100) + 1).ToString()
        lbl.Text = lbl.Name
        lbl.Top = 100 + x
        lbl.Left = 100
        x = x + 100
        AddHandler lbl.Click, AddressOf Control_MouseDown
        AddHandler lbl.Click, AddressOf Control_MouseUp
        AddHandler lbl.Click, AddressOf Control_MouseMove
        Me.Controls.Add(lbl)
    End Sub

Private Sub Control_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        ' Set the flag
        Dragging = True
        ' Note positions of cursor when pressed
        cursorX = e.X
        CursorY = e.Y
    End Sub

Static x As Integer = 100 or whatever

That's not the problem, it's that I can't drag and drop the created labels.

O.K. You got the addhandler and an event. There is more to it. I have whipped up some minimal code you can start a new project,copy it in, put a button on the form, option strict = off - otherise you will get late binding errors, otherwise the code will work (VB.net express 13)

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Text
Public Class Form1
    Dim lbl As Label
    Dim flag As Integer = 0
    Dim i As Integer = 50
    Dim dragging As Boolean
    Dim beginX, beginY As Integer
    Dim counter As Integer = 1
    Private ptX, ptY As Integer
    Private drag As Boolean
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


        Dim lbl As New Label

        lbl.Name = "Label" & counter
        lbl.Text = lbl.Name
        lbl.Size = New Size(80, 20)

        lbl.Location = New Point(80, counter * 22)


        AddHandler lbl.Click, AddressOf labMouseClick
        AddHandler lbl.DragEnter, AddressOf labDragEnter
        AddHandler lbl.MouseDown, AddressOf labMouseDown
        AddHandler lbl.DragDrop, AddressOf labDragDrop
        AddHandler lbl.MouseUp, AddressOf labMouseUp
        AddHandler lbl.MouseMove, AddressOf labMouseMove
        Me.Controls.Add(lbl)

        counter += 1
    End Sub
    Private Sub labMouseMove(lbl As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If drag Then
            lbl.Location = New Point(lbl.Location.X + e.X - ptX, lbl.Location.Y + e.Y - ptY)
            Me.Refresh()
        End If
    End Sub
    Private Sub labDragDrop(sender As Object, e As DragEventArgs)
        e.Effect = DragDropEffects.Move
        Static mousePosX As Single, mousePosY As Single
        lbl.Left = lbl.Left + (e.X - mousePosX)
        lbl.Top = lbl.Top + (e.Y - mousePosY)
    End Sub
    Private Sub labMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        drag = False

    End Sub
    Private Sub labMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim lab As Label = DirectCast(sender, Label)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            drag = True
            lbl = CType(sender, Label)
            ptX = e.X : ptY = e.Y

        End If
    End Sub
    Private Sub labDragEnter(lab As Object, e As DragEventArgs)
        e.Effect = DragDropEffects.Move
    End Sub
    Private Sub labMouseClick(lab As Object, e As EventArgs)
        Dim xPos As Integer
        Dim yPos As Integer
        If Windows.Forms.MouseButtons.Left Then
            yPos = DirectCast(lbl, Label).Left()
            xPos = DirectCast(lbl, Label).Top()
            MsgBox("The X Position is " & xPos & " The Y Position is " & yPos)
        End If
    End Sub
End Class
commented: exactly +3

Ok everything works just as I need it too; thank you so much. Would you mind giving me a brief explanation of what's making this code work as opposed to mine?

Well that would take too long. Look at the imports. Look what you need to put into the eventhandlers and what the events are.Example: Detect on the label that the mouse has entered the region occupied by the label and is ready to drop and so on. You can also google each part of the events and will find out trhat also put it all together by googling. Good luck and don't forget to mark it as solved. Thanks

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.