675 Aufrufe
Gefragt in Anwendungen(Java,C++...) von
ich hebe ein progremirfehler denn ich nicht finde
mit visual basic 2010

code:

Imports System.ComponentModel

Public Class Form1
Dim jmax As Integer = 10

Dim j As Integer = 2
Dim hallo As String = "s"

Private Sub w_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles w.Click
hallo = "w"
End Sub
Private Sub s_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles s.Click
hallo = "s"
End Sub
Private Sub d_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles d.Click
hallo = "d"
End Sub
Private Sub a_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles a.Click
hallo = "a"
End Sub

Private Sub start_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles start.Click
j = InputBox("geswindichkeit")
If j > jmax Then
Dim h = MsgBox("einen wert gleiner als 10")
j = InputBox("geswindichkeit")
End If
Timer1.Enabled = True

End Sub




Private Sub Buttonstop_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Buttonstop.Click
Timer1.Enabled = False

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick


neu(hallo)
If neu(hallo) = 0 Then
If pbmove.Location.Y < 1 Then
pbmove.Location = New Point(pbmove.Location.X, pbmove.Location.Y +
50)
ElseIf pbmove.Location.Y > 2000 Then
pbmove.Location = New Point(pbmove.Location.X, pbmove.Location.Y -
50)
ElseIf pbmove.Location.X < 1 Then
pbmove.Location = New Point(pbmove.Location.X + 50,
pbmove.Location.Y)
ElseIf pbmove.Location.X > 2500 Then
pbmove.Location = New Point(pbmove.Location.X - 50,
pbmove.Location.Y)
End If
Else

End If


End Sub
Function neu(ByVal hallo As String)
Try
Return 1

If hallo = "s" Then
pbmove.Location = New Point(pbmove.Location.X, pbmove.Location.Y + j)
ElseIf hallo = "d" Then
pbmove.Location = New Point(pbmove.Location.X + j, pbmove.Location.Y)
ElseIf hallo = "w" Then
pbmove.Location = New Point(pbmove.Location.X, pbmove.Location.Y - j)
ElseIf hallo = "a" Then
pbmove.Location = New Point(pbmove.Location.X - j, pbmove.Location.Y)
Else
pbmove.Location = New Point(pbmove.Location.X, pbmove.Location.Y + 2)
End If

Catch ex As Exception
Return 0
End Try


End Function

End Class

1 Antwort

0 Punkte
Beantwortet von repiv Mitglied (272 Punkte)
Hallo,

1. der Timer1 muss zum Starten nicht nur enabled werden sondern auch mit timer1.Start() gestartet werden.
Entsprechend gibt es auch Timer1.Stop().

2. in der Timer1_Tick ist die erste Zeile überflüssig
also der Aufruf neu(hallo) kann weg

3. die Funktion Neu gibt direkt 1 zurück und dann wird der restliche Code nicht durchlaufen.
also return 1 ans Ende der Funktion vor das Catch

Dann sollte es laufen.

Weitere Verbesserungen:
Funktion Neu sollte wie folgt sein:
- Function neu(ByVal hallo As String) As Boolean
also durch "as boolean" true oder false zurückgeben
- für hallo würde ich eine Enum definieren damit kein String Vergleich notwendig ist, z.B.

Dim hallo As Richtung = Richtung.S

Enum Richtung
W
S
D
A
End Enum

Private Sub w_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles w.Click
hallo = Richtung.W
End Sub


Gruß Martin
...