Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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.
Posted 28-Mar-13 1:11am
Comments
ryanb31 at 28-Mar-13 7:25am
   
Why is your textbox keydown event firing when you click an Exit button?
Rachna0309 at 28-Mar-13 7:26am
   
It is firing validating event
ryanb31 at 28-Mar-13 7:27am
   
OK, just add CausesValidation="false" to your Exit button.
Rachna0309 at 28-Mar-13 7:38am
   
Still doesnt work...
ryanb31 at 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?
Rachna0309 at 28-Mar-13 7:38am
   
What is the solution then?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
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
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
  Permalink  
Comments
Rachna0309 at 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 at 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
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 28 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100