16 Replies - 733 Views - Last Post: 02 February 2013 - 04:21 PM
#1
Advanced - TableLayoutPanel Drag and Drop
Posted 18 January 2013 - 07:13 PM
Thanks in Advance!
JeyT
Replies To: Advanced - TableLayoutPanel Drag and Drop
#2
Re: Advanced - TableLayoutPanel Drag and Drop
Posted 18 January 2013 - 07:39 PM
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
#3
Re: Advanced - TableLayoutPanel Drag and Drop
Posted 19 January 2013 - 05:55 AM
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
#4
Re: Advanced - TableLayoutPanel Drag and Drop
Posted 19 January 2013 - 06:39 AM
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
#5
Re: Advanced - TableLayoutPanel Drag and Drop
Posted 19 January 2013 - 05:10 PM
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!
#6
Re: Advanced - TableLayoutPanel Drag and Drop
Posted 19 January 2013 - 06:58 PM
Quote
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!
#7
Re: Advanced - TableLayoutPanel Drag and Drop
Posted 19 January 2013 - 07:09 PM
[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..
#8
Re: Advanced - TableLayoutPanel Drag and Drop
Posted 19 January 2013 - 07:11 PM
#9
Re: Advanced - TableLayoutPanel Drag and Drop
Posted 19 January 2013 - 07:22 PM
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!
#10
Re: Advanced - TableLayoutPanel Drag and Drop
Posted 19 January 2013 - 07:24 PM
#11
Re: Advanced - TableLayoutPanel Drag and Drop
Posted 20 January 2013 - 10:41 AM
‘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???
#12
Re: Advanced - TableLayoutPanel Drag and Drop
Posted 20 January 2013 - 11:15 AM
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.
#13
Re: Advanced - TableLayoutPanel Drag and Drop
Posted 02 February 2013 - 06:07 AM
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!
#14
Re: Advanced - TableLayoutPanel Drag and Drop
Posted 02 February 2013 - 08:29 AM
#15
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?
|
|

New Topic/Question
Reply



MultiQuote




|