Advanced - TableLayoutPanel Drag and Drop

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 4693 Views - Last Post: 02 February 2013 - 04:21 PM Rate Topic: -----

#1 JeyT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-January 13

Advanced - TableLayoutPanel Drag and Drop

Posted 18 January 2013 - 07:13 PM

Does anyone know if it's possible to set up drag and drop functionality to copy all rows in one tablelayoutpanel to another, when the rows in the first tablelayoutpanel were all created programmatically with rowstyles from a datasource (VB.Net)?? If not, is there a way to copy all the text from the first tablelayoutpanel to the second one with a button?



Thanks in Advance!
JeyT
Is This A Good Question/Topic? 0
  • +

Replies To: Advanced - TableLayoutPanel Drag and Drop

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3729
  • View blog
  • Posts: 13,019
  • Joined: 12-December 12

Re: Advanced - TableLayoutPanel Drag and Drop

Posted 18 January 2013 - 07:39 PM

I believe it is possible to drag most UI control: MSDN

Although, I'm not certain about a TableLayoutPanel. Is it possible to grab it?!

Added: I believe we can because we can Select it.

This post has been edited by andrewsw: 18 January 2013 - 07:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 JeyT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-January 13

Re: Advanced - TableLayoutPanel Drag and Drop

Posted 19 January 2013 - 05:55 AM

Thank you for your reply.

I was able to get the drag part working on the first tablelayoutpanel, with some code from another example, but it still doesn't drop (the text) to the second tablelayoutpanel.

Private Sub TableLayoutPanel2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TableLayoutPanel2.MouseDown
        Me.TableLayoutPanel2.DoDragDrop(Me.TableLayoutPanel2, DragDropEffects.Copy)
    End Sub


    Private Sub TableLayoutPanel1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TableLayoutPanel1.DragDrop

"VB"
        'Define Point
        Dim loc As Point = Me.TableLayoutPanel1.PointToClient(New Point(e.X, e.Y))
        
        'detemine the cell location
        Dim ColumnIndex As Integer = -1
        Dim RowIndex As Integer = -1
        Dim x As Integer = 0
        Dim y As Integer = 0

        While (ColumnIndex <= Me.TableLayoutPanel1.ColumnCount)
            If (loc.X < x) Then
                Exit While
            End If
            ColumnIndex = (ColumnIndex + 1)
            x = (x + Me.TableLayoutPanel1.GetColumnWidths(ColumnIndex))

        End While

        While (RowIndex <= Me.tlpCurrent.RowCount)
            If (loc.Y < y) Then
                Exit While
            End If
            RowIndex = (RowIndex + 1)
            y = (y + Me.TableLayoutPanel1.GetRowHeights(RowIndex))

        End While
        Me.TableLayoutPanel1.Controls.Add(lb, ColumnIndex, RowIndex)
    End Sub


    Private Sub TableLayoutPanel1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TableLayoutPanel1.DragEnter
        e.Effect = DragDropEffects.Copy
    End Sub




Part of the problem is that the cells with text in the tablelayoutpanels, are created programmatically from a database, which is working fine, but I can't figure out how to copy the cells that are created:

tablelayoutpanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.AutoSize, 10.0))
                Dim t As New RichTextBox
                tablelayoutpanel1.Controls.Add(t, 0, r)



This post has been edited by macosxnerd101: 19 January 2013 - 07:10 PM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3729
  • View blog
  • Posts: 13,019
  • Joined: 12-December 12

Re: Advanced - TableLayoutPanel Drag and Drop

Posted 19 January 2013 - 06:39 AM

Please edit your post or, if necessary, copy all to a new post, wrapping your code in code tags - it's very hard to read. It tells you how to do this directly in the area where you are typing your post! Preview your post a few times before posting it.

Make sure the AllowDrop property of the second control is set to True;
Use a MessageBox, or set the text of a label, to check that the second control has recognised the drop.

This post has been edited by andrewsw: 19 January 2013 - 06:42 AM

Was This Post Helpful? 0
  • +
  • -

#5 JeyT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-January 13

Re: Advanced - TableLayoutPanel Drag and Drop

