验证控件的事件不允许在输入值之前退出 [英] Validating Event of a Control does not allow to exit until value is entered
问题描述
我有一个表单中有很多控件。我需要验证其中一些我已经编写了验证事件的代码.Below是keydownevent的代码,从中调用验证事件:
I have a form in which there are many controls.I need to validate some of them for which I have written code on validating event.Below is the code of keydownevent from which validating event is called:
Private Sub TxtVnum_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtVnum.KeyDown
If (KeyCode = 13 Or KeyCode = 9) Then TxtVnum_Validating(TxtVnum, New System.ComponentModel.CancelEventArgs((False)))
End Sub
Private Sub TxtVnum_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TxtVnum.Validating
If TxtVnum.Text = "" Then
MsgBox("Enter Receipt Number")
TxtVnum.Focus()
Exit Sub
End If
End Sub
现在如果我在没有输入收据号的情况下按退出按钮,上面的错误会一直持续到输入数字为止。我需要的是如果我按下退出按钮,表格应该关闭。
我应该用哪个事件来完成这个任务?
请帮助我。
Now if I press "Exit" button without entering Receipt Number, the above error keeps on coming until I enter number. What I need is if I press Exit button, form should get closed.
Which event I should use to do this task?
Please help me.
推荐答案
除了将退出按钮的CausesValidation
属性设置为false之外,还要在处理单击的函数中设置表单的DialogResult在您关闭之前退出按钮的事件,例如
Beyond setting theCausesValidation
property of the exit button to false, also set the DialogResult of the form in the function handling the click event of the exit button before you call Close, e.g.
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
DialogResult = Windows.Forms.DialogResult.Cancel
Close()
End Sub
T在验证码中测试DialogResult
Then test for a DialogResult in your validation code
Private Sub TxtVnum_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TxtVnum.Validating
If Me.DialogResult = Windows.Forms.DialogResult.None Then
If TxtVnum.Text = "" Then
MsgBox("Enter Receipt Number")
TxtVnum.Focus()
Exit Sub
End If
End If
End Sub
这篇关于验证控件的事件不允许在输入值之前退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!