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

Recommended Answers

All 7 Replies

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.

commented: thx! +0

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

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

See above. I edited my post over an hour so I didn't see your new post until after I refreshed.

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.