Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Mr. Mogtabam please check..

Mr. Mogtabam please check..

Scheduled Pinned Locked Moved Visual Basic
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    manni_n
    wrote on last edited by
    #1

    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

    K N M 3 Replies Last reply
    0
    • M manni_n

      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

      K Offline
      K Offline
      kubben
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • M manni_n

        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

        N Offline
        N Offline
        nlarson11
        wrote on last edited by
        #3

        use the control collection and add an event to each textbox calling the same routine

        1 Reply Last reply
        0
        • K kubben

          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

          M Offline
          M Offline
          manni_n
          wrote on last edited by
          #4

          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

          K T 3 Replies Last reply
          0
          • M manni_n

            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

            K Offline
            K Offline
            kubben
            wrote on last edited by
            #5

            Your eventSender is the TextBox So: if Val(CType(eventSender, TextBox).Text) > 45 Then CType(eventSender, TextBox).Text = CStr(0) ... Hope that helps. Ben

            M 1 Reply Last reply
            0
            • M manni_n

              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

              T Offline
              T Offline
              TwoFaced
              wrote on last edited by
              #6

              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
              
              1 Reply Last reply
              0
              • M manni_n

                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

                T Offline
                T Offline
                TwoFaced
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • M manni_n

                  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

                  M Offline
                  M Offline
                  Mogtabam
                  wrote on last edited by
                  #8

                  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
                  
                  1 Reply Last reply
                  0
                  • K kubben

                    Your eventSender is the TextBox So: if Val(CType(eventSender, TextBox).Text) > 45 Then CType(eventSender, TextBox).Text = CStr(0) ... Hope that helps. Ben

                    M Offline
                    M Offline
                    manni_n
                    wrote on last edited by
                    #9

                    thanks man.. its working perfectly... and reduced mah problem alot.. regards.. manni

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups