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. validation of textoxes...

validation of textoxes...

Scheduled Pinned Locked Moved Visual Basic
19 Posts 3 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 manni_n

    i modified the code check it.... i did in this way only but still list(of textbox) and isnot line are giving errors.

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

    Check my last post again...I made a modification. Also what is the error your getting with the IsNot line. I don't know when IsNot was added but perhaps you should try 'Not txt is txtSender' instead

    M 2 Replies Last reply
    0
    • T TwoFaced

      Check my last post again...I made a modification. Also what is the error your getting with the IsNot line. I don't know when IsNot was added but perhaps you should try 'Not txt is txtSender' instead

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

      this code is not workin man, its flying... great job... i am really thankful to ya alot... now i think i can frealy go for a sleep... i was tryn differnt logics for this validations from last 10 hours.. feeling gud now.. bubye buddy.. thanks again and gud night.

      1 Reply Last reply
      0
      • T TwoFaced

        Check my last post again...I made a modification. Also what is the error your getting with the IsNot line. I don't know when IsNot was added but perhaps you should try 'Not txt is txtSender' instead

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

        there is one more problem in it... suppose in textbox1 i have filled integer 3 and in textbox2 i have to fill 31, so the moment i press 3 in textbox2 it will show the error that same value... it wont wait for the 1 to get typed...

        T 1 Reply Last reply
        0
        • M manni_n

          there is one more problem in it... suppose in textbox1 i have filled integer 3 and in textbox2 i have to fill 31, so the moment i press 3 in textbox2 it will show the error that same value... it wont wait for the 1 to get typed...

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

          Yup, thats right. I recomend the validation event. Personally I wouldn't validate text in the manner you want as it changes for the very reason you are now experiencing. What you need to do is create a new procedure to handle the validation event. This is the declaration of that procedure.

          Private Sub txtBox\_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
          
          End Sub
          

          You should move the validation code into this procedure. Then you'll need to change the addhandler statement. The event should be validating and the part that goes after addressof should be the name of the procedure ie. txtBox_Validating. The validating event fires just before the focus changes. In the validating event you can use e.cancel = true to prevent focus from leaving the textbox. So if the text isn't validated you can display a message and cancel the change of focus. One more thing. Use this

          If not txt Is txtSender AndAlso txt.Text = txtSender.Text AndAlso txt.Text <> "" Then

          instead of the current line you have otherwise validation will fail if a textbox is empty because other textbox's are empty. I should have changed that earlier. I forgot to mention it.

          M 1 Reply Last reply
          0
          • T TwoFaced

            Yup, thats right. I recomend the validation event. Personally I wouldn't validate text in the manner you want as it changes for the very reason you are now experiencing. What you need to do is create a new procedure to handle the validation event. This is the declaration of that procedure.

            Private Sub txtBox\_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
            
            End Sub
            

            You should move the validation code into this procedure. Then you'll need to change the addhandler statement. The event should be validating and the part that goes after addressof should be the name of the procedure ie. txtBox_Validating. The validating event fires just before the focus changes. In the validating event you can use e.cancel = true to prevent focus from leaving the textbox. So if the text isn't validated you can display a message and cancel the change of focus. One more thing. Use this

            If not txt Is txtSender AndAlso txt.Text = txtSender.Text AndAlso txt.Text <> "" Then

            instead of the current line you have otherwise validation will fail if a textbox is empty because other textbox's are empty. I should have changed that earlier. I forgot to mention it.

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

            yeah i had done this thing of validating the empty textbox... but i am affraid this e.cancel = true is not working dude... i am using VS 2003. i think this code must be valid in 2005. only this thing is stucked rest everything is running fine.... any other method u might be having...

            T 1 Reply Last reply
            0
            • M manni_n

              yeah i had done this thing of validating the empty textbox... but i am affraid this e.cancel = true is not working dude... i am using VS 2003. i think this code must be valid in 2005. only this thing is stucked rest everything is running fine.... any other method u might be having...

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

              Sorry, that's the only event I know of. I've never used 2003 so I'm not aware of the differences but it does work in 2005. If it's not working for you I'm not sure what to do. I think that's still the right place to do the validation however. You'll just have to come up with a workaround. For one, calling a controls focus method will make that control active. So you could do that inside the validating event if validation fails. At least that way you can bring the users attention back to that control.

              M 1 Reply Last reply
              0
              • T TwoFaced

                Sorry, that's the only event I know of. I've never used 2003 so I'm not aware of the differences but it does work in 2005. If it's not working for you I'm not sure what to do. I think that's still the right place to do the validation however. You'll just have to come up with a workaround. For one, calling a controls focus method will make that control active. So you could do that inside the validating event if validation fails. At least that way you can bring the users attention back to that control.

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

                no buddy, i have checked every second method which i was knowing, to do this validation. i also tried to used gotfocus validation of textbox.but i think every effort went into vein. as i am using back end also so i tried to validate this through backend validation. like if the value in the textbox is same then it wont enter in DB and gives an error. But i think this will be the bad logic.. i cant do anything as i am left with this only... nyways after using your method fully now i am only stuck at this problem, my every second problem is solved by you reagardin this..the only thing left is it shud not validate untill the user moves to other controll. sending u the code agian jst have alook and check out if something can be done or not.... nyways thanks.... you really helped alot , i'll make sure in future that i could help you in some way and repay you... Public class form1 Inherits System.Windows.Forms.Form Private TextBoxCollection As New ArrayList Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each ctrl As Control In Me.Controls If TypeOf ctrl Is TextBox Then TextBoxCollection.Add(DirectCast(ctrl, TextBox)) AddHandler ctrl.TextChanged, AddressOf txtvalidate_validating 'e.cancel = True End If Next End Sub Private Sub txtvalidate_validating(ByVal sender As Object, ByVal e As EventArgs) Dim txtSender As TextBox = DirectCast(sender, TextBox) For Each txt As TextBox In TextBoxCollection If Not txt Is txtSender AndAlso txt.Text = txtSender.Text AndAlso txt.Text <> "" Then CType(sender, TextBox).Text = CStr(txt.Text) MsgBox("You have already filled this value") Exit For End If Next End Sub

                T 1 Reply Last reply
                0
                • M manni_n

                  no buddy, i have checked every second method which i was knowing, to do this validation. i also tried to used gotfocus validation of textbox.but i think every effort went into vein. as i am using back end also so i tried to validate this through backend validation. like if the value in the textbox is same then it wont enter in DB and gives an error. But i think this will be the bad logic.. i cant do anything as i am left with this only... nyways after using your method fully now i am only stuck at this problem, my every second problem is solved by you reagardin this..the only thing left is it shud not validate untill the user moves to other controll. sending u the code agian jst have alook and check out if something can be done or not.... nyways thanks.... you really helped alot , i'll make sure in future that i could help you in some way and repay you... Public class form1 Inherits System.Windows.Forms.Form Private TextBoxCollection As New ArrayList Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each ctrl As Control In Me.Controls If TypeOf ctrl Is TextBox Then TextBoxCollection.Add(DirectCast(ctrl, TextBox)) AddHandler ctrl.TextChanged, AddressOf txtvalidate_validating 'e.cancel = True End If Next End Sub Private Sub txtvalidate_validating(ByVal sender As Object, ByVal e As EventArgs) Dim txtSender As TextBox = DirectCast(sender, TextBox) For Each txt As TextBox In TextBoxCollection If Not txt Is txtSender AndAlso txt.Text = txtSender.Text AndAlso txt.Text <> "" Then CType(sender, TextBox).Text = CStr(txt.Text) MsgBox("You have already filled this value") Exit For End If Next End Sub

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

                  Okay I see the mistake. Well I assume it's a mistake. Maybe you reverted back to using the TextChanged event because you couldn't get it to work, but I'll assume for the moment you didn't. When you use AddHandler you never changed the event that would be handled. The following line AddHandler ctrl.TextChanged, AddressOf txtvalidate_validating Should be: AddHandler ctrl.Validating, AddressOf txtvalidate_validating The portion after Addhandler is the event that gets handled. The portion after addressof is the method that does the handling. In this case we want to handle the validating event which is the change I made above. e.cancel = true should occur when validation fails. Also you changed the parameters for the txtValidate_Validating procedure. Probably because you were getting errors do to the fact that the event you were trying to handle was the TextChanged event and not the validating event. The final code should be this: Public Class Form1 Inherits System.Windows.Forms.Form Private TextBoxCollection As New ArrayList Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each ctrl As Control In Me.Controls If TypeOf ctrl Is TextBox Then TextBoxCollection.Add(DirectCast(ctrl, TextBox)) AddHandler ctrl.Validating, AddressOf txtvalidate_validating End If Next End Sub Private Sub txtvalidate_validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Dim txtSender As TextBox = DirectCast(sender, TextBox) For Each txt As TextBox In TextBoxCollection If Not txt Is txtSender AndAlso txt.Text = txtSender.Text AndAlso txt.Text <> "" Then MsgBox("You have already filled this value") e.Cancel = True Exit For End If Next End Sub End Class If there is a problem with this code it's because of differences in 2003 vs 2005. But this code works perfectly under 2005.

                  M 1 Reply Last reply
                  0
                  • T TwoFaced

                    Okay I see the mistake. Well I assume it's a mistake. Maybe you reverted back to using the TextChanged event because you couldn't get it to work, but I'll assume for the moment you didn't. When you use AddHandler you never changed the event that would be handled. The following line AddHandler ctrl.TextChanged, AddressOf txtvalidate_validating Should be: AddHandler ctrl.Validating, AddressOf txtvalidate_validating The portion after Addhandler is the event that gets handled. The portion after addressof is the method that does the handling. In this case we want to handle the validating event which is the change I made above. e.cancel = true should occur when validation fails. Also you changed the parameters for the txtValidate_Validating procedure. Probably because you were getting errors do to the fact that the event you were trying to handle was the TextChanged event and not the validating event. The final code should be this: Public Class Form1 Inherits System.Windows.Forms.Form Private TextBoxCollection As New ArrayList Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each ctrl As Control In Me.Controls If TypeOf ctrl Is TextBox Then TextBoxCollection.Add(DirectCast(ctrl, TextBox)) AddHandler ctrl.Validating, AddressOf txtvalidate_validating End If Next End Sub Private Sub txtvalidate_validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Dim txtSender As TextBox = DirectCast(sender, TextBox) For Each txt As TextBox In TextBoxCollection If Not txt Is txtSender AndAlso txt.Text = txtSender.Text AndAlso txt.Text <> "" Then MsgBox("You have already filled this value") e.Cancel = True Exit For End If Next End Sub End Class If there is a problem with this code it's because of differences in 2003 vs 2005. But this code works perfectly under 2005.

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

                    its ok and fine... the line in which u have modified should be having validated in place of validating coz controll is validated after the focus is off, like this still e.cancel is not working and showing the error that cancel is not the member of system.eventarg but its ok...i'll rectify this part through my database, by dissaloving the user to insert blank value.. AddHandler ctrl.Validated,AddressOf txtvalidate_validating now its working working perfectly... thanks... -- modified at 17:14 Friday 16th March, 2007

                    T 1 Reply Last reply
                    0
                    • M manni_n

                      its ok and fine... the line in which u have modified should be having validated in place of validating coz controll is validated after the focus is off, like this still e.cancel is not working and showing the error that cancel is not the member of system.eventarg but its ok...i'll rectify this part through my database, by dissaloving the user to insert blank value.. AddHandler ctrl.Validated,AddressOf txtvalidate_validating now its working working perfectly... thanks... -- modified at 17:14 Friday 16th March, 2007

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

                      The events fire in this order: LostFocus Validating Validated 'Cancel' is not a property of the system.eventarg object which is the object the Validated event recieves. 'Cancel' IS a property of the System.ComponentModel.CancelEventArgs which is the object the Validating event recieves. I don't understand why you think the validation should occur in the validated event. There is no reason the validation shouldn't be done in the validating event and one very good reason why it should be. This reason would be that you can prevent other controls from responding to the user before they have inserted valid data. I'm glad you've got it working though. As long as it works the way you want I see no reason to change it.

                      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