Results 1 to 16 of 16

Thread: [RESOLVED] VB.NET condensed code to allow moving multiple controls on a form.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    Resolved [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

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    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

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    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.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    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.

  6. #6
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,388

    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

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    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

  8. #8
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    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.

  10. #10
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    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!

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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…

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    Re: VB.NET condensed code to allow moving multiple controls on a form.

    Quote Originally Posted by .paul. View Post
    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)
    Last edited by mikeg71; May 1st, 2024 at 11:31 AM.

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: VB.NET condensed code to allow moving multiple controls on a form.

    Quote Originally Posted by mikeg71 View Post
    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…

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    Re: VB.NET condensed code to allow moving multiple controls on a form.

    Thanks a bunch .paul.

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