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..........
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)
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
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)
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
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
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.
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
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.