Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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.
Posted
Comments
ZurdoDev 28-Mar-13 7:25am    
Why is your textbox keydown event firing when you click an Exit button?
Rachna0309 28-Mar-13 7:26am    
It is firing validating event
ZurdoDev 28-Mar-13 7:27am    
OK, just add CausesValidation="false" to your Exit button.
Rachna0309 28-Mar-13 7:38am    
Still doesnt work...
ZurdoDev 28-Mar-13 7:48am    
So, I go back to why is a KeyDown event getting called when a button is clicked. KeyDown is for when the textbox has focus and keys are being pressed on the keyboard. Why is it getting called when you click the Exit button?

1 solution

Beyond setting the CausesValidation 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.
VB
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


Then test for a DialogResult in your validation code
VB
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
 
Share this answer
 
Comments
Rachna0309 28-Mar-13 8:08am    
I changed my code as given by you, but i am still facing the same problem.
Instead of using Validating Event can I put the above code in some function and call that function in keydown event?
F. Xaver 28-Mar-13 9:09am    
why you need KeyDown event?
KeyUp or KeyPress event should also work fine for your problem.
you can cancel inputs, and check if the entered value is ok.
(KeyPress don't fire on delete or backspace)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900