2 Attachment(s)
TableLayoutPanel and Drag and Drop
Attachment 187885
I have thi TableLayoutPanel and 4 colored picturebox. In runtime user can drag each picturebox and drop on a cell of the tablelayoutpanel, so once dropped the picturebox should be docked to the cell. Something like second picture:
Attachment 187886
I tryed this library https://www.codeproject.com/Tips/178...rms-Controls-2 to make controls draggable, but I can't really drop them with it.
Any advice?
Re: TableLayoutPanel and Drag and Drop
I've only had a quick look and not done any testing but i would think that you'd want to handle the MouseUp event of the control(s) and check whether the mouse cursor position is within a cell of the TLP and, if it is, add it to that cell. It's up to you to decide what to do if the cell is already occupied. You might want to return the control to its original position or you might want to drop it in the next empty cell or something else.
Re: TableLayoutPanel and Drag and Drop
Here is a quick and dirty example of dragging and dropping panels into a TableLayoutPanel:
Code:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim tlpContainer = New TableLayoutPanel() With {
.AllowDrop = True,
.BorderStyle = BorderStyle.FixedSingle,
.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset,
.ColumnCount = 2,
.Location = New Point(5, 5),
.RowCount = 2,
.Size = New Size(300, 300)
}
tlpContainer.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 50))
tlpContainer.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 50))
tlpContainer.RowStyles.Add(New RowStyle(SizeType.Percent, 50))
tlpContainer.RowStyles.Add(New RowStyle(SizeType.Percent, 50))
AddHandler tlpContainer.DragEnter, AddressOf TableLayoutPanel_DragEnter
AddHandler tlpContainer.DragDrop, AddressOf TableLayoutPanel_DragDrop
Controls.Add(tlpContainer)
Dim colors = {Color.Yellow, Color.Blue, Color.Red, Color.Green}
For index = 0 To colors.Length - 1
Dim panel = New Panel() With {
.BackColor = colors(index),
.Location = New Point(tlpContainer.Right + 15, tlpContainer.Top + (index * 55)),
.Size = New Size(50, 50)
}
AddHandler panel.MouseDown, AddressOf Panel_MouseDown
Controls.Add(panel)
Next
End Sub
Private Sub Panel_MouseDown(sender As Object, e As MouseEventArgs)
If (e.Button = MouseButtons.Left) Then
Dim panel = DirectCast(sender, Panel)
panel.DoDragDrop(panel, DragDropEffects.Move)
End If
End Sub
Private Sub TableLayoutPanel_DragEnter(sender As Object, e As DragEventArgs)
If (e.Data.GetDataPresent(GetType(Panel))) Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub TableLayoutPanel_DragDrop(sender As Object, e As DragEventArgs)
If (e.Data.GetDataPresent(GetType(Panel))) Then
Dim panel = DirectCast(e.Data.GetData(GetType(Panel)), Panel)
Dim tlpContainer = DirectCast(sender, TableLayoutPanel)
panel.Dock = DockStyle.Fill
tlpContainer.Controls.Add(panel)
End If
End Sub
End Class
But I don't think this is exactly what you want in that it is not specifying which cell to drop the control in.