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 Offline
    M Offline
    manni_n
    wrote on last edited by
    #1

    one or two days back i got he answer of validating a textboxes by giving values between 0 and 45. now i wanna validate textboxes again in a way that if one value is filled in one textbox user cant enter it again in any other textbox.. like if i have enterd 21 in textbox1, then in textbox2 i would not be able to fill 21 again. it shud give error at that time only... any help.. thanks..

    T 1 Reply Last reply
    0
    • M manni_n

      one or two days back i got he answer of validating a textboxes by giving values between 0 and 45. now i wanna validate textboxes again in a way that if one value is filled in one textbox user cant enter it again in any other textbox.. like if i have enterd 21 in textbox1, then in textbox2 i would not be able to fill 21 again. it shud give error at that time only... any help.. thanks..

      T Offline
      T Offline
      The ANZAC
      wrote on last edited by
      #2

      Try this, Iterate through all the controls, pick out the textbox's and check their value: For textbox1_textchanged event: dim t as textbox for each t in me.controls if ctype(t) is textbox then if t.text = textbox1.text then 'error end if end if next

      Posted by The ANZAC

      M 1 Reply Last reply
      0
      • T The ANZAC

        Try this, Iterate through all the controls, pick out the textbox's and check their value: For textbox1_textchanged event: dim t as textbox for each t in me.controls if ctype(t) is textbox then if t.text = textbox1.text then 'error end if end if next

        Posted by The ANZAC

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

        its not workin man.. giving some errors and one more thing each and every textbox i have to validate through this coding.. if i have more than 100 no. of textboxes then wouldn't it will become very lenghty procedure.. any shortcut for validating 100 textboxes.. thanks..

        T 1 Reply Last reply
        0
        • M manni_n

          its not workin man.. giving some errors and one more thing each and every textbox i have to validate through this coding.. if i have more than 100 no. of textboxes then wouldn't it will become very lenghty procedure.. any shortcut for validating 100 textboxes.. thanks..

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

          The first posters code failed because 't' was declared as a textbox. If the control collection contains something other then a textbox the cast will fail. 't' should have been a control. Also instead of hardcoding textbox1 you can use the same technique you learned from your first validation post. Use directcast to cast the sender object as a textbox. This will allow you to handle all your textboxes from this single procedure. Try the following

              Dim txtSender As TextBox = DirectCast(sender, TextBox)
          
              'Loop through all controls on the form
              'If the form isn't the parent use ParentName.Controls instead of me.controls
              For Each ctrl As Control In Me.Controls
                  'Is ctrl a texbox and NOT the textbox that raised this method
                  If TypeOf ctrl Is TextBox AndAlso ctrl IsNot txtSender Then
                      If DirectCast(ctrl, TextBox).Text = txtSender.Text Then
                          'Add code to raise Error
                          Exit For
                      End If
                  End If
              Next
          

          I know you were told you could add handlers for multiple textboxes by doing Handles textbox1.textchanged, textbox2.textchange etc. But if you have a 100 textboxes that's just tedious especially when you have to do that for multiple procedures. The following code will loop through the controls and add handlers for you when the form loads. In this example I added a handler for the textchanged event for all the textboxes found on the form but you can add whatever handlers you want. I also added the textbox to a collection so you don't have to loop through the controls later but can loop through the collection of just textboxes.

          Private TextBoxCollection As List(Of TextBox)
          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 txtBox\_TextChanged
                  End If
              Next
          End Sub
          

          With the addition of the collection the first bit of code could be changed to this.

              Dim txtSender As TextBox = DirectCast(sender, TextBox)
          
              'Loop through all the textboxes
              For Each txt As TextBox In TextBoxCollection
                  'If txt isn't the control that raised this method and the values
                  'match raise an error
                  If txt IsNot txtSender AndAlso t
          
          M 1 Reply Last reply
          0
          • T TwoFaced

            The first posters code failed because 't' was declared as a textbox. If the control collection contains something other then a textbox the cast will fail. 't' should have been a control. Also instead of hardcoding textbox1 you can use the same technique you learned from your first validation post. Use directcast to cast the sender object as a textbox. This will allow you to handle all your textboxes from this single procedure. Try the following

                Dim txtSender As TextBox = DirectCast(sender, TextBox)
            
                'Loop through all controls on the form
                'If the form isn't the parent use ParentName.Controls instead of me.controls
                For Each ctrl As Control In Me.Controls
                    'Is ctrl a texbox and NOT the textbox that raised this method
                    If TypeOf ctrl Is TextBox AndAlso ctrl IsNot txtSender Then
                        If DirectCast(ctrl, TextBox).Text = txtSender.Text Then
                            'Add code to raise Error
                            Exit For
                        End If
                    End If
                Next
            

            I know you were told you could add handlers for multiple textboxes by doing Handles textbox1.textchanged, textbox2.textchange etc. But if you have a 100 textboxes that's just tedious especially when you have to do that for multiple procedures. The following code will loop through the controls and add handlers for you when the form loads. In this example I added a handler for the textchanged event for all the textboxes found on the form but you can add whatever handlers you want. I also added the textbox to a collection so you don't have to loop through the controls later but can loop through the collection of just textboxes.

            Private TextBoxCollection As List(Of TextBox)
            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 txtBox\_TextChanged
                    End If
                Next
            End Sub
            

            With the addition of the collection the first bit of code could be changed to this.

                Dim txtSender As TextBox = DirectCast(sender, TextBox)
            
                'Loop through all the textboxes
                For Each txt As TextBox In TextBoxCollection
                    'If txt isn't the control that raised this method and the values
                    'match raise an error
                    If txt IsNot txtSender AndAlso t
            
            M Offline
            M Offline
            manni_n
            wrote on last edited by
            #5

            great man.. thanks for sending such a nice and detailed solution.. but i dont know why, i am not able to get this method. may be i need a decent R & D on this topic , you gave a good link to study. and meanwhile without putting my mind into into i am not able to implement this code directly coz getting some errors and literally having no idea abt them.. as this topic is new to me.... i wanted this validation very immedietly.. nyways i hope your code will be too much helpful for me once i'll come to know the topic...

            T 1 Reply Last reply
            0
            • M manni_n

              great man.. thanks for sending such a nice and detailed solution.. but i dont know why, i am not able to get this method. may be i need a decent R & D on this topic , you gave a good link to study. and meanwhile without putting my mind into into i am not able to implement this code directly coz getting some errors and literally having no idea abt them.. as this topic is new to me.... i wanted this validation very immedietly.. nyways i hope your code will be too much helpful for me once i'll come to know the topic...

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

              Well it doesn't help that I screwed up :) This line 'Private TextBoxCollection As List(Of TextBox)' should be 'Private TextBoxCollection As new List(Of TextBox)" Other then that the code works. It's hard to debug something when you don't fully understand it. It's pretty simple once you get it though. Sorry I don't have any links for you but you may want to research addhandler, and creating control arrays. The important parts of the code are as follows. The code in the Form Load event loops through ALL the controls directly on the form. What I mean by that is it won't include controls that may be inside a panel or other container. If your textboxes are located in a panel and not on the form you should use Panel1.controls (or whatever applies) instead of me.controls. It tests each control it finds to see if its a textbox. If it is, it adds it to the collection and adds a handler for the TextChanged event. The method txtBox_TextChanged is required for this to work. Basically what it does is says when the TextChanged event for this control fires run this procedure. The other code I gave you loops through the collection and compares the text in the current textbox with that of all the other textboxes. This code should probably go in the validation event. If you want it could be put in the TextChanged event instead. What also just occured to me is I forgot to post the txtBox_TextChanged method. This is one way you could do it.

              Private Sub txtBox\_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
                  Dim txtSender As TextBox = DirectCast(sender, TextBox)
                  'Loop through all the textboxes  
                  For Each txt As TextBox In TextBoxCollection
                      'If txt isn't the control that raised this method and the values   
                      'match raise an error    
                      If txt IsNot txtSender AndAlso txt.Text = txtSender.Text Then
                          'Add code to raise Error  
                          Console.WriteLine("Matched")
                          Exit For
                      End If
                  Next
              End Sub
              

              If you add this method to your code with the form load code you should have something that works without error. Sorry, I left out the necessary method before.

              M 1 Reply Last reply
              0
              • T TwoFaced

                Well it doesn't help that I screwed up :) This line 'Private TextBoxCollection As List(Of TextBox)' should be 'Private TextBoxCollection As new List(Of TextBox)" Other then that the code works. It's hard to debug something when you don't fully understand it. It's pretty simple once you get it though. Sorry I don't have any links for you but you may want to research addhandler, and creating control arrays. The important parts of the code are as follows. The code in the Form Load event loops through ALL the controls directly on the form. What I mean by that is it won't include controls that may be inside a panel or other container. If your textboxes are located in a panel and not on the form you should use Panel1.controls (or whatever applies) instead of me.controls. It tests each control it finds to see if its a textbox. If it is, it adds it to the collection and adds a handler for the TextChanged event. The method txtBox_TextChanged is required for this to work. Basically what it does is says when the TextChanged event for this control fires run this procedure. The other code I gave you loops through the collection and compares the text in the current textbox with that of all the other textboxes. This code should probably go in the validation event. If you want it could be put in the TextChanged event instead. What also just occured to me is I forgot to post the txtBox_TextChanged method. This is one way you could do it.

                Private Sub txtBox\_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
                    Dim txtSender As TextBox = DirectCast(sender, TextBox)
                    'Loop through all the textboxes  
                    For Each txt As TextBox In TextBoxCollection
                        'If txt isn't the control that raised this method and the values   
                        'match raise an error    
                        If txt IsNot txtSender AndAlso txt.Text = txtSender.Text Then
                            'Add code to raise Error  
                            Console.WriteLine("Matched")
                            Exit For
                        End If
                    Next
                End Sub
                

                If you add this method to your code with the form load code you should have something that works without error. Sorry, I left out the necessary method before.

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

                well its still giving the same errors as previous.. its still givingg error at 1. list(of textbox) 2. txtbox_textchanged 3. isnot textsender line. m sending u the code as i am implementing it and as u have told.. jst check it out.. may be m inplementing it incorrectly... if possible please run it and send me the full code of program, i am gonna insert it directly... that woulds be an appreciable help to me... thanks.. Private TextBoxCollection As new List(Of TextBox)' giving error 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 txtBox_TextChanged End If Next End Sub Private Sub txtBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Dim txtSender As TextBox = DirectCas(sender,TextBox) For Each txt As TextBox In TextBoxCollection If txt IsNot txtSender AndAlso txt.Text = txtSender.Text Then ' this whole line is giving errror Console.WriteLine("Matched") Exit For End If Next End Sub

                T 1 Reply Last reply
                0
                • M manni_n

                  well its still giving the same errors as previous.. its still givingg error at 1. list(of textbox) 2. txtbox_textchanged 3. isnot textsender line. m sending u the code as i am implementing it and as u have told.. jst check it out.. may be m inplementing it incorrectly... if possible please run it and send me the full code of program, i am gonna insert it directly... that woulds be an appreciable help to me... thanks.. Private TextBoxCollection As new List(Of TextBox)' giving error 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 txtBox_TextChanged End If Next End Sub Private Sub txtBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Dim txtSender As TextBox = DirectCas(sender,TextBox) For Each txt As TextBox In TextBoxCollection If txt IsNot txtSender AndAlso txt.Text = txtSender.Text Then ' this whole line is giving errror Console.WriteLine("Matched") Exit For End If Next End Sub

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

                  Change this:

                  manni_n wrote:

                  Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

                  To:

                  Private Sub txtBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)

                  Also what version of Visual Studio are you using. List(Of) is new in .net 2.0. If you are running a version before 2005 you may not have that class. Try using an arraylist instead.

                  Private TextBoxCollection As New ArrayList

                  M 1 Reply Last reply
                  0
                  • T TwoFaced

                    Change this:

                    manni_n wrote:

                    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

                    To:

                    Private Sub txtBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)

                    Also what version of Visual Studio are you using. List(Of) is new in .net 2.0. If you are running a version before 2005 you may not have that class. Try using an arraylist instead.

                    Private TextBoxCollection As New ArrayList

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

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

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