Re-Draw form

Hi Guys,

I have a form with a number of Pictureboxes, that can be moved around. What I'd like to have is a button that with "Re-draw" the form to bring it back to it's initial state, ie. all controls where they originally were.

I could just set the top and left properties of each control to what they originally were, but I was hoping there was an easier way?

Cheers,

Wayne
LVL 48
Wayne Taylor (webtubbs)Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
That's about how to do it....

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private startX, startY As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is PictureBox Then
                AddHandler ctl.MouseDown, AddressOf Me.pb_MouseDown
                AddHandler ctl.MouseMove, AddressOf Me.pb_MouseMove
                ctl.Tag = ctl.Location
            End If
        Next
    End Sub

    Private Sub pb_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = MouseButtons.Left Then
            startX = e.X
            startY = e.Y
        End If
    End Sub

    Private Sub pb_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = MouseButtons.Left Then
            Dim pb As PictureBox = CType(sender, PictureBox)
            pb.Left = pb.Left + (e.X - startX)
            pb.Top = pb.Top + (e.Y - startY)
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is PictureBox AndAlso Not IsNothing(ctl.Tag) Then
                ctl.Location = ctl.Tag
            End If
        Next
    End Sub

End Class

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wayne Taylor (webtubbs)Author Commented:
Yeah, thought as much.

Cheers Mike!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.