I am using the following to store the X Y cords

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        MyX = Me.Location.X
        MyY = Me.Location.Y
        My.Settings.Save()
    End Sub

but the Me.Location.X or .Y are not detecting the forms position when I move it?

Recommended Answers

All 3 Replies

If you want instant storing of the forms position when you move it, you need to use the MouseDown, MouseMove and MouseUp events.

'First a private member variable
Private bIsMouseDown As Boolean = False

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        'First check to see which mousebutton was pressed
        If e.Button = Windows.Forms.MouseButtons.Left Then
            'Then check to see if the location of the mouse is within the region of the form
            If e.Location.X > Me.Bounds.Left AndAlso e.Location.X < Me.Bounds.Right _
            AndAlso e.Location.Y > Me.Bounds.Top AndAlso e.Location.Y < Me.Bounds.Bottom Then
                bMouseDown = True
            End If
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If bMouseDown Then
            MyX = Me.Location.X
            MyY = Me.Location.Y
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        bMouseDown = False
    End Sub

Wow talk about overcomplicating. If there's one useful thing in programming its keeping it simple. try this instead:

Public Class Form1

    Dim MyX As Integer
    Dim MyY As Integer
    Dim writer As System.IO.StreamWriter

    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        Call Timer1_Tick()
    End Sub

    Private Sub Timer1_Tick()

        'assigning variables
        MyX = Me.Left
        MyY = Me.Top

        'writes to a text file called Co-ords
        writer = New System.IO.StreamWriter("co-ords.txt")

        writer.WriteLine(MyX & Space(3) & MyY)

    End Sub
End Class

Better yet: why not put the code into the form's FormClosing event? It's dreadfully excessive to save the form position every few seconds. Just create user settings called FormX and FormY then put the following 2 event handlers into your form code:

Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    My.Settings.FormX = Me.Left
    My.Settings.FormY = Me.Top
    My.Settings.Save()
End Sub

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Try
        Me.Left = My.Settings.FormX
        Me.Top = My.Settings.FormY
    Catch ex As Exception
        'Valid coordinates not saved in settings
    End Try
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.