Posted 19 January 2013 - 05:10 PM

Sorry about that - I moved this to :
http://www.dreaminco...d-drop-updated/

The Allow drop is set to true. Great idea about using a label. I'll let you know if that works!
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3729
  • View blog
  • Posts: 13,019
  • Joined: 12-December 12

Re: Advanced - TableLayoutPanel Drag and Drop

Posted 19 January 2013 - 06:58 PM

Quote

Part of the problem is that the cells with text in the tablelayoutpanels, are created programmatically from a database, which is working fine, but I can't figure out how to copy the cells that are created


I don't know what controls your panel contains, or how data from a database has been included, but a TableLayoutPanel has a Controls collection, which I assume you would need to loop (navigate) through to copy data individually.

It is possible to clone the panel (using the ICloneable interface), replacing the 2nd panel, but I'm not sure how this would work with data-sources attached to controls, or even text in textboxes.

Just speculating.. I suppose it would be possible to initially have two versions of the same panel, with one hidden and mirroring and data-changes between them. Then remove the dropped-on panel and replace its location with the hidden version. This sounds a little messy to me.

Not sure exactly what you are trying to achieve but good luck!
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3729
  • View blog
  • Posts: 13,019
  • Joined: 12-December 12

Re: Advanced - TableLayoutPanel Drag and Drop

Posted 19 January 2013 - 07:09 PM

Actually, to be honest, it sounds strange to me. A TableLayoutPanel is just that: a panel, a container. If you wanted to drag something shouldn't it be one of the controls that it contains, such as a TextBox for its content(?).

[In Microsoft Access, when I drag a form onto another form it does copy it, but its only copying an empty shell (effectively, creating a new instance of the form) whose record-source happens to already be specified.]

But anyway..
Was This Post Helpful? 0
  • +
  • -

#8 JeyT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-January 13

Re: Advanced - TableLayoutPanel Drag and Drop

Posted 19 January 2013 - 07:11 PM

Thanks for that suggestion. I had thought of that, but the end objective is to be able to COPY all the text in the cells of the TableLayoutPanel, which are created by RichTextBoxes. Your idea would work if it was simply copying the cells, but the text can be revised, so select all to copy, or drag and drop is preferable. ....still trying, but I am relatively new to VB. If I get this resolved, I will post the code. Thanks for your support.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3729
  • View blog
  • Posts: 13,019
  • Joined: 12-December 12

Re: Advanced - TableLayoutPanel Drag and Drop

Posted 19 January 2013 - 07:22 PM

If you are only copy text from rich-textboxes then it should be straight-forward:

RichTextBoxThatOne.Text = RichTextBoxTheOtherOne.Text


If it's adding data to some kind of data-grid then you would need to start a new record and copy the text into the relevant fields/cells.

But anyway, good luck again!
Was This Post Helpful? 0
  • +
  • -

#10 JeyT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-January 13

Re: Advanced - TableLayoutPanel Drag and Drop

Posted 19 January 2013 - 07:24 PM

That's what I thought too, which is why I was trying to use drag and drop instead of a copy button. I think the way the TableLayoutPanels are populated with data are complicating things.
Was This Post Helpful? 0
  • +
  • -

#11 JeyT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-January 13

Re: Advanced - TableLayoutPanel Drag and Drop

Posted 20 January 2013 - 10:41 AM

Just wanted to let you know how I got this to WORK!!!

‘Form Load

           tlp1.AllowDrop = True

           tlp2.AllowDrop = True

 

            AddHandler tlp1.DragEnter, AddressOf tlp1_DragEnter

            AddHandler tlp2.DragDrop, AddressOf tlp2_DragDrop

 

 

Private Sub tlp1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles tlp1.DragDrop, tlp2.DragDrop

        Dim tlp As TableLayoutPanel

        Dim rows(sender.SelectedItems.Count - 1) As TableLayoutPanel

        Dim i As Integer = 0

 

        ' Loop though the SelectedItems collection for the source.

        For Each tlp In sender.SelectedItems

            ' Add the ListViewItem to the array of ListViewItems.

            rows(i) = tlp

            i = i + 1

        Next

        ' Create a DataObject containg the array of ListViewItems.

        sender.DoDragDrop(New DataObject("System.Windows.Forms.TableLayoutPanel()", rows), DragDropEffects.Move)

    End Sub

 

    Private Sub tlp1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles tlp1.DragEnter, tlp2.DragEnter

        ' Check for the custom DataFormat ListViewItem array.

        If e.Data.GetDataPresent("System.Windows.Forms.TableLayoutPanel()") Then

            e.Effect = DragDropEffects.Move

        Else

            e.Effect = DragDropEffects.None

        End If

    End Sub

 

    Private Sub tlp2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles tlp1.DragDrop, tlp2.DragDrop

        Dim tlp As TableLayoutPanel

        Dim rows() As TableLayoutPanel = e.Data.GetData("System.Windows.Forms.TableLayoutPanel()")

        Dim i As Integer = 0

 

        For Each tlp In rows

            ' Add the item to the target list.

            sender.Items.Add(rows(i).Text)

            ' Remove the item from the source list.

            If sender Is tlp1 Then

                tlp2.RowStyles.Remove(tlp2.RowStyles(0))

            Else

                tlp1.RowStyles.Remove(tlp1.RowStyles(0))

            End If

            i = i + 1

        Next

    End Sub




Now I just need to figure out how to SELECT ALL (rows) somehow. Any ideas???
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3729
  • View blog
  • Posts: 13,019
  • Joined: 12-December 12

Re: Advanced - TableLayoutPanel Drag and Drop

Posted 20 January 2013 - 11:15 AM

Select or Paint a nice colour?

    Private Sub TableLayoutPanel1_CellPaint(sender As Object, e As TableLayoutCellPaintEventArgs) Handles TableLayoutPanel1.CellPaint
        If (e.Row = 0 Or e.Row = 2) Then
            Dim g As Graphics = e.Graphics
            Dim r As Rectangle = e.CellBounds
            g.FillRectangle(New SolidBrush(Color.Blue), r)
        End If

    End Sub

This paints alternative rows, and on loading the form, but you can incorporate it in your code.
Was This Post Helpful? 0
  • +
  • -

#13 JeyT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-January 13

Re: Advanced - TableLayoutPanel Drag and Drop

Posted 02 February 2013 - 06:07 AM

It turns out that I was wrong about how to accomplish this drag and drop for dynamically created RichTextBoxes in a TableLayoutPanel. I didn't need any of the standard drag and drop code, all I needed to do was to enable dragging and dropping in the code of the TableLayoutPanel.

The only problem now is that there is no way to control the AllowedEffects (Copy, Move, None), so the drag and drop only Moves the text. Can you help me find a way to COPY only, so the text doesn't MOVE from the original RichTextBox?

VB.NET
TableLayoutPanel.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.AutoSize, 10.0))
                r = r + 1
                Dim b As New RichTextBox
                b.Name = "Name" & i
                b.Text = row.Text
                b.BackColor = Color.Blue
                b.Dock = DockStyle.Fill
                tlpName.Controls.Add(b, 0, r)
                b.EnableAutoDragDrop = True
            Next



Even if I add :
b.AllowedEffect = DragDropEffects.Copy
'OR
b.Effect = DragDropEffects.Copy



...the text still MOVES at runtime. When I change the rows to a TextBox, it can copy, cut, paste and select all, but it won't drag anymore.

Thank You!
Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3729
  • View blog
  • Posts: 13,019
  • Joined: 12-December 12

Re: Advanced - TableLayoutPanel Drag and Drop

Posted 02 February 2013 - 08:29 AM

Hold the Ctrl key as you drag.
Was This Post Helpful? 0
  • +
  • -

#15 JeyT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-January 13

Re: Advanced - TableLayoutPanel Drag and Drop

Posted 02 February 2013 - 09:39 AM

Thank you for your reply. I forgot to mention that. That does work, but I wanted to completely disable moving. Do you know if that's doable?

Thank you for your reply. I forgot to mention that. That does work, but I wanted to completely disable moving. Do you know if that's doable?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2