Looping through TextBoxes, need help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KungWaz
    New Member
    • Mar 2008
    • 5

    Looping through TextBoxes, need help

    I want to loop through all my text boxes in my form and write a 0 (zero) in all those which don't have positive integers in them. Don't want any characters or negative numbers, only positive integers.

    Thanks in advance
  • janders468
    Recognized Expert New Member
    • Mar 2008
    • 112

    #2
    I believe this will do what you want:
    Code:
      Dim ctrl As Control
        For Each ctrl In Me.Controls
            If TypeName(ctrl) = "TextBox" Then
                If (Not (IsNumeric(ctrl.Value)) Or (ctrl.Value < 0)) Then
                    ctrl.Value = 0
                End If
            End If
        Next ctrl
    If you are controlling this from another form then use that form instead of 'me' as the form containing the controls.

    Comment

    • KungWaz
      New Member
      • Mar 2008
      • 5

      #3
      Thank you very much! I will try it right away! =)

      Comment

      • KungWaz
        New Member
        • Mar 2008
        • 5

        #4
        It say's:

        Value is not a member of "system.windows .form.control"

        ... why?

        Comment

        • janders468
          Recognized Expert New Member
          • Mar 2008
          • 112

          #5
          Sorry, didn't realize that you were using vb.net, thought it was vb/vba use text instead of value. I just tested this and be sure that when you check for the typename that it is "TextBox" (case is significant) let me know if that works.

          Comment

          • KungWaz
            New Member
            • Mar 2008
            • 5

            #6
            Now it works for the negative ingers, but still not if I type a char,

            An unhandled exception of type 'System.Invalid CastException' occurred in Microsoft.Visua lBasic.dll

            Additional information: Conversion from string "characters " to type 'Double' is not valid.

            and it highlights this:

            If (Not (IsNumeric(ctrl .Text)) Or (ctrl.Text < 0)) Then

            I wrote "characters " in the TextBox...

            Comment

            • janders468
              Recognized Expert New Member
              • Mar 2008
              • 112

              #7
              Okay Sorry for all the runaround I think this will work for you
              Code:
                     Dim ctrl As Control
                      For Each ctrl In Me.Controls
                          If TypeName(ctrl) = "TextBox" Then
                              If ctrl.Text = "" Then
                                  ctrl.Text = 0
                              ElseIf (Not IsNumeric(ctrl.Text)) Then
                                  ctrl.Text = 0
                              ElseIf (CInt(ctrl.Text) < 0) Then
                                  ctrl.Text = 0
                              End If
                          End If
                      Next ctrl

              Comment

              • KungWaz
                New Member
                • Mar 2008
                • 5

                #8
                YeaY! Now it works!

                Thank you very much! now I'm happy :p

                Comment

                Working...