用于动态添加控件的VB.NET MouseEventArgs

时间:2015-12-10 14:21:59

标签: vb.net dynamic drag-and-drop

我目前正在尝试创建一个塔式构建工具,这是基于拖放功能。我现在已经准备好让用户建造他的塔了,但现在我还想添加他可以调整他的塔的选项。这可以通过拖动添加的块来交换位置。

我目前正在使用flowlayoutpanel来安排我添加的块(groupboxes)。这些是根据输入在每个dragevent之后新创建的。

enter image description here

Public Class Form1
    Private Sub BODY_ADD_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
        BODY_ADD_CILINDER.MouseMove,
        BODY_ADD_CONE_DOWN.MouseMove,
        BODY_ADD_CONE_UP.MouseMove,
        BODY_ADD_HEAD_DOWN.MouseMove,
        BODY_ADD_HEAD_UP.MouseMove

    ' Initiate dragging.
    Me.DoDragDrop(sender, DragDropEffects.Copy)

End Sub

Private Sub BODY_Arrange_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
BODY_LAYOUT_PANEL.MouseMove

    ' Initiate dragging.
    Me.DoDragDrop(sender, DragDropEffects.Copy)

End Sub

Private Sub FlowLayoutPanel1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles BODY_LAYOUT_PANEL.DragEnter

    ' Check the format of the data being dropped.
    If (e.Data.GetDataPresent(GetType(PictureBox))) Then
        ' Display the copy cursor.
        e.Effect = DragDropEffects.Copy
    Else
        ' Display the no-drop cursor.
        e.Effect = DragDropEffects.None
    End If

End Sub

Private Sub BODY_LAYOUT_PANEL_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles BODY_LAYOUT_PANEL.DragDrop

    Dim oPB = e.Data.GetData(GetType(PictureBox))
    Dim oGB As New GroupBox

    oGB.Name = "Test" & GET_BODY_ITEM_NO()

    oGB.Visible = True
    oGB.Width = 520
    oGB.Height = 119


    If oPB.name = BODY_ADD_CILINDER.Name Then
        oGB.Text = "Cilinder" & GET_CILINDER_ITEM_NO()
        BODY_LAYOUT_PANEL.Controls.Add(oGB)
        FILL_CILINDER_GROUP_BOX(oGB)

    ElseIf oPB.name = BODY_ADD_CONE_DOWN.Name Then
        oGB.Text = "Cone down" & GET_BODY_ITEM_NO()
        BODY_LAYOUT_PANEL.Controls.Add(oGB)

    ElseIf oPB.name = BODY_ADD_CONE_UP.Name Then
        oGB.Text = "Cone up" & GET_BODY_ITEM_NO()
        BODY_LAYOUT_PANEL.Controls.Add(oGB)

    ElseIf oPB.name = BODY_ADD_HEAD_DOWN.Name Then
        oGB.Text = "Head down" & GET_HEAD_DOWN_ITEM_NO()
        BODY_LAYOUT_PANEL.Controls.Add(oGB)

    ElseIf oPB.name = BODY_ADD_HEAD_UP.Name Then
        oGB.Text = "Head up" & GET_BODY_ITEM_NO()
        BODY_LAYOUT_PANEL.Controls.Add(oGB)

    End If

End Sub

Private Sub FILL_CILINDER_GROUP_BOX(ByVal oGB As GroupBox)

    Dim oPB As New PictureBox
    oPB.Dock = DockStyle.Left
    oPB.SizeMode = PictureBoxSizeMode.StretchImage
    oPB.Image = My.Resources.ResourceManager.GetObject("Cilinder")
    oPB.Name = oGB.Name & "_PictureBox"
    oGB.Controls.Add(oPB)

    Dim oLBL1 As New Label

    oLBL1.Name = oGB.Name & "_LABEL_HOOGTE"
    oLBL1.Text = "Hoogte"
    oLBL1.Location = New System.Drawing.Point(128, 25)
    oLBL1.Width = 42
    oGB.Controls.Add(oLBL1)

    Dim oLBL2 As New Label

    oLBL2.Name = oGB.Name & "_LABEL_DIKTE"
    oLBL2.Text = "Dikte"
    oLBL2.Location = New System.Drawing.Point(138, 52)
    oLBL2.Width = 32
    oGB.Controls.Add(oLBL2)

    Dim oLBL3 As New Label

    oLBL3.Name = oGB.Name & "_LABEL_ORIENTATIE"
    oLBL3.Text = "Orientatie LW"
    oLBL3.Location = New System.Drawing.Point(311, 25)
    oLBL3.Width = 72
    oGB.Controls.Add(oLBL3)

    Dim oLBL4 As New Label

    oLBL4.Name = oGB.Name & "_LABEL_SEGMENTEN"
    oLBL4.Text = "Segmenten"
    oLBL4.Location = New System.Drawing.Point(322, 52)
    oLBL4.Width = 61
    oGB.Controls.Add(oLBL4)

    Dim oTB1 As New TextBox

    oTB1.Name = oGB.Name & "_TB_HOOGTE"
    oTB1.Location = New System.Drawing.Point(176, 22)
    oGB.Controls.Add(oTB1)

    Dim oTB2 As New TextBox

    oTB2.Name = oGB.Name & "_TB_DIKTE"
    oTB2.Location = New System.Drawing.Point(176, 49)
    oGB.Controls.Add(oTB2)

    Dim oTB3 As New TextBox

    oTB3.Name = oGB.Name & "_TB_ORIENTATIE"
    oTB3.Location = New System.Drawing.Point(389, 22)
    oGB.Controls.Add(oTB3)

    Dim oTB4 As New TextBox

    oTB4.Name = oGB.Name & "_TB_SEGMENTEN"
    oTB4.Location = New System.Drawing.Point(389, 45)
    oGB.Controls.Add(oTB4)

End Sub

Private Function GET_BODY_ITEM_NO()
    Return (BODY_LAYOUT_PANEL.Controls.Count)
End Function

Private Function GET_CILINDER_ITEM_NO()

    Dim s As String
    Dim y As Integer = 1

    For i = 0 To BODY_LAYOUT_PANEL.Controls.Count - 1
        s = BODY_LAYOUT_PANEL.Controls.Item(i).Text

        If s.Contains("Cilinder") Then
            y = y + 1
        End If
    Next

    Return (y)
End Function

Private Function GET_HEAD_DOWN_ITEM_NO()

    Dim s As String
    Dim y As Integer = 1

    For i = 0 To BODY_LAYOUT_PANEL.Controls.Count - 1
        s = BODY_LAYOUT_PANEL.Controls.Item(i).Text

        If s.Contains("Head down") Then
            y = y + 1
        End If
    Next

    Return (y)
End Function
End Class

对于重新排列部分,我必须为动态添加的组框创建一个拖动事件。你是怎么写的?我该如何指定要处理的内容?

所以问题是:如何将鼠标事件挂钩到动态添加的组框控件?

Private Sub BODY_REARRANGE_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles

如果对我的示例代码有任何意见,我可以使用一些指针!

0 个答案:

没有答案