Results 1 to 12 of 12

Thread: Validating and checking the field

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    west
    Posts
    96

    Validating and checking the field

    Hi everybody

    I have a question to ask about the validation in the textbox shown in the screen dump.......I have attached a screen dump for your references......as u can see that, the numbers in each and every textbox is unique.......i do not want the same number to be appear in any of the textbox......may i ask ? if the user enters any of the number twice which i do not want it to happen, how do i check for the identical number after the user enters into the textbox.........thank u very much..........
    Attached Images Attached Images  

  2. #2
    Addicted Member
    Join Date
    Jan 2005
    Posts
    136

    Re: Validating and checking the field

    create an array.store what user enters and compare the array with textboxes

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    west
    Posts
    96

    Re: Validating and checking the field

    thanks nana_81.....can u write a simple code to illustrate this ? thank u very much

  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Validating and checking the field

    I would create a keypress event that includes all of your boxes. Next created two loops, one nested inside the other. Setup both loops to look through each control, and return text, if the control is a text box. Inside your loops check if the text from one loop matches the text from another. If it does, cancel the keypress and return a message box. I don't have VS at home, but here is a very rough draft of what it would like:
    VB Code:
    1. private sub ValidateTextbox_keypress(byval sender as object, byval e as system.keypressargs) handles textbox1, textbox2 'add your textboxes here
    2. for each ocontrol as control in me.controls
    3.   if typeof ocontrol is textbox then
    4.     for each ochildcontrol as control in me.controls
    5.       if typeof ochildcontrol is textbox then
    6.         if ocontrol.text = ochildcontrol.text then
    7.           e.handled = true
    8.           messagebox.show("You have entered the same value in " & ocontrol.name & " and " & ochildcontrol.name)
    9.         end if
    10.       end if
    11.     next ochildcontrol
    12.   end if
    13. next ocontrol
    14. end sub

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    west
    Posts
    96

    Re: Validating and checking the field

    thanks wild bill but may i ask ? b4 u use system.keypressargs.......you will need to import a class right? something like Import system.......outside the private class

  6. #6
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Validating and checking the field

    Nope. I selected the textbox1_keypress event, then changed the sub name, and add textbox2 to the handled section. Here is a much better way, that doesn't use nested loops, and prevents a user from entering letters and symbols.
    VB Code:
    1. Private Sub ValidEntry(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress, TextBox1.KeyPress
    2.         'Only allows numbers, and control keys (enter, backspace, . . .)
    3.         If e.KeyChar.IsDigit(e.KeyChar) = False And e.KeyChar.IsControl(e.KeyChar) = False Then
    4.             e.Handled = True
    5.         End If
    6.     End Sub
    7.     Private Sub CheckDups(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp, TextBox2.KeyUp
    8.         'Ensures the sender value does not exist in another textbox
    9.         Dim txtbx As TextBox = CType(sender, TextBox)
    10.         For Each oControl As Control In Me.Controls
    11.             If TypeOf oControl Is TextBox Then
    12.                 If oControl.Text = txtbx.Text And oControl.Name <> txtbx.Name And oControl.Text <> Nothing Then
    13.                     txtbx.Text = Nothing
    14.                     MessageBox.Show("You have entered the same value in " & oControl.Name & " and " & txtbx.Name)
    15.                     Exit Sub
    16.                 End If
    17.             End If
    18.         Next oControl
    19.     End Sub

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    west
    Posts
    96

    Re: Validating and checking the field

    thanks wild bill for your help but i still encounter a syntax error......it says that the name "oControl" is not declared....how to declare this class......thank u very much

  8. #8
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Validating and checking the field

    oControl is a control declaration.
    dim oControl as control

  9. #9
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Validating and checking the field

    The variable should be declared here:
    VB Code:
    1. For Each oControl As Control In Me.Controls

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

    Re: Validating and checking the field

    Hi,

    I prefer nana81's approach. Something like:

    VB Code:
    1. Dim arrNumbers(0) As Integer
    2. Dim icount As Integer
    3.  
    4.   Private Sub txtBoxes_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBox1.LostFocus, txtBox2.LostFocus, txtBox3  etc
    5.  
    6. iCount =  Cint(CType(Sender,TextBox).text)
    7.   If arrNumbers.GetUpperBound(0) < icount Then
    8.     Redim Preserve arrNumbers(icount)
    9.   End IF
    10.   If arrNumbers(iCount) <> 0 Then
    11.      MessageBox.Show("The Number  " &  iCount.ToString & "Has already been entered in  " & ctype(sender,TextBox).Name)     'I'm not sure about this bit. You may need to
    12.                                                       add  .TOString
    13.     ctype(Sender,TextBox).Text = ""
    14.     Exit Sub
    15.   End If
    16.   arrNumbers(iCount)      ' You could use a boolean instead of a numeric array
    17. End Sub
    Looking at it, you could use a reference to ctype(Sender,TextBox) rather than keep repeating it.

    BTW I trust you are creating the textboxes dynamically so that you can add them to the Handler event automatically
    Last edited by taxes; May 13th, 2005 at 10:12 AM.
    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.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    west
    Posts
    96

    Re: Validating and checking the field

    Hi taxes

    i try to use the code that u have provided and i hv this error message when
    arrNumbers is an expression and is not a method............may i know how to resolve this.....thank u very much

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

    Re: Validating and checking the field

    Quote Originally Posted by chioman
    Hi taxes

    i try to use the code that u have provided and i hv this error message when
    arrNumbers is an expression and is not a method............may i know how to resolve this.....thank u very much
    Sorry, my crystal ball is not working today

    WHAT error message and on WHICH line?
    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.

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