Reputation: 31
I have a form with some questions on it and MaskedTextBoxes for user input. I have that validation for possible entries.
With the question/text boxes, the user can put something into the textbox, but can, of course, enter nothing and click the 'Next' button. This results in an error for which I display an error dialog box. This happens when the focus is lost on the textbox, which tells the user they must enter up to 5 characters, however, because I have this being trapped by a focuslost event, if the user just clicks the 'Cancel' button to exit, the focuslost event kicks in first and displays the error dialog box, rather than just exiting. I need to figure out a way to trap the button click before the focuslost.
Now I know this is not possible, so am really looking for a different way to allow the cancel button to be clicked without the focuslost event trapping it as an error.
The code for the focuslost is below. Of course it always runs this first, before getting to the 'Cancel' button click code block.
function tb1validation_focuslost {
$script:ans1 = $tb1.Text.tostring()
if ($ans1 -le 0 -xor $ans1 -gt 365){
$f2.controls.AddRange(@($e1,$ok2))
$f1.topmost = $False
$f2.Topmost = $True
$e1.text = "Error - Out of range here."
[void]$f2.ShowDialog()
$tb1.focus()
}
elseif ($ans1 -gt 0 -and $ans1 -le 365) {
# *** Turn on second question ***
$script:now = [datetime]::Today.AddDays(-$ans1)
$script:q1done = 1
$tb1.Enabled = $false
$q2.visible = $True
$tb2.visible = $True
$tb2.focus()
}
}
$tb1.add_leave({tb1validation_focuslost})
Not sure the code is going to help. I just need advice on how to allow a cancel button to be clicked and then the form closes without any error.
Upvotes: 1
Views: 38