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

Re: My form needs to move with my arrowkeys in VB.2010 80 80

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
Re: My form needs to move with my arrowkeys in VB.2010 80 80

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

Re: My form needs to move with my arrowkeys in VB.2010 80 80

Do you mean a form from the same application?

Re: My form needs to move with my arrowkeys in VB.2010 80 80

yes

Re: My form needs to move with my arrowkeys in VB.2010 80 80

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
Re: My form needs to move with my arrowkeys in VB.2010 80 80

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

Re: My form needs to move with my arrowkeys in VB.2010 80 80

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 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.