how to validate the Textbox.text is not empty?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LittleDong
    New Member
    • Mar 2012
    • 11

    how to validate the Textbox.text is not empty?

    would somebody show me a convenient way to validate Textbox's value are not null or empty in Winform.
    if-else condition is only good for fewer textboxs in a winform or solution,
    if(Textbox1.tex t==null)
    {
    ...
    }
    else
    {
    ...
    }

    but if there are lots of textbox?
  • mohit2k8
    New Member
    • Feb 2012
    • 5

    #2
    may this code would help you..
    after converting textbox value into string
    if(textbox1.tex t=="")

    Comment

    • LittleDong
      New Member
      • Mar 2012
      • 11

      #3
      if there are six textforms in a winform
      I think it's not a good way to write
      if((Textbox1.te xt=="")&&(Textb ox1.text==null) )
      .
      .
      if((Textbox2.te xt=="")&&(Textb ox2.text==null) )
      .
      .
      if((Textbox3.te xt=="")&&(Textb ox3.text==null) )
      .
      .
      if((Textbox4.te xt=="")&&(Textb ox4.text==null) )
      .
      .
      .
      .

      Originally posted by mohit2k8
      may this code would help you..
      after converting textbox value into string
      if(textbox1.tex t=="")

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        You have many options, IMO, worst is to create a helper method that does this on a list of text components, much better is to learn the decorator pattern:http://en.wikipedia.org/wiki/Decorator_pattern
        You decorate the text boxes with ability to validate their text. You are going to need to learn to learn this pattern if you want to write maintainable gui programs anyway.

        Comment

        • rexzel0230
          New Member
          • Aug 2007
          • 3

          #5
          hello.. you can create a function to check if the textbox is empty.

          sample (vb.net)

          Code:
          function txtIsEmpty(frm as form)
              dim isempty as boolean=true
                  For Each crl As Control In frm.Controls
                      If TypeOf crl Is TextBox Then
                          if trim(trl.text)="" then
                              isempty=true
                              exit for
                          else
                              isempty=true
                          end if
                      ElseIf TypeOf crl Is GroupBox Then
                          'if the control is groupbox, its contents will not be included.
                          For Each crl1 As Control In crl.Controls
                              If crl1.GetType Is GetType(TextBox) Then
                                  if trim(crl1.text)="" then
                                      isempty=true
                                      exit for
                                  else
                                      isempty=false
                                  end if
                              End If
                          Next
                      End If
                  Next
              txtIsEmpty=isempty
          end function
          the above function will check all of the textbox on your form as well as inside the group box

          to call the function:

          if txtIsEmpty(Form 1) then
          msgbox "Other texts are empty."
          end if
          Last edited by PsychoCoder; Mar 7 '12, 07:10 PM. Reason: Added code tags

          Comment

          • PalomoSoft
            New Member
            • Mar 2012
            • 1

            #6
            for VB6

            Code:
            ************
            Public function cHecker(frm as form)as Boolean
              dim obj as string
              For each obj in frm
                If typeof obj is textbox then
                  If obj.text="" then
                     checker=false
                     msgbox "Textbox is empty"
                     exit function
                  End if
                End if
              Next obj
            End Function
            
            ***********syntax
            if checker= true then
               msgbox "Textbox is not empty"
            end if
            Last edited by PsychoCoder; Mar 23 '12, 03:47 AM. Reason: Added code tags

            Comment

            • bshelton3
              New Member
              • Mar 2012
              • 3

              #7
              For simply checking to see if a text string is null or empty I would use this:

              Comment

              • bshelton3
                New Member
                • Mar 2012
                • 3

                #8
                For simply checking to see if a text string is null or empty I would use this:

                Comment

                • PsychoCoder
                  Recognized Expert Contributor
                  • Jul 2010
                  • 465

                  #9
                  There's no need to write a method for this, just use String.IsNullOr Empty like this

                  Code:
                  if(string.IsNullOrEmpty(TextBox1.text))
                  { 
                      //do work here
                  }
                  You could also use string.IsNullOr WhiteSpace
                  Last edited by NeoPa; Mar 27 '12, 02:23 PM. Reason: Fixed retrospectively for Best Answer {Originally IsNullOrEmpty(TextBox1))}

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Did you mean
                    Code:
                    if(string.IsNullOrEmpty(Textbox1.text)){
                    instead?

                    Mod edit
                    For line #1 instead of :
                    Code:
                    if(string.IsNullOrEmpty(TextBox1))
                    Last edited by NeoPa; Mar 27 '12, 02:25 PM. Reason: Added to illustrate conversation after I fixed original for Best Answer.

                    Comment

                    • PsychoCoder
                      Recognized Expert Contributor
                      • Jul 2010
                      • 465

                      #11
                      Yes I did, thanks for seeing that

                      Comment

                      Working...