【问题标题】:Position control anywhere in GroupBox for drag and drop将控件放置在 GroupBox 中的任意位置以进行拖放
【发布时间】:2018-07-20 19:24:05
【问题描述】:

我基本上允许用户将控件拖放到组框中,这将动态创建这些控件。我希望在释放光标的任何地方创建控件。我假设我需要在鼠标释放时获取坐标或其他东西,然后以某种方式将其传递给 groupbox.DragDrop 并在这些坐标处动态创建我的控件。我该怎么做呢?我已经有拖拽的代码了,我的控件是动态添加到groupbox的,但是总是添加到groupbox的左上角

Private Sub GroupBox1_Enter(sender As Object, e As DragEventArgs) Handles DragAndDropGroupBox.DragEnter
    e.Effect = DragDropEffects.Copy
    Debug.Print("enter")
End Sub

Private Sub GroupBox_DragDrop(sender As Object, e As DragEventArgs) Handles DragAndDropGroupBox.DragDrop
    Dim obj As dragObject = e.Data.GetData(GetType(dragObject))

    If obj.type.Equals("textbox") Then
        Debug.Print("label")
        Dim textBox As New TextBox
        textBox.Name = "TextBox" + Convert.ToString(rnd.Next)
        DragAndDropGroupBox.Controls.Add(textBox)

    ElseIf obj.type.Equals("logo") Then
    ElseIf obj.type.Equals("qrcode") Then
        Dim qrcode As New PictureBox
        qrcode.Name = "PictureBox" + Convert.ToString(rnd.Next)
        qrcode.Image = My.Resources.qr_code
        DragAndDropGroupBox.Controls.Add(qrcode)
    End If

End Sub


Private Sub idTextBoxDragHandler(sender As Object, e As MouseEventArgs) Handles idTextBox.MouseDown
    Dim width As Integer
    Dim height As Integer
    Dim fontSize As Integer
    Dim truncation As Integer

    Int32.TryParse(idWidth.Text, width)
    Int32.TryParse(idHeight.Text, height)
    Int32.TryParse(idFontSize.Text, fontSize)
    Int32.TryParse(idTruncation.Text, truncation)

    Dim obj As New dragObject With
    {.width = width,
     .height = height,
     .fontSize = fontSize,
     .truncation = truncation,
     .type = "textbox"}

    idTextBox.DoDragDrop(obj, DragDropEffects.Copy)

End Sub

Private Sub nameTextBoxDragHandler(sender As Object, e As MouseEventArgs) Handles nameTextBox.MouseDown
    Dim width As Integer
    Dim height As Integer
    Dim fontSize As Integer
    Dim truncation As Integer

    Int32.TryParse(nameWidth.Text, width)
    Int32.TryParse(nameHeight.Text, height)
    Int32.TryParse(nameFontSize.Text, fontSize)
    Int32.TryParse(nameTruncation.Text, truncation)

    Dim obj As New dragObject With
    {.width = width,
     .height = height,
     .fontSize = fontSize,
     .truncation = truncation,
     .type = "textbox"}

    idTextBox.DoDragDrop(obj, DragDropEffects.Copy)

End Sub

Private Sub descriptionTextBoxDragHandler(sender As Object, e As MouseEventArgs) Handles descriptionTextBox.MouseDown
    Dim width As Integer
    Dim height As Integer
    Dim fontSize As Integer
    Dim truncation As Integer

    Int32.TryParse(descriptionWidth.Text, width)
    Int32.TryParse(descriptionHeight.Text, height)
    Int32.TryParse(descriptionFontSize.Text, fontSize)
    Int32.TryParse(descriptionTruncation.Text, truncation)

    Dim obj As New dragObject With
    {.width = width,
     .height = height,
     .fontSize = fontSize,
     .truncation = truncation,
     .type = "textbox"}

    idTextBox.DoDragDrop(obj, DragDropEffects.Copy)

End Sub

Private Sub pathTextBoxDragHandler(sender As Object, e As MouseEventArgs) Handles pathTextBox.MouseDown
    Dim width As Integer
    Dim height As Integer
    Dim fontSize As Integer
    Dim truncation As Integer

    Int32.TryParse(pathWidth.Text, width)
    Int32.TryParse(pathHeight.Text, height)
    Int32.TryParse(pathFontSize.Text, fontSize)
    Int32.TryParse(pathTruncation.Text, truncation)

    Dim obj As New dragObject With
    {.width = width,
     .height = height,
     .fontSize = fontSize,
     .truncation = truncation,
     .type = "textbox"}

    idTextBox.DoDragDrop(obj, DragDropEffects.Copy)

End Sub

Private Sub logoDragHandler(sender As Object, e As MouseEventArgs) Handles logoDrag.MouseDown
    Dim width As Integer
    Dim height As Integer

    Int32.TryParse(logoWidth.Text, width)
    Int32.TryParse(logoHeight.Text, height)

    Dim obj As New dragObject With
    {.width = width,
     .height = height,
     .type = "logo"}

    idTextBox.DoDragDrop(obj, DragDropEffects.Copy)

End Sub


Private Sub QRCodeDragHandler(sender As Object, e As MouseEventArgs) Handles QRCodeDrag.MouseDown
    Dim width As Integer
    Dim height As Integer

    Int32.TryParse(qrCodeWidth.Text, width)
    Int32.TryParse(qrCodeHeight.Text, height)

    Dim obj As New dragObject With
    {.width = width,
     .height = height,
     .type = "qrcode"}

    QRCodeDrag.DoDragDrop(obj, DragDropEffects.Copy)
End Sub

【问题讨论】:

  • 编辑帖子
  • 我还不知道怎么获取坐标,所以没有设置

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


【解决方案1】:

当你放下你的对象并创建你的控件时,你需要设置它相对于你的 GroupBox 内部坐标的位置:

Dim textBox As New TextBox
textBox.Name = "TextBox" + Convert.ToString(rnd.Next)
textBox.Location = DragAndDropGroupBox.PointToClient(New Point(e.X, e.Y))
DragAndDropGroupBox.Controls.Add(textBox)

【讨论】:

  • PointToClient 是我所缺少的。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-08
  • 2010-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多