Wie betätige ich "buttons" mit Tastaturbefehlen? Wie erstelle ich Grenzen?

  • VB.NET

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

    Wie betätige ich "buttons" mit Tastaturbefehlen? Wie erstelle ich Grenzen?

    Hi!
    In einem Projekt möchte ich ein Objekt über buttons, die ticker aktivieren, in Bewegung setzen...
    Klappt alles super: button klicken und schon geht's los :thumbsup: ... Jetzt möchte ich aber nicht den button KLICKEN sondern beispielsweise über W A S D beeinflussen... 8|

    Also: Wie beeinflusse ich buttons über die Tastatur? Und wie bewege ich Objekte ohne buttons und tickers?
    Jetzt zu meiner zweiten Frage: Wie kann ich Bereiche als Blocker (sie sollen das Objekt stoppen, sobald es dagegen stößt) oder als Bereiche, die sobald das Objekt sie betritt, eine MsgBox("Error") erscheinen lassen, festlegen?

    Vielen Dank im Voraus und schöne Grüße von einem Visual Basic Anfänger ;)
    SchuLLes
    Danke erstmal und coole homepage!
    Jetzt habe ich gleich noch eine Frage:
    Würde es klappen, wenn ich schriebe:

    If PictureBox1.Location.Y -30 Then Timer1.Stop ()

    ???

    Oder z.B.:

    If PictureBox1.Location.Y -30 Then
    Timer1.Stop ()
    MsgBox("You failed")

    ???

    Oder funktioniert es anders?

    MFG Schulles

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

    Danke, ja müsste eigendlich wenn z.b. der Code so aussieht:

    VB.NET-Quellcode

    1. If e.KeyCode = Keys.Up Then
    2. PictureBox1.Top = PictureBox1.Top + 50
    3. End If
    4. If PictureBox1.Location.Y + 50 Then
    5. MsgBox("You failed")
    6. End If


    PS: Auch wenn der Code ein bisl unlogisch ist, denn wenn mal Pfeiltaste nach oben drückt geht er normaler weise nicht nach unten xd
    Und wie sorge ich dafür, dass das Objekt nicht weiter als bis zu diesem Punkt bewegt werden kann?

    Also

    If PictureBox1.Location = PictureBox1.Location.Y - 100 Then [Kann nicht weiter als bis zu diesem Punkt bewegt werden]?

    Außerdem habe ich das Problem, dass ich nicht weiß, welchen Wert ich hinter der Achse eintragen muss... Y -???
    Ist die Zahl, die auch bei location steht? Ein weiteres Problem ist, dass erst, wenn ich die "W" Taste loslasse die Meldung kommt... bis dahin bewegt sich das Objekt weiter :(

    Edit:
    Oh tut mir leid! Ich habe die Formel an den falschen Platz gesetzt... Jetzt gab's 'nen Private Sub und jetzt kommt gar keine message mehr, egal ob If Location... oder Top... :(

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „SchuLLes“ ()

    Also...:
    Es funktioniert über Timer, welche durch buttons ausgelöst werden (noch eine Frage: funktionieren keycodes wenn buttons im Programm sind? Es hat nie geklappt, wenn welche drin waren). Was ich möchte ist, dass die Timer stoppen, sobald die PictureBoxen einen bestimmten Punkt erreichen und danach auch nicht weiter als bis zu dieser Grenze bewegt werden können... Hier der Code:
    Ach ja, noch etwas wichtiges: Bitte nicht wundern, dass es drei PictureBoxen gibt, ich habe 3 Buttons über die man auswählt, welche PictureBox als "Objekt" benutzt werden soll... Stickfigure, Billardkugel, oder Rakete... lol...

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    3. Timer1.Start()
    4. Timer2.Stop()
    5. Timer3.Stop()
    6. Timer4.Stop()
    7. Timer5.Stop()
    8. Timer6.Stop()
    9. Timer7.Stop()
    10. Timer8.Stop()
    11. End Sub
    12. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    13. Timer2.Start()
    14. Timer1.Stop()
    15. Timer3.Stop()
    16. Timer4.Stop()
    17. Timer5.Stop()
    18. Timer6.Stop()
    19. Timer7.Stop()
    20. Timer8.Stop()
    21. End Sub
    22. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    23. Timer3.Start()
    24. Timer2.Stop()
    25. Timer1.Stop()
    26. Timer4.Stop()
    27. Timer5.Stop()
    28. Timer6.Stop()
    29. Timer7.Stop()
    30. Timer8.Stop()
    31. End Sub
    32. Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    33. Timer4.Start()
    34. Timer2.Stop()
    35. Timer3.Stop()
    36. Timer1.Stop()
    37. Timer5.Stop()
    38. Timer6.Stop()
    39. Timer7.Stop()
    40. Timer1.Stop()
    41. End Sub
    42. Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    43. Timer4.Stop()
    44. Timer2.Stop()
    45. Timer3.Stop()
    46. Timer1.Stop()
    47. Timer5.Stop()
    48. Timer6.Stop()
    49. Timer7.Stop()
    50. Timer8.Stop()
    51. End Sub
    52. Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
    53. PictureBox1.Location = New Point(PictureBox1.Location.X - 20, PictureBox1.Location.Y)
    54. PictureBox2.Location = New Point(PictureBox2.Location.X - 20, PictureBox2.Location.Y)
    55. PictureBox3.Location = New Point(PictureBox3.Location.X - 20, PictureBox3.Location.Y)
    56. End Sub
    57. Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
    58. PictureBox1.Location = New Point(PictureBox1.Location.X + 20, PictureBox1.Location.Y)
    59. PictureBox2.Location = New Point(PictureBox2.Location.X + 20, PictureBox2.Location.Y)
    60. PictureBox3.Location = New Point(PictureBox3.Location.X + 20, PictureBox3.Location.Y)
    61. End Sub
    62. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    63. PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - 20)
    64. PictureBox2.Location = New Point(PictureBox2.Location.X, PictureBox2.Location.Y - 20)
    65. PictureBox3.Location = New Point(PictureBox3.Location.X, PictureBox3.Location.Y - 20)
    66. End Sub
    67. Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    68. PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 20)
    69. PictureBox2.Location = New Point(PictureBox2.Location.X, PictureBox2.Location.Y + 20)
    70. PictureBox3.Location = New Point(PictureBox3.Location.X, PictureBox3.Location.Y + 20)
    71. End Sub
    72. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    73. End Sub
    74. Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
    75. PictureBox1.Visible = False
    76. PictureBox2.Visible = False
    77. PictureBox3.Visible = True
    78. End Sub
    79. Private Sub Man_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Man.Click
    80. PictureBox1.Visible = False
    81. PictureBox2.Visible = True
    82. PictureBox3.Visible = False
    83. End Sub
    84. Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    85. PictureBox1.Visible = True
    86. PictureBox2.Visible = False
    87. PictureBox3.Visible = False
    88. End Sub
    89. Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    90. Timer4.Stop()
    91. Timer2.Stop()
    92. Timer3.Stop()
    93. Timer1.Stop()
    94. Timer5.Start()
    95. Timer6.Stop()
    96. Timer7.Stop()
    97. Timer8.Stop()
    98. End Sub
    99. Private Sub Timer6_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer6.Tick
    100. PictureBox1.Location = New Point(PictureBox1.Location.X + 20, PictureBox1.Location.Y + 20)
    101. PictureBox2.Location = New Point(PictureBox2.Location.X + 20, PictureBox2.Location.Y + 20)
    102. PictureBox3.Location = New Point(PictureBox3.Location.X + 20, PictureBox3.Location.Y + 20)
    103. End Sub
    104. Private Sub Timer7_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer7.Tick
    105. PictureBox1.Location = New Point(PictureBox1.Location.X - 20, PictureBox1.Location.Y - 20)
    106. PictureBox2.Location = New Point(PictureBox2.Location.X - 20, PictureBox2.Location.Y - 20)
    107. PictureBox3.Location = New Point(PictureBox3.Location.X - 20, PictureBox3.Location.Y - 20)
    108. End Sub
    109. Private Sub Timer8_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer8.Tick
    110. PictureBox1.Location = New Point(PictureBox1.Location.X + 20, PictureBox1.Location.Y - 20)
    111. PictureBox2.Location = New Point(PictureBox2.Location.X + 20, PictureBox2.Location.Y - 20)
    112. PictureBox3.Location = New Point(PictureBox3.Location.X + 20, PictureBox3.Location.Y - 20)
    113. End Sub
    114. Private Sub Timer5_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer5.Tick
    115. PictureBox1.Location = New Point(PictureBox1.Location.X - 20, PictureBox1.Location.Y + 20)
    116. PictureBox2.Location = New Point(PictureBox2.Location.X - 20, PictureBox2.Location.Y + 20)
    117. PictureBox3.Location = New Point(PictureBox3.Location.X - 20, PictureBox3.Location.Y + 20)
    118. End Sub
    119. Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
    120. Timer4.Stop()
    121. Timer2.Stop()
    122. Timer3.Stop()
    123. Timer1.Stop()
    124. Timer5.Stop()
    125. Timer6.Start()
    126. Timer7.Stop()
    127. Timer8.Stop()
    128. End Sub
    129. Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
    130. Timer4.Stop()
    131. Timer2.Stop()
    132. Timer3.Stop()
    133. Timer1.Stop()
    134. Timer5.Stop()
    135. Timer6.Stop()
    136. Timer7.Start()
    137. Timer8.Stop()
    138. End Sub
    139. Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
    140. Timer4.Stop()
    141. Timer2.Stop()
    142. Timer3.Stop()
    143. Timer1.Stop()
    144. Timer5.Stop()
    145. Timer6.Stop()
    146. Timer7.Stop()
    147. Timer8.Start()
    148. End Sub
    149. End Class

    Bin jetzt erstmal weg, komme so gegen 21:00 mal wieder schauen^^
    Danke für deine Hilfe und schöne Grüße
    Schulles

    (Im Anhang ein Bild des Programms)
    Bilder
    • picture01.JPG

      27,25 kB, 748×563, 127 mal angesehen
    Also was du genau willst habe ich verstanden und es müsste so bei dir gehen:

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    3. Timer1.Start()
    4. End Sub
    5. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    6. PictureBox1.Top = PictureBox1.Top + 10
    7. Button1.Enabled = False
    8. End Sub
    9. Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    10. If e.KeyCode = Keys.Up Then
    11. Timer1.Stop()
    12. Button1.Enabled = True
    13. End If
    14. End Sub
    15. End Class


    Natürlich ist as jetzt nur ein beispiel ^^
    Ne sry, ist nicht was ich suche... :(
    Was ich suche ist das hier: Wenn Objekt Location X/Y +/- ? dann automatisch stopp: Objekt anhalten und keinen Schritt in die Richtung weiter (also eine Grenze, 'ne Mauer, Blocker...) 8o
    Falls du mir da nicht helfen kannst auch nicht schlimm, warst mir ja eine große Hilfe mit e.keycode! :thumbsup:
    Danke nochmal und schöne Grüße

    SchuLLes 8|
    Ohh hab den falschen Code gepostet ich meine:

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    3. Timer1.Start()
    4. End Sub
    5. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    6. PictureBox1.Top = PictureBox1.Top + 10
    7. Button1.Enabled = False
    8. End Sub
    9. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10. timer2.start()
    11. End Sub
    12. Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    13. If PictureBox1.Location.Y = 150 Then
    14. Button1.Enabled = True
    15. Timer1.Stop()
    16. End If
    17. End Sub
    18. End Class


    ist halt nicht so schön der Code funz aber

    Hoffe du meinst das!

    PS: Es gibt aber immer verschiedene möglichkeiten z.b. dies:

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    3. Timer1.Start()
    4. End Sub
    5. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    6. Button1.Enabled = False
    7. PictureBox1.Top = PictureBox1.Top + 10
    8. End Sub
    9. Private Sub PictureBox1_Move(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Move
    10. If PictureBox1.Location.Y = 150 Then
    11. Button1.Enabled = True
    12. Timer1.Stop()
    13. End If
    14. End Sub
    15. End Class

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

    Ja bei diesem Code kommt es auch an wo genau deine Picturebox steht, denn wenn sie unter der 150 ist wird sie logischerweise auch nie die 150 erreichen und somit nie stehen bleiben und da ich ja 10 schritte angegeben habe kannst du nicht anfangen von z.b. 83 zu zählen das er nicht am ende 150 sondern 153 erreicht und somit einfach weiter läuft!
    OMG! OMG! OMG! OMG! :D :D :D :D :D :D
    Du Genie ich Idiot! :P
    Na klar!!! Wenn ich mit +20 oder -20 das Objekt bewege ist es kein Wunder, wenn es nicht bei 53 stehen bleibt, wenn es bei 22 anfängt! :pinch: :sleeping: :wacko: :whistling:
    Ich Idiot! Mann.. wie geil... es läuft :thumbsup:
    Vielen Dank für deine Hilfe und Ausdauer!!! ;)

    Lol... Smiley Wahnsinn

    MFG SchuLLes
    Hallo SchuLLes,

    anscheinend fängst du gleich als Anfänger mit einem Spiel an.

    Spiele mit VB sind "höhere Gewalten", selbst Profis sind da nicht so gut. Auch wenn du schnell lernst und schon recht gut bist, würde ich dir davon abraten, hier ein zweites "Counter-Strike" zu programmieren! (Und eigentlich sind das auch keine Grundlagen mehr, das gehört eher ins Hauptforum oder zu Multimedia-/Spielprogrammmierung... ;) )
    Als Anfänger sollte man zunächst kleinere Projekte machen, und dann sich immer wieder steigern.

    Sieh das jetzt nicht als Kritik, sondern als Vorschlag.

    Weiterhin gutes Gelingen,
    bruno1998
    Gut, danke für den Tipp! ;)
    Glaub' mir: ich hatte wirklich nicht vor, ein riesen Programm zu erstellen^^ Es sollte mal ein kleines Projekt werden, das nicht nur aus buttons und textboxen besteht. :D Jedoch merke ich: Es ist noch zu viel. Es fehlt an vielem und ich sollte wirklich lieber mit etwas einfacherem beginnen...

    Schöne Grüße

    SchuLLes 8|

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