Reputation: 91
I'm basically allowing users to drag and drop controls into a groupbox, which will dynamically create these controls. I want the controls to be created wherever the cursor is released. I'm assuming I need to get the coordinates or something on mouse release, then somehow pass that to the groupbox.DragDrop and dynamically create my control at those coordinates. How would I go about doing this? I already have the code to drag and drop, and my control is dynamically added to the groupbox, however, it is always added to the upper left corner of the group box
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
Upvotes: 0
Views: 279
Reputation: 81620
When you drop your object and create your control, you need to set the location of it relative to your GroupBox interior coordinates:
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)
Upvotes: 1