Hello , I was wondering if I could move my form or any other object with my arrowkeys in vb.2010?
I found out how to do this in vb6 but this function can't be used in vb.2010 and can't find the replaced function anywhere.
If anyone can help me , please do it!

thx

Set the form KeyPreview property to true then add the following handler

Private Sub Form1_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp

    Dim inc As Integer = 10

    Select Case e.KeyCode
        Case Keys.Up
            Me.Location = New Point(Me.Location.X, Me.Location.Y - inc)
        Case Keys.Down
            Me.Location = New Point(Me.Location.X, Me.Location.Y + inc)
        Case Keys.Left
            Me.Location = New Point(Me.Location.X - inc, Me.Location.Y)
        Case Keys.Right
            Me.Location = New Point(Me.Location.X + inc, Me.Location.Y)
    End Select

End Sub

Set inc to whatever increment is appropriate for youtr app.

Edited 4 Years Ago by Reverend Jim

Comments
thx!

And do you also know how to make a form recognize when it touches an other form?
like in if form1 touches form2 then...

The easiest way is to use the IntersectsWith method of the Rectangle object.

    Private Sub Form1_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp

        Dim inc As Integer = 10
        Dim locn As Point

        Select Case e.KeyCode
            Case Keys.Up
                locn = New Point(Me.Location.X, Me.Location.Y - inc)
            Case Keys.Down
                locn = New Point(Me.Location.X, Me.Location.Y + inc)
            Case Keys.Left
                locn = New Point(Me.Location.X - inc, Me.Location.Y)
            Case Keys.Right
                locn = New Point(Me.Location.X + inc, Me.Location.Y)
        End Select

        If Not OverLaps(locn, Me.Size, Form2.Location, Form2.Size) Then
            Me.Location = locn
        End If

    End Sub

    Private Function OverLaps(f1locn As Point, f1size As Size, f2locn As Point, f2size As Size) As Boolean

        Dim rect1 As New Rectangle(f1locn, f1size)
        Dim rect2 As New Rectangle(f2locn, f2size)

        Return rect1.IntersectsWith(rect2)

    End Function

Edited 4 Years Ago by Reverend Jim

well i know i have to calculate the edges of form2 but the problem is i don't know hom an di don't know how to put it in words if my form1 touches form2

This article has been dead for over six months. Start a new discussion instead.