PDA

View Full Version : Don't exit if there was no button clicked



austenr
02-08-2010, 04:54 PM
What would you have to write if no one clicked a button on a form to not allow them to exit the form? I've got this but it doesnt work:



Public Sub btnAirplane_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAirplane.Click
Dim AirplaneCnt As Integer
AirplaneCnt = AirplaneCnt + 1
pbAirplane.Visible = True
pbBall.Visible = False
pbDoll.Visible = False
End Sub

Public Sub btnBall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBall.Click
Dim BallCnt As Integer
BallCnt = BallCnt + 1
pbBall.Visible = True
pbAirplane.Visible = False
pbDoll.Visible = False
End Sub

Public Sub btnDoll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDoll.Click
Dim DollCnt As Integer
DollCnt = DollCnt + 1
pbDoll.Visible = True
pbAirplane.Visible = False
pbBall.Visible = False
End Sub

Public Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Dim BallCnt, AirplaneCnt, DollCnt As Integer
If BallCnt < 0 Or AirplaneCnt < 0 Or DollCnt < 0 Then
MsgBox("You cannot exit without selecting any gifts!!")
Else
Me.Close()
End If
End Sub

mbarron
02-08-2010, 09:09 PM
These "Or"s should be "And"s, otherwise you are checking to make sure all 3 buttons were clicked.

If BallCnt < 0 Or AirplaneCnt < 0 Or DollCnt < 0 Then

and

Shouldn't they be =0 instead of checking to see if the values are less than 0

austenr
02-08-2010, 09:21 PM
Thanks I figured it out and went another way by hiding the Exit button until one of the buttons is clicked. Thanks for the reply.

mikerickson
02-09-2010, 08:25 AM
This will prevent the corner X from closing a userform
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Cancel = (CloseMode = 0)
If Cancel Then MsgBox "You must use a button to exit the form."
End Sub