我現在正在完成一個最終專案,但我的輸入框出現了一些問題。我正在創建一個數學棋盤游戲。每次玩家(圖片框)與標簽相交時,都會出現一個帶有數學問題的輸入框。當我運行我的程式時,一切都很順利,除了當玩家與一個標簽而不是一個標簽相交時會顯示多個輸入框的事實。這是在計時器滴答子下。我有一種感覺,這是一個簡單的解決方法,但我無法弄清楚。我該如何解決?
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Temps = 1
lblTimer.Text = Temps
Dim Réponse As Integer
If PictureBoxJoueur.Bounds.IntersectsWith(lblCase1.Bounds) Then
Réponse = InputBox("Qu'est ce que 4 7 ", "Répond a la question ci-dessous:")
If Réponse = 11 Then
lblScore.Text = 1
MsgBox("Bravo!")
Else
lblScore.Text -= 1
MsgBox("Mauvaise réponse!")
End If
uj5u.com熱心網友回復:
您需要在顯示輸入框之前停止計時器,然后再次啟動它:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Temps = 1
lblTimer.Text = Temps
Dim Réponse As Integer
If PictureBoxJoueur.Bounds.IntersectsWith(lblCase1.Bounds) Then
Timer1.Enabled = False
Réponse = InputBox("Qu'est ce que 4 7 ", "Répond a la question ci-dessous:")
If Réponse = 11 Then
lblScore.Text = 1
MsgBox("Bravo!")
Else
lblScore.Text -= 1
MsgBox("Mauvaise réponse!")
End If
Timer1.Enabled = True
End Sub
uj5u.com熱心網友回復:
如果您希望計時器繼續滴答作響,您可以使用 bool 來表示您是否正在顯示輸入。否則我可能會使用互斥鎖將輸入代碼標記為關鍵,但由于您使用的是在 UI 上運行的計時器,我們不想阻止 UI 執行緒。
Option Strict On
' Namespace, Class declaration
Private Temps As Integer
Private score As Integer
Private isDoingInput As Boolean = False
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Temps = 1
lblTimer.Text = Temps.ToString
If isDoingInput Then Exit Sub
isDoingInput = True
If PictureBoxJoueur.Bounds.IntersectsWith(lblCase1.Bounds) Then
Dim response = InputBox("Qu'est ce que 4 7 ", "Répond a la question ci-dessous:")
Dim Réponse As Integer
If Integer.TryParse(response, Réponse) Then
If Réponse = 11 Then
score = 1
lblScore.Text = score.ToString()
MsgBox("Bravo!")
Else
score -= 1
lblScore.Text = score.ToString()
MsgBox("Mauvaise réponse!")
End If
ResetPictureBoxJoueur()
Else
MsgBox("Not a valid number!")
End If
End If
isDoingInput = False
End Sub
Private Sub resetPictureBoxJoueur()
PictureBoxJoueur.Location = New Point()
End Sub
我還在Option Strict On代碼頂部添加了這樣的內容,以便您可以進行適當的型別轉換(它們被引入到您的代碼中)。這也需要 InputBox 驗證。此外,當輸入完成時,圖片框仍在標簽上,因此我將其重置為 0,0,但您可以根據需要進行處理。
您沒有顯示它,但這是用于拖動的簡單代碼,這使應用程式更加完整。
Private isDragging As Boolean = False
Private currentPosition As New Point()
Private Sub PictureBoxJoueur_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBoxJoueur.MouseDown
isDragging = True
currentPosition = e.Location
End Sub
Private Sub PictureBoxJoueur_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBoxJoueur.MouseUp
isDragging = False
End Sub
Private Sub PictureBoxJoueur_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBoxJoueur.MouseMove
If isDragging Then
PictureBoxJoueur.Top = PictureBoxJoueur.Top e.Y - currentPosition.Y
PictureBoxJoueur.Left = PictureBoxJoueur.Left e.X - currentPosition.X
End If
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/351523.html
標籤:网络
