daveofgv
Well-known member
Hi Everyone!
I have a flowlayoutpanel and the controls inside can be dragged and dropped. They are panels with textbox and label on each panel (roughly 40 panels or so) The problem is that when the user closes the form (or application) I need the controls to remain in the same position as they moved them. I have a My.Settings named My.Settings.controllocations and have it set to System.Collection.Specialized.StringConnection (which works for the controls on the form, but not for the controls inside the flowlayoutpanel.
Here is my code right now (which I found some of the code on this site a long time ago - but not sure where - I believe maybe from JohnH, idk):
I do not receive any errors, however, the controls are not saved in the location that were last moved.
Any help would be much appreciated!
Thanks in advanced
daveofgv
I have a flowlayoutpanel and the controls inside can be dragged and dropped. They are panels with textbox and label on each panel (roughly 40 panels or so) The problem is that when the user closes the form (or application) I need the controls to remain in the same position as they moved them. I have a My.Settings named My.Settings.controllocations and have it set to System.Collection.Specialized.StringConnection (which works for the controls on the form, but not for the controls inside the flowlayoutpanel.
Here is my code right now (which I found some of the code on this site a long time ago - but not sure where - I believe maybe from JohnH, idk):
VB.NET:
Private Sub Form4_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
For Each child As Control In Me.FlowLayoutPanel1.Controls
AddHandler child.MouseDown, AddressOf childs_MouseDown
Next
For Each child As Control In Me.FlowLayoutPanel1.Controls
For Each item In My.Settings.controlLocations
If Split(item, "!")(0) = child.Name Then
child.Location = New Point(Split(item, "!")(1), _
Split(item, "!")(2))
End If
Next
Next
VB.NET:
Private dragcursor As Cursor, dragtype As Type
Private Sub childs_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If Me.ReorderCheckBox.Checked Then
Dim source As Control = CType(sender, Control)
Using bmp As New Bitmap(source.Width, source.Height)
source.DrawToBitmap(bmp, New Rectangle(Point.Empty, source.Size))
Me.dragcursor = New Cursor(bmp.GetHicon)
End Using
Me.dragtype = source.GetType
Me.FlowLayoutPanel1.DoDragDrop(source, DragDropEffects.Move)
Me.dragcursor.Dispose()
End If
End Sub
VB.NET:
Private Sub FlowLayoutPanel1_GiveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) Handles FlowLayoutPanel1.GiveFeedback
e.UseDefaultCursors = False
Cursor.Current = Me.dragcursor
End Sub
VB.NET:
Private Sub FlowLayoutPanel1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragEnter
If e.AllowedEffect = DragDropEffects.Move AndAlso e.Data.GetDataPresent(dragtype) Then
e.Effect = DragDropEffects.Move
Me.Refresh()
End If
End Sub
VB.NET:
Private Sub FlowLayoutPanel1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragDrop
Dim source As Control = CType(e.Data.GetData(dragtype), Control)
Dim target As Control = Me.FlowLayoutPanel1.GetChildAtPoint(Me.FlowLayoutPanel1.PointToClient(New Point(e.X, e.Y)))
If target IsNot Nothing Then
Dim ix As Integer = Me.FlowLayoutPanel1.Controls.GetChildIndex(target)
Me.FlowLayoutPanel1.Controls.SetChildIndex(source, ix)
My.Settings.controlLocations.Clear()
For Each Control As Control In Me.FlowLayoutPanel1.Controls
My.Settings.controlLocations.Add(Control.Name & "!" _
& Control.Location.X & "!" & Control.Location.Y)
Next
My.Settings.Save()
End If
End Sub
VB.NET:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
My.Settings.controlLocations.Clear()
For Each Control As Control In Me.FlowLayoutPanel1.Controls
My.Settings.controlLocations.Add(Control.Name & "!" _
& Control.Location.X & "!" & Control.Location.Y)
Next
My.Settings.Save()
End Sub
End Class
I do not receive any errors, however, the controls are not saved in the location that were last moved.
Any help would be much appreciated!
Thanks in advanced
daveofgv