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?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
Wayne Taylor (webtubbs)Author Commented:
Yeah, thought as much.

Cheers Mike!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.