Objekt mit Tastatur bewegen

  • VB.NET

Es gibt 16 Antworten in diesem Thema. Der letzte Beitrag () ist von chris17.

    Objekt mit Tastatur bewegen

    Hilfe!
    Bin hier neu, in VB10 Anfänger und hoffe, im richtigen Forum zu sein.
    Mein Problem:
    Ich habe einen Kreis in meiner Form und möchte diesen mit den Pfeiltasten steuern. Solange ich also zB "Pfeil nach links" gedrückt halte, soll sich der Kreis nach links bewegen. Dazu hab ich folgenden Code geschrieben:

    VB.NET-Quellcode

    1. Private Sub Kreis_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    2. If e.KeyCode = Keys.Down Then
    3. kreis.Location = New Point(kreis.Location.X, kreis.Location.Y - 1)
    4. End If
    5. If e.KeyCode = Keys.upThen
    6. kreis.Location = New Point(kreis.Location.X, kreis.Location.Y + 1)
    7. End If
    8. If e.KeyCode = Keys.leftThen
    9. kreis.Location = New Point(kreis.Location.X-1, kreis.Location.Y )
    10. End If
    11. If e.KeyCode = Keys.leftThen
    12. kreis.Location = New Point(kreis.Location.X+1, kreis.Location.Y )
    13. End If
    14. Application.DoEvents()
    15. End Sub


    leider funzt das nicht. Warum? Und wie muss es richtig sein?

    Code-Tags eingefügt. Thread-Tags angepasst (das ist kein VB6!). ~Thunderbolt

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Thunderbolt“ ()

    Moin,

    setze mal KeyPreview der Form auf True.

    Grüße
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:
    Off Topic:

    Bitte Code Tags nutzen:
    Wie füge ich Quellcode korrekt im Forum ein?
    Welche BBCodes (Tags) sind im Forum verfügbar? (Anleitung zur Benutzung)

    Bitte lesen: Tipps für eine höhere Antwort-Quote

    On Topic:

    Dein Code ist grundsätzlich richtig, aber mit Flüchtigkeitsfehlern behaftet.

    Ändere:

    VB.NET-Quellcode

    1. If e.KeyCode = Keys.Down Then
    2. kreis.Location = New Point(kreis.Location.X, kreis.Location.Y - 1)
    3. End If
    zu

    VB.NET-Quellcode

    1. If e.KeyCode = Keys.Down Then
    2. kreis.Location = New Point(kreis.Location.X, kreis.Location.Y + 1)
    3. End If


    Ändere:

    VB.NET-Quellcode

    1. If e.KeyCode = Keys.upThen
    2. kreis.Location = New Point(kreis.Location.X, kreis.Location.Y + 1)
    3. End If
    zu

    VB.NET-Quellcode

    1. If e.KeyCode = Keys.Up Then
    2. kreis.Location = New Point(kreis.Location.X, kreis.Location.Y - 1)
    3. End If


    Ändere:

    VB.NET-Quellcode

    1. If e.KeyCode = Keys.leftThen
    2. kreis.Location = New Point(kreis.Location.X-1, kreis.Location.Y )
    3. End If
    zu

    VB.NET-Quellcode

    1. If e.KeyCode = Keys.Left Then
    2. kreis.Location = New Point(kreis.Location.X-1, kreis.Location.Y )
    3. End If


    Ändere:

    VB.NET-Quellcode

    1. If e.KeyCode = Keys.leftThen
    2. kreis.Location = New Point(kreis.Location.X+1, kreis.Location.Y )
    3. End If
    zu

    VB.NET-Quellcode

    1. If e.KeyCode = Keys.Right Then
    2. kreis.Location = New Point(kreis.Location.X+1, kreis.Location.Y )
    3. End If
    @timonator Jou.
    @chris17 Willkommen im forum. :thumbup:
    Keys.Left => hin und Keys.Left => her hebt sich auf. ;)
    Lass das DoEvents() weg und mach es so:

    VB.NET-Quellcode

    1. Private Sub Kreis_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    2. Select Case e.KeyCode
    3. Case Keys.Up : Kreis.Location = New Point(Kreis.Location.X, Kreis.Location.Y - 1)
    4. Case Keys.Down : Kreis.Location = New Point(Kreis.Location.X, Kreis.Location.Y + 1)
    5. Case Keys.Left : Kreis.Location = New Point(Kreis.Location.X - 1, Kreis.Location.Y)
    6. Case Keys.Right : Kreis.Location = New Point(Kreis.Location.X + 1, Kreis.Location.Y)
    7. End Select
    8. End Sub
    oder so:

    VB.NET-Quellcode

    1. Private Sub Kreis_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    2. Dim dx = 0
    3. Dim dy = 0
    4. Select Case e.KeyCode
    5. Case Keys.Up : dy = -1
    6. Case Keys.Down : dy = 1
    7. Case Keys.Left : dx = -1
    8. Case Keys.Right : dx = 1
    9. End Select
    10. Kreis.Location = New Point(Kreis.Location.X + dx, Kreis.Location.Y + dy)
    11. End Sub

    Beim nächsten Mal setzt Du einen Haltepunkt in die Prozedur und findest den Fehler selbst. Gugst Du hier.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    chris17 schrieb:

    Leider hat alles nicht gefunzt.
    Was genau?
    Mein Code läuft bei mir.
    Hast Du bei der Form KeyPreview = True?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Ist so bei der Form eingestellt. Wenn ich zb statt keys.up keys.w usw programmiere, dann läuft alles, d.h. der Kreis bewegt sich wie gewünscht. Ich finde aber die Steuerung mit diesen Tasten sehr umständlich, deshalb soll mit den Pfeiltasten gesteuert werden. Und das funzt nicht. Folgerung meiner Meinung: keys.up stimmt nicht für die Taste "Pfeil nachoben" usw.

    chris17 schrieb:

    keys.up stimmt nicht für die Taste "Pfeil nachoben" usw.
    Richtig.
    Bei Windows-Controls ist oben-links (0, 0).
    Außerdem kannst Du doch die Richtung Deinen Bedürfnissen anpassen.
    Reagiert das Programm nicht auf Up, Down, Left und Right?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    chris17 schrieb:

    Das Programm reagiert nicht
    Bei mir schon.
    Kannst Du mal Deine Projektmappe nach Bereinigung zippen und anhängen?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    chris17 schrieb:

    hab ich
    nicht dein in meinem Code:

    VB.NET-Quellcode

    1. Private Sub Kreis_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    2. Select Case e.KeyCode
    3. Case Keys.D : Kreis.Location = New Point(Kreis.Location.X + 1, Kreis.Location.Y)
    4. Case Keys.W : Kreis.Location = New Point(Kreis.Location.X, Kreis.Location.Y - 1)
    5. Case Keys.A : Kreis.Location = New Point(Kreis.Location.X - 1, Kreis.Location.Y)
    6. Case Keys.S : Kreis.Location = New Point(Kreis.Location.X, Kreis.Location.Y + 1)
    7. Case Keys.Up : Kreis.Location = New Point(Kreis.Location.X, Kreis.Location.Y - 1)
    8. Case Keys.Down : Kreis.Location = New Point(Kreis.Location.X, Kreis.Location.Y + 1)
    9. Case Keys.Left : Kreis.Location = New Point(Kreis.Location.X - 1, Kreis.Location.Y)
    10. Case Keys.Right : Kreis.Location = New Point(Kreis.Location.X + 1, Kreis.Location.Y)
    11. End Select
    12. End Sub
    und es läuft
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    und noch bischen hübschen:

    VB.NET-Quellcode

    1. Private Sub Kreis_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyDown
    2. Dim pt = Kreis.Location
    3. Select Case e.KeyCode
    4. Case Keys.D, Keys.Right : pt.X += 1
    5. Case Keys.W, Keys.Up : pt.Y -= 1
    6. Case Keys.A, Keys.Left : pt.X -= 1
    7. Case Keys.S, Keys.Right : pt.X += 1
    8. Case Else : Return
    9. End Select
    10. Kreis.Location = pt
    11. End Sub

    ErfinderDesRades schrieb:

    und noch bischen hübschen:
    Siehe Post #6.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!