Results 1 to 7 of 7

Thread: Validation assistance [Resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    127

    Validation assistance [Resolved]

    It may not be tidy but I am trying to fine tune the code below to handle validation.

    Code:
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    
            If e.KeyChar.IsNumber(e.KeyChar) = False Then
                e.Handled = True
            End If
    
        End Sub
    
        Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
    
            If e.KeyChar.IsNumber(e.KeyChar) = False Then
                e.Handled = True
            End If
    It works great at keeping anything but numbers from being entered in either TextBox. I need some suggestions / examples if possible on how to force input in both boxes.

    Example: When my form loads it places focus on TextBox1 and the user must enter a number in TextBox1 and then tab over to TextBox2 and place another number in it before they will be allowed to click a button that runs another function in the form. If they don't put input in both TextBoxes I need a MessageBox to show that reminds them to enter numbers in both TextBoxes before they can click any other buttons on the form.

    As always, much thanks for replies.
    Last edited by teamdad; Jun 13th, 2004 at 08:23 PM.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This is the perfect event to validate user input . It only release focus to next control if the e.Cancel=False . This is field validation type . You need to do the same with other textbox2 .

    VB Code:
    1. Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    2.         If Me.TextBox1.Text = "blah" Then
    3.             e.Cancel = False
    4.         Else
    5.             e.Cancel = True
    6.         End If
    7.     End Sub

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    You could also disable the buttons until both the textboxes have been filled.

    Or, in the button click event, check to see if a value exists in both the textboxes.

    For example

    VB Code:
    1. If TextBox1.Text = "" OR TextBox2.Text = "" Then
    2. MessageBox.Show "Fill up the boxes, MORON!"
    3. Exit Sub
    4. End If
    5.  
    6. 'Rest of your code

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Why not Make all controls invisible except TextBox1 and a btnExit

    In the TextBox1 TextChanged event make a btnConfirmEntry visible and also make TextBox2 invisible (in case you have to make provision for the entries being changed.)

    In the click event of btnConfirmEntry validate the entry using something like

    If TextBox2.Visible=True then
    (Validate TextBox2 entry, make btnConfirmEntry invisible
    and btnCalculate visible)
    else
    (Validate TextBox1 entry and make TextBox2 visible)
    End if



    Elementary but visually effective as it concentrates attention on the relative textboxes.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    127
    I used the modified code below to get a combination that I think will fit best. It throws the MessageBox and prevents other functions perfectly but..... the MessageBox doesn't have a title in the title bar. Something like "Error Message" would be nice to notify the user what the MessageBox is telling them other than the text it contains "You must enter a range first!"

    Code:
    If TextBox1.Text = "" Or TextBox2.Text = "" Then
                MessageBox.Show("You must enter a range first!")
            Else
                Label1.Text = ""
                Label2.Text = ""
                Label3.Text = ""
                Label4.Text = ""
                Label5.Text = ""
                Label6.Text = ""
                TextBox3.Text = Val(Label1.Text)
                Exit Sub
            End If
    I'm still not experienced enough to know if there is a way to build a title in the titlebar of a MessageBox.

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Very simple. Just add another string.

    MessageBox.Show("You must enter a range first!", "Error Message")

    That quick enough for you
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    127
    You guys rock!!!

Posting Permissions

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



Click Here to Expand Forum to Full Width