Results 1 to 3 of 3

Thread: TableLayoutPanel and Drag and Drop

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2015
    Posts
    174

    TableLayoutPanel and Drag and Drop

    Name:  Capture.jpg
Views: 746
Size:  11.1 KB
    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:
    Name:  Capture2.jpg
Views: 686
Size:  13.3 KB
    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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width