首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GroupBox中用于拖放的位置控制

GroupBox中用于拖放的位置控制
EN

Stack Overflow用户
提问于 2018-07-20 19:24:05
回答 1查看 183关注 0票数 0

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-20 19:49:57

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

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51449150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档