0

I have a VB.net windows form application that will be used for users(our inside people) to customize the locations of certain variable data fields for a particular customer's needs. Each product type has certain fields that are available to be used and all of the default information for them exists in tables.

When a certain product is selected I can make sample data appear as labels with a tooltip letting them know the name of the field. All of that works.

The issue I have is making the labels be movable after the defaults are loaded.

I have found examples of code to allow for labels made at development time to be moved, but nothing that allows for labels made on the fly to be movable.

The second part is to be able to retrieve the new X and y coordinates from a moved field so records can be made for that customer.

Here is the sub that creates the labels. It is called from inside a loop that iterates through the available field data. One note: the fields appear within a panel the background of which will be an image that will be used to know where data can be put.

   Private Sub lblCreateLabel_Click(ByVal RecCount As Integer, ByVal FieldType As String, ByVal FieldName As String, ByVal FieldData As String, ByVal FieldWidth As Integer, ByVal FieldJustification As String, ByVal xpos As Integer, ByVal ypos As Integer, ByVal Color As String, ByVal Side As String, ByVal Font As String, ByVal FontSize As String, ByVal FontStyle As String, ByVal SetType As String)

        Dim label1 As New Label
        Dim intHorizontalAlignment As Integer = 0
        Dim intFontStyle As FontStyle = Drawing.FontStyle.Regular

        Select Case FieldJustification
            Case "Center"
                intHorizontalAlignment = 32
            Case "Left"
                intHorizontalAlignment = 16
            Case "Right"
                intHorizontalAlignment = 64
            Case Else
                intHorizontalAlignment = 16
        End Select

        Select Case FontStyle
            Case "Reg"
                intFontStyle = Drawing.FontStyle.Regular
            Case "Bold"
                intFontStyle = Drawing.FontStyle.Bold
            Case "Italic"
                intFontStyle = Drawing.FontStyle.Italic
            Case Else
                intFontStyle = Drawing.FontStyle.Regular
        End Select

        label1.Name = "txt" & RecCount
        label1.Text = FieldData.Replace("&", "&&")
        label1.Width = FieldWidth
        label1.ForeColor = System.Drawing.Color.FromName(Color.Replace("Color[", "").Replace("]", ""))
        label1.Location = New Point(xpos, ypos)
        Dim tt As New ToolTip()
        tt.SetToolTip(label1, FieldName)

        If FieldType = "Address" Or FieldType = "Slug" Or FieldType = "ChurchName" Then
            label1.AutoSize = True
        Else
            label1.AutoSize = False
        End If

        If FieldName.Contains("811PlaceStamp") Then
            label1.AutoSize = True
        End If

        If FieldType = "Barcode" Then
            label1.Height = 30
            label1.BackColor = Drawing.Color.Transparent
        Else
            label1.BackColor = Drawing.Color.Transparent
        End If

        label1.TextAlign = intHorizontalAlignment

        Try
            label1.Font = New Font(Font, CInt(FontSize), intFontStyle)
        Catch
        End Try

        If Side = "Face" Then
            pnlEnvelopeFace.Controls.Add(label1)
            label1.Refresh()
        Else
            pnlEnvelopeFlap.Controls.Add(label1)
            label1.Refresh()
        End If

    End Sub```

Any guidance would be very much appreciated.
behzad
  • 801
  • 3
  • 15
  • 33
tromba99
  • 1
  • 1
  • Add a handler to the `MouseDown` and `MouseMove` events. The event's `sender` object can be cast to Control (or Label), it represents the Control that raised the event. You can then use these events as usual. (unrelated) You could pass those arguments as the actual types/enumerators instead of strings: e.g., pass a `FontStyle` type parameter directly, so you don't need all those *conversions*. – Jimi Jan 03 '20 at 16:11
  • Use the question's code from [here](https://stackoverflow.com/questions/16909032/how-do-you-move-controls-in-vb-during-runtime-and-save-the-location-data) in the `MouseDown` event of your labels. –  Jan 03 '20 at 17:09
  • jimi ..thank you for a prompt response. My question would then be how do I add an event handler for a label that is being created programmatically? I have no issues adding a handler to a control that exists at development time, but this would be new to me. – tromba99 Jan 03 '20 at 20:38
  • Jimi - adding this inside of my sub got me started in the right direction. ```AddHandler label1.MouseDown, AddressOf Label_MouseDown```Is that what you meant? – tromba99 Jan 03 '20 at 20:50
  • Yes, this is what you should do in these cases. Remember that you can cast `sender` to `Control` (or `ListBox`, the actual type) e.g., `dim lstBox = DirectCast(sender, Control)` or `dim lstBox = DirectCast(sender, ListBox)`: `lstBox` is the Control that raised the event. You can set/use its properties/methods as usual. – Jimi Jan 04 '20 at 00:25

0 Answers0