I am finishing up a final project right now and am having some trouble with my input boxes. I am creating a math board game. Every time the player (picture box) intersects with a label an input box appears with a math question. When I run my program everything goes smoothly except for the fact that multiple input boxes will show when the player intersects with a label instead of just one. This is under a timer tick sub. I have a feeling that it is an easy fix but I cannot figure it out. How do I fix that?
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
CodePudding user response:
You need to stop the timer before showing the inputbox, and then start it again afterwards:
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
CodePudding user response:
If you want the timer to continue ticking you can use a bool to signal whether you are showing the input or not. I may otherwise use a mutex to mark the input code as critical, but since you're using a Timer, that runs on the UI and we don't want to hold up the UI thread.
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
I also added Option Strict On
at the top of the code so you can do proper type conversions (they are introduced into your code). This necessitated InputBox validation as well. Also when the Input is complete, the PictureBox is still on the Label so I reset it to 0,0 but you can handle that as you see fit.
You didn't show it, but this is simple code for dragging, and this makes the app somewhat more complete.
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