Mr. Mogtabam please check..
-
hello sir u have send me the solution of validating textboxes,given below its working absolutely fine ... but if i have 100 textboxes then do i have to validate each and everyone by coding 12-15 lines.. i think it will be very tedious.... any shortcut..? please tell in vb.net i am working on VS2003. code i am usung is Private Sub Textbox1_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TextBox1.TextChanged If Val(TextBox1.Text) > 45 Then TextBox1.Text = CStr(0) : MsgBox("Please Enter A Number Between 0 to 45", MsgBoxStyle.Critical) End Sub Private Sub Text1_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim KeyAscii As Short = Asc(eventArgs.KeyChar) Select Case KeyAscii Case 48 To 57 '0 To 9 Case 8 'Back Space Case 13 'Enter Case Else 'Other Keys KeyAscii = 0 End Select If KeyAscii = 0 Then eventArgs.Handled = True End If End Sub
-
hello sir u have send me the solution of validating textboxes,given below its working absolutely fine ... but if i have 100 textboxes then do i have to validate each and everyone by coding 12-15 lines.. i think it will be very tedious.... any shortcut..? please tell in vb.net i am working on VS2003. code i am usung is Private Sub Textbox1_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TextBox1.TextChanged If Val(TextBox1.Text) > 45 Then TextBox1.Text = CStr(0) : MsgBox("Please Enter A Number Between 0 to 45", MsgBoxStyle.Critical) End Sub Private Sub Text1_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim KeyAscii As Short = Asc(eventArgs.KeyChar) Select Case KeyAscii Case 48 To 57 '0 To 9 Case 8 'Back Space Case 13 'Enter Case Else 'Other Keys KeyAscii = 0 End Select If KeyAscii = 0 Then eventArgs.Handled = True End If End Sub
I didn't give you the code, but the same textChanged event can be assigned to all of your 100 textboxes. So I would suggest that you connect each of your textboxes with that one textChanged event. The way to do this is: At the end where you see the Handles key word you have the TextBox1.TextChanged All you have to do is add: ,TextBox2.TextChanged, TextBox3.TextChanged, ... till you have added all 100 textboxes. Hope that helps. Ben
-
hello sir u have send me the solution of validating textboxes,given below its working absolutely fine ... but if i have 100 textboxes then do i have to validate each and everyone by coding 12-15 lines.. i think it will be very tedious.... any shortcut..? please tell in vb.net i am working on VS2003. code i am usung is Private Sub Textbox1_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TextBox1.TextChanged If Val(TextBox1.Text) > 45 Then TextBox1.Text = CStr(0) : MsgBox("Please Enter A Number Between 0 to 45", MsgBoxStyle.Critical) End Sub Private Sub Text1_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim KeyAscii As Short = Asc(eventArgs.KeyChar) Select Case KeyAscii Case 48 To 57 '0 To 9 Case 8 'Back Space Case 13 'Enter Case Else 'Other Keys KeyAscii = 0 End Select If KeyAscii = 0 Then eventArgs.Handled = True End If End Sub
-
I didn't give you the code, but the same textChanged event can be assigned to all of your 100 textboxes. So I would suggest that you connect each of your textboxes with that one textChanged event. The way to do this is: At the end where you see the Handles key word you have the TextBox1.TextChanged All you have to do is add: ,TextBox2.TextChanged, TextBox3.TextChanged, ... till you have added all 100 textboxes. Hope that helps. Ben
u want tosay like this.... Private Sub Textbox1_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged what about the other places where textbox1.text is written like textbox1.keypress and if val(textbox1.text) and all such place where only textbox1.validated.... its highly confusing... Private Sub Textbox1_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged If Val(TextBox1.Text) > 45 Then TextBox1.Text = CStr(0) : MsgBox("Please Enter A Number Between 0 to 45", MsgBoxStyle.Critical) End Sub Private Sub Text1box_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim KeyAscii As Short = Asc(eventArgs.KeyChar) Select Case KeyAscii Case 48 To 57 '0 To 9 Case 8 'Back Space Case 13 'Enter Case Else 'Other Keys KeyAscii = 0 End Select If KeyAscii = 0 Then eventArgs.Handled = True End If End Sub
-
u want tosay like this.... Private Sub Textbox1_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged what about the other places where textbox1.text is written like textbox1.keypress and if val(textbox1.text) and all such place where only textbox1.validated.... its highly confusing... Private Sub Textbox1_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged If Val(TextBox1.Text) > 45 Then TextBox1.Text = CStr(0) : MsgBox("Please Enter A Number Between 0 to 45", MsgBoxStyle.Critical) End Sub Private Sub Text1box_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim KeyAscii As Short = Asc(eventArgs.KeyChar) Select Case KeyAscii Case 48 To 57 '0 To 9 Case 8 'Back Space Case 13 'Enter Case Else 'Other Keys KeyAscii = 0 End Select If KeyAscii = 0 Then eventArgs.Handled = True End If End Sub
-
u want tosay like this.... Private Sub Textbox1_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged what about the other places where textbox1.text is written like textbox1.keypress and if val(textbox1.text) and all such place where only textbox1.validated.... its highly confusing... Private Sub Textbox1_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged If Val(TextBox1.Text) > 45 Then TextBox1.Text = CStr(0) : MsgBox("Please Enter A Number Between 0 to 45", MsgBoxStyle.Critical) End Sub Private Sub Text1box_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim KeyAscii As Short = Asc(eventArgs.KeyChar) Select Case KeyAscii Case 48 To 57 '0 To 9 Case 8 'Back Space Case 13 'Enter Case Else 'Other Keys KeyAscii = 0 End Select If KeyAscii = 0 Then eventArgs.Handled = True End If End Sub
I think as a user I would be annoyed if I got a message everytime I typed in a number greater then 45. The way you have it set up I could type in 12 then accidentally hit 3, thus having 123 in the box, and I would get a warning in addition to the value being reset to 0. I think you should just stop the key from being pressed if it will result in to large a value. This way the user can't type in a larger number and you avoid the warning and clearing the value simply because I hit an extra character on my keyboard. Here is the code to handle that.
Private Sub TextBox1\_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If Char.IsDigit(e.KeyChar) Then 'Key was a digit Dim txtBox As TextBox = DirectCast(sender, TextBox) Dim value As Integer Dim newText As String = txtBox.Text.Remove(txtBox.SelectionStart, txtBox.SelectionLength) newText = newText.Insert(txtBox.SelectionStart, e.KeyChar.ToString) 'If value of textbox will be greater then 45 ignore key If Integer.TryParse(newText, value) Then If value > 45 Then e.Handled = True End If ElseIf Not Char.IsControl(e.KeyChar) Then 'Ignore key: It wasn't digit, backspace, enter, or delete e.Handled = True End If End Sub
-
u want tosay like this.... Private Sub Textbox1_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged what about the other places where textbox1.text is written like textbox1.keypress and if val(textbox1.text) and all such place where only textbox1.validated.... its highly confusing... Private Sub Textbox1_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged If Val(TextBox1.Text) > 45 Then TextBox1.Text = CStr(0) : MsgBox("Please Enter A Number Between 0 to 45", MsgBoxStyle.Critical) End Sub Private Sub Text1box_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim KeyAscii As Short = Asc(eventArgs.KeyChar) Select Case KeyAscii Case 48 To 57 '0 To 9 Case 8 'Back Space Case 13 'Enter Case Else 'Other Keys KeyAscii = 0 End Select If KeyAscii = 0 Then eventArgs.Handled = True End If End Sub
Oh, one more thing. The function 'val' returns 0 if the string isn't a number. Remember a user can still copy and paste text into your textbox. If you use val to determine the value a string of letters it will pass because 'val' will return 0 which is less then 45. You should first make sure the text is a number then convert it's value.
-
hello sir u have send me the solution of validating textboxes,given below its working absolutely fine ... but if i have 100 textboxes then do i have to validate each and everyone by coding 12-15 lines.. i think it will be very tedious.... any shortcut..? please tell in vb.net i am working on VS2003. code i am usung is Private Sub Textbox1_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TextBox1.TextChanged If Val(TextBox1.Text) > 45 Then TextBox1.Text = CStr(0) : MsgBox("Please Enter A Number Between 0 to 45", MsgBoxStyle.Critical) End Sub Private Sub Text1_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim KeyAscii As Short = Asc(eventArgs.KeyChar) Select Case KeyAscii Case 48 To 57 '0 To 9 Case 8 'Back Space Case 13 'Enter Case Else 'Other Keys KeyAscii = 0 End Select If KeyAscii = 0 Then eventArgs.Handled = True End If End Sub
Hi You Can Make An "User Control" For Problem Such This. or If Posible You Can Use Array. If You Make Array TextBox Your Code Will Be Same This :
Private Sub Text1_Change(Index As Integer) If Val(Text1(Index).Text) > 45 Then Text1(Index).Text = 45: MsgBox "Please Enter A Number Between 0 to 45", vbCritical End Sub Private Sub Text1_KeyPrees(Index as integer,KeyAscii as integer) Same Last Code End Sub
-
Your eventSender is the TextBox So: if Val(CType(eventSender, TextBox).Text) > 45 Then CType(eventSender, TextBox).Text = CStr(0) ... Hope that helps. Ben