1 Attachment(s)
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..........
Re: Validating and checking the field
create an array.store what user enters and compare the array with textboxes
Re: Validating and checking the field
thanks nana_81.....can u write a simple code to illustrate this ? thank u very much
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:
private sub ValidateTextbox_keypress(byval sender as object, byval e as system.keypressargs) handles textbox1, textbox2 'add your textboxes here
for each ocontrol as control in me.controls
if typeof ocontrol is textbox then
for each ochildcontrol as control in me.controls
if typeof ochildcontrol is textbox then
if ocontrol.text = ochildcontrol.text then
e.handled = true
messagebox.show("You have entered the same value in " & ocontrol.name & " and " & ochildcontrol.name)
end if
end if
next ochildcontrol
end if
next ocontrol
end sub
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
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:
Private Sub ValidEntry(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress, TextBox1.KeyPress
'Only allows numbers, and control keys (enter, backspace, . . .)
If e.KeyChar.IsDigit(e.KeyChar) = False And e.KeyChar.IsControl(e.KeyChar) = False Then
e.Handled = True
End If
End Sub
Private Sub CheckDups(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp, TextBox2.KeyUp
'Ensures the sender value does not exist in another textbox
Dim txtbx As TextBox = CType(sender, TextBox)
For Each oControl As Control In Me.Controls
If TypeOf oControl Is TextBox Then
If oControl.Text = txtbx.Text And oControl.Name <> txtbx.Name And oControl.Text <> Nothing Then
txtbx.Text = Nothing
MessageBox.Show("You have entered the same value in " & oControl.Name & " and " & txtbx.Name)
Exit Sub
End If
End If
Next oControl
End Sub
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
Re: Validating and checking the field
oControl is a control declaration.
dim oControl as control
Re: Validating and checking the field
The variable should be declared here:
VB Code:
For Each oControl As Control In Me.Controls
Re: Validating and checking the field
Hi,
I prefer nana81's approach. Something like:
VB Code:
Dim arrNumbers(0) As Integer
Dim icount As Integer
Private Sub txtBoxes_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBox1.LostFocus, txtBox2.LostFocus, txtBox3 etc
iCount = Cint(CType(Sender,TextBox).text)
If arrNumbers.GetUpperBound(0) < icount Then
Redim Preserve arrNumbers(icount)
End IF
If arrNumbers(iCount) <> 0 Then
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
add .TOString
ctype(Sender,TextBox).Text = ""
Exit Sub
End If
arrNumbers(iCount) ' You could use a boolean instead of a numeric array
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 :eek:
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
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 :D
WHAT error message and on WHICH line?