none
Tastaturbefehle RRS feed

  • Frage

  •  

    Moin Moin!

    Ich hab da mal ne Frage:

    Ich möchte gerne eine PictureBox mit den Pfeiltasten bewegen.

    Hab aber keinen Plan wie ich das anstellen soll.

    Würd mich sehr freuen wenn mir jemand helfen könnte!

     

    P.s. Ich schreib Visual Basic. 

    Sonntag, 21. Oktober 2007 10:29

Antworten

  • Ich habs rausgefunden:


    Code Snippet

    Private Sub FormKeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

            If e.KeyCode = Keys.Up Then

                PictureBox1.Location = New Point(PictureBox1.Location.X - 0, PictureBox1.Location.Y - 10)

            ElseIf e.KeyCode = Keys.Down Then

                PictureBox1.Location = New Point(PictureBox1.Location.X + 0, PictureBox1.Location.Y + 10)

            ElseIf e.KeyCode = Keys.Right Then

                PictureBox1.Location = New Point(PictureBox1.Location.X + 10, PictureBox1.Location.Y + 0)

            ElseIf e.KeyCode = Keys.Left Then

                PictureBox1.Location = New Point(PictureBox1.Location.X - 10, PictureBox1.Location.Y - 0)


            End If


        End Sub



    Vorher aber bei der Form KeyPreview auf True setzen
    Sonntag, 18. Januar 2009 14:33
  • Es funktioniert auch folgender Code:

            Dim y As Integer = pnlPacmanWASD.Location.Y
            Dim x As Integer = pnlPacmanWASD.Location.X

            If Asc(e.KeyChar) = 119 Then 'w
                e.Handled = True
                pnlPacmanWASD.Location = New Point(x, y - 10)
            End If

            If Asc(e.KeyChar) = 115 Then 's
                e.Handled = True
                pnlPacmanWASD.Location = New Point(x, y + 10)
            End If

            If Asc(e.KeyChar) = 97 Then 'a
                e.Handled = True
                pnlPacmanWASD.Location = New Point(x - 10, y)
            End If

            If Asc(e.KeyChar) = 100 Then 'd
                e.Handled = True
                pnlPacmanWASD.Location = New Point(x + 10, y)
            End If
    Montag, 19. Januar 2009 19:22

Alle Antworten

  •  

    Keyup Ereignisse abfangen - nach schauen welche Taste gedrückt wurde und wenn eine Pfeiltaste gedrückt ist die location Property von der Picture Box verändern.
    Donnerstag, 25. Oktober 2007 18:55
  •  

    Hallo!

    Irgendwie hab ich keine Ahnung was du damit meinst!

    Was sind Keyup ereignisse ,location Property und wie schreib ich das überhaubt?!?!?!

    Wäre über ne Erklärung sehr dankbar!

    Dienstag, 30. Oktober 2007 16:05
  •  

    Keyup ist ein Standardereignis von Formularen und Steuerelementen. Das tritt ein, wenn eine Taste gedrückt wurde und losgelassen wird.

    Quellcodeabschnitt

    Private Sub FormKeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

    If e.KeyCode.Up Then

    Controlname.Location.Y -= 50

    ElseIf e.KeyCode = Keys.Down Then

    Controlname.Location.Y += 50

    End If

    End Sub

     

     

    Wenn die nach Pfeil nach unten Taste gedrückt wird, dann wird das Contol um 50 in der Y achse nach unten verschoben - bei der nach oben Taste um 50 nach oben. Prinzipiell muss is KeyPreview aktiviert sein, dass das Form auch Tastaturereignisse abfängt, wenn untergeordnete Controls den Focus haben und nicht das Form selbst.
    Dienstag, 30. Oktober 2007 16:12
  • Also ich habs so geschrieben

     

    Private Sub FormKeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

    If Keys.Up Then

    PictureBox1.Location.Y -= 50

    ElseIf e.KeyCode = Keys.Down Then

    PictureBox1.Location.Y(+50)

    End If

    End Sub

     

    Das was ich unterstrichen habe zeigt das Programm als Fehler an.

    Bei dem erstem Fehler steht als Fehlerbeschreibung: Der Ausdruck ist ein Wert und kann nicht als Ziel einer Zuweisung verwendet werden.

     

    Bei dem zweitem Fehler steht als Fehlerbeschreibung : Eigenschaftszugriff muss der Eigenschaft zugewiesen werden oder deren Wert verwenden.

    Dienstag, 30. Oktober 2007 17:04
  • Bei mir kommen genau die beiden gleichen Fehler gibts mittlerweile eine Lösung?
    Samstag, 17. Januar 2009 20:15
  • Ich habs rausgefunden:


    Code Snippet

    Private Sub FormKeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

            If e.KeyCode = Keys.Up Then

                PictureBox1.Location = New Point(PictureBox1.Location.X - 0, PictureBox1.Location.Y - 10)

            ElseIf e.KeyCode = Keys.Down Then

                PictureBox1.Location = New Point(PictureBox1.Location.X + 0, PictureBox1.Location.Y + 10)

            ElseIf e.KeyCode = Keys.Right Then

                PictureBox1.Location = New Point(PictureBox1.Location.X + 10, PictureBox1.Location.Y + 0)

            ElseIf e.KeyCode = Keys.Left Then

                PictureBox1.Location = New Point(PictureBox1.Location.X - 10, PictureBox1.Location.Y - 0)


            End If


        End Sub



    Vorher aber bei der Form KeyPreview auf True setzen
    Sonntag, 18. Januar 2009 14:33
  • Es funktioniert auch folgender Code:

            Dim y As Integer = pnlPacmanWASD.Location.Y
            Dim x As Integer = pnlPacmanWASD.Location.X

            If Asc(e.KeyChar) = 119 Then 'w
                e.Handled = True
                pnlPacmanWASD.Location = New Point(x, y - 10)
            End If

            If Asc(e.KeyChar) = 115 Then 's
                e.Handled = True
                pnlPacmanWASD.Location = New Point(x, y + 10)
            End If

            If Asc(e.KeyChar) = 97 Then 'a
                e.Handled = True
                pnlPacmanWASD.Location = New Point(x - 10, y)
            End If

            If Asc(e.KeyChar) = 100 Then 'd
                e.Handled = True
                pnlPacmanWASD.Location = New Point(x + 10, y)
            End If
    Montag, 19. Januar 2009 19:22