[RESOLVED] VB.NET condensed code to allow moving multiple controls on a form.
Hi All - At the moment, I am working with 22 table layout panels on my form. I am now trying to find a way (if there is one) to allow moving controls on my form with less code. I am using the below code for the panel to allow moving and then saving the new location. Is there a function for this to allow moving any of the panels and saving each one to my.settings without creating this much code for a single panel?
Code:
Private allowTLPmove As Boolean = False
Private newTLPpoint As New Point
Private Sub TableLayoutPanel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TableLayoutPanel1.MouseDown
allowTLPmove = True
newTLPpoint = New Point(e.X, e.Y)
Me.Cursor = Cursors.SizeAll
End Sub
Private Sub TableLayoutPanel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TableLayoutPanel1.MouseMove
If allowTLPmove = True Then
TableLayoutPanel1.Location = New Point(TableLayoutPanel1.Location.X + e.X - newTLPpoint.X, TableLayoutPanel1.Location.Y + e.Y - newTLPpoint.Y)
End If
End Sub
Private Sub TableLayoutPanel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TableLayoutPanel1.MouseUp
allowTLPmove = False
Me.Cursor = Cursors.Default
My.Settings.TblPanel1 = TableLayoutPanel1.Location
My.Settings.Save()
End Sub
Re: VB.NET condensed code to allow moving multiple controls on a form.
One thing that comes to mind is drag and drop, but I don't think you'd find that as visually appealing as what you are doing. You can drag and drop a control, which the form itself would handle. That would mean that the amount of code would be less, but I believe that the control, while being dragged, would remain just an icon, which wouldn't look nearly as good as moving the table the way you are. I may be wrong about that, though, as my use of drag and drop is kind of specialized.
Re: VB.NET condensed code to allow moving multiple controls on a form.
@Shaggy Hiker - Considering your status, now I am real nervous if you don't have an answer for this one. So this code seems to work great for my table layout panel, I was just hoping for a shorter way to handle all 22 of them without lots and lots of copy/paste & rename :)
Re: VB.NET condensed code to allow moving multiple controls on a form.
If the code for the TLP's then what about assigning all of them to the same event?
Code:
Private Sub TableLayoutPanel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TableLayoutPanel1.MouseDown,TableLayoutPanel2.MouseDown,TableLayoutPanel3.MouseDown,TableLayoutPanel4.MouseDown
allowTLPmove = True
newTLPpoint = New Point(e.X, e.Y)
Me.Cursor = Cursors.SizeAll
End Sub
btw - It would probably be good idea to give the TLP's more meaningful names.
Re: VB.NET condensed code to allow moving multiple controls on a form.
Thanks @wes4dbt I will give this a try. You are 100% on the name, I am planning to create better names once I had this solved.
Re: VB.NET condensed code to allow moving multiple controls on a form.
look into addhandler
also include an if in your MouseUp so you only save when really moving, the mouse down could have happened on another control.
Code:
If allowTLPmove Then
allowTLPmove = False
Me.Cursor = Cursors.Default
My.Settings.TblPanel1 = TableLayoutPanel1.Location
My.Settings.Save()
End If
Re: VB.NET condensed code to allow moving multiple controls on a form.
Thanks for the info on that. I added the IF and also created a test form, but I am just getting strange results, so I don't know where I am going wrong with this.
Code:
Public Class Form1
Private allowTLPmove As Boolean = False
Private newTLPpoint As New Point
Private Sub TableLayoutPanel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TableLayoutPanel1.MouseDown, TableLayoutPanel2.MouseDown, TableLayoutPanel3.MouseDown
allowTLPmove = True
newTLPpoint = New Point(e.X, e.Y)
Me.Cursor = Cursors.SizeAll
End Sub
Private Sub TableLayoutPanel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TableLayoutPanel1.MouseMove
If allowTLPmove = True Then
TableLayoutPanel1.Location = New Point(TableLayoutPanel1.Location.X + e.X - newTLPpoint.X, TableLayoutPanel1.Location.Y + e.Y - newTLPpoint.Y)
End If
End Sub
Private Sub TableLayoutPanel2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TableLayoutPanel2.MouseMove
If allowTLPmove = True Then
TableLayoutPanel2.Location = New Point(TableLayoutPanel2.Location.X + e.X - newTLPpoint.X, TableLayoutPanel2.Location.Y + e.Y - newTLPpoint.Y)
End If
End Sub
Private Sub TableLayoutPanel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TableLayoutPanel1.MouseUp, TableLayoutPanel2.MouseUp, TableLayoutPanel3.MouseUp
If allowTLPmove Then
allowTLPmove = False
Me.Cursor = Cursors.Default
My.Settings.TblPanel1 = TableLayoutPanel1.Location
My.Settings.TblPanel2 = TableLayoutPanel2.Location
My.Settings.TblPanel3 = TableLayoutPanel3.Location
My.Settings.Save()
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TableLayoutPanel1.Location = My.Settings.TblPanel1
TableLayoutPanel2.Location = My.Settings.TblPanel2
TableLayoutPanel3.Location = My.Settings.TblPanel3
End Sub
End Class
Re: VB.NET condensed code to allow moving multiple controls on a form.
If it helps, this is what I have done with a set of groupboxes in an old app:
Code:
Dim IgotThemouse as String
Private Sub GroupBox1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles GroupBox1.MouseDown
IgotThemouse = "groupbox1"
p.X = e.X
p.Y = e.Y
End Sub
Private Sub GroupBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles GroupBox1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
If IgotThemouse = "groupbox1" Then
GroupBox1.Location = New Point(e.X - p.X + GroupBox1.Location.X, e.Y - p.Y + GroupBox1.Location.Y)
End If
End If
End Sub
Private Sub GroupBox1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles GroupBox1.MouseUp
If IgotThemouse = "groupbox1" Then
GroupBox1.Location = New Point(e.X - p.X + GroupBox1.Location.X, e.Y - p.Y + GroupBox1.Location.Y)
End If
End Sub
Re: VB.NET condensed code to allow moving multiple controls on a form.
Thanks @sapator. How to apply this to multiple controls is where I am getting stuck.
Re: VB.NET condensed code to allow moving multiple controls on a form.
IgotThemouse = "groupbox1". On mouserdown it takes the name of the control, so On mouserdown of let's say GroupBox2 the IgotThemouse = "groupbox2" etc
It might be able to be done in one go but that's another story.
Re: VB.NET condensed code to allow moving multiple controls on a form.
Try this...
Code:
Public Class Form1
Private allowTLPmove As Boolean = False
Private newTLPpoint As New Point
Private Sub TableLayoutPanel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TableLayoutPanel1.MouseDown, TableLayoutPanel2.MouseDown, TableLayoutPanel3.MouseDown
allowTLPmove = True
newTLPpoint = New Point(e.X, e.Y)
Me.Cursor = Cursors.SizeAll
End Sub
Private Sub TableLayoutPanel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TableLayoutPanel1.MouseMove, TableLayoutPanel2.MouseMove, TableLayoutPanel3.MouseMove
If allowTLPmove = True Then
Dim tlp As TableLayoutPanel = DirectCast(sender, TableLayoutPanel)
tlp.Location = New Point(tlp.Location.X + e.X - newTLPpoint.X, tlp.Location.Y + e.Y - newTLPpoint.Y)
End If
End Sub
Private Sub TableLayoutPanel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TableLayoutPanel1.MouseUp, TableLayoutPanel2.MouseUp, TableLayoutPanel3.MouseUp
If allowTLPmove Then
allowTLPmove = False
Me.Cursor = Cursors.Default
My.Settings.TblPanel1 = TableLayoutPanel1.Location
My.Settings.TblPanel2 = TableLayoutPanel2.Location
My.Settings.TblPanel3 = TableLayoutPanel3.Location
My.Settings.Save()
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TableLayoutPanel1.Location = My.Settings.TblPanel1
TableLayoutPanel2.Location = My.Settings.TblPanel2
TableLayoutPanel3.Location = My.Settings.TblPanel3
End Sub
End Class
Re: VB.NET condensed code to allow moving multiple controls on a form.
@.paul. This is looking great! I am going to do more testing with it, but so far it works like I want it to :) Thank You!
Re: VB.NET condensed code to allow moving multiple controls on a form.
No problem. I was going to answer yesterday, but I got distracted. It’ll work with all of your TableLayoutPanels, but do your testing and familiarise yourself with the code…
Re: VB.NET condensed code to allow moving multiple controls on a form.
Quote:
Originally Posted by
.paul.
No problem. I was going to answer yesterday, but I got distracted. It’ll work with all of your TableLayoutPanels, but do your testing and familiarise yourself with the code…
This will be great! One question about the moving of the panels, is there a way to keep the dragging to a new X position only and keep the Y the same? I am curious if I have panels in a row, the same Y coordinate would be great.
**Update: I think I got it? If this is all I need to do.
Code:
tlp.Location = New Point(tlp.Location.X + e.X - newTLPpoint.X, tlp.Location.Y)
Re: VB.NET condensed code to allow moving multiple controls on a form.
Quote:
Originally Posted by
mikeg71
I think I got it? If this is all I need to do.
Code:
tlp.Location = New Point(tlp.Location.X + e.X - newTLPpoint.X, tlp.Location.Y)
You got it… :D
Re: VB.NET condensed code to allow moving multiple controls on a form.