PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2019 [RESOLVED] How to force the current TextBox to validate when closing the form.-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] How to force the current TextBox to validate when closing the form.

  1. #1

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    870

    Resolved [RESOLVED] How to force the current TextBox to validate when closing the form.

    I have a settings form that tried to save integer values to My.Settings when the field is validating. It's my new way around the fact one can't bind a TextBox.Text to an integer value setting. Also I need the items to be saved because there are test buttons and I like to use the actual values stored in My.Settings. It's nice because I have one sub that handles the Validating even for all TextBoxes that contain integers.
    But I found a problem, I don't have a button to close the form but instead rely on the form's close button. But this doesn't fire validation on the TextBox currently being edited. It results in the setting not being saved.
    I know I can always go back to my old way and use the form's closing event to cast all the integer TextBoxes to the My.Settings, but I was wondering if anyone knew of a way to cause that last text box to validate when the form close button is clicked and prevent it from finishing if it doesn't validate.

  2. #2
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,004

    Re: How to force the current TextBox to validate when closing the form.

    Cant you just call Validate() in the form closing event? I would think this should validate all controls.

  3. #3

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    870

    Re: How to force the current TextBox to validate when closing the form.

    Great idea. In the form closing event I tried Me.Validate() and Me.ValidateChildren() and it works except it doesn't stop if it fails validation. Any idea how I can cancel?

  4. #4
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,004

    Re: How to force the current TextBox to validate when closing the form.

    Ive not much messed with validation or error providers but i picture it looking close to this
    Code:
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    
            For Each TxBx As TextBox In Controls.OfType(Of TextBox)
                AddHandler TxBx.Validating, Sub(s As Object, ev As System.ComponentModel.CancelEventArgs)
                                                'do validation condition and set
                                                e.Cancel = True
                                                'if something messed up
                                            End Sub
            Next
    
    
        End Sub

  5. #5

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    870

    Re: How to force the current TextBox to validate when closing the form.

    I finally got a chance to read the description of ValidateChildren. I didn't realize it returned a Boolean. Very simple and nice. And since my validation routine highlights the offending text and activates an ErrorProvider, it's obvious to the user what they need to do. All I had to add was this to the FormClosing.
    vb.net Code:
    1. If Not Me.ValidateChildren() Then
    2.     e.Cancel = True
    3. End If
    Thanks for your help.

  6. #6
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,004

    Re: [RESOLVED] How to force the current TextBox to validate when closing the form.

    That makes perfect sense, actually. Glad you worked it out.

  7. #7

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    870

    Re: [RESOLVED] How to force the current TextBox to validate when closing the form.

    Thank you for your help.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width