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. C#
  4. Checking several textboxes to make sure none are blank?

Checking several textboxes to make sure none are blank?

Scheduled Pinned Locked Moved C#
cssquestion
12 Posts 8 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.
  • V Vertekal

    I'm hoping there's a more streamlined approach to this. In a form, I have 7 textboxes and a button. When the button is clicked, each textbox needs to be looked at to ensure it contains something. If the textbox is blank, I have a label on the forum to alert the user of such. Then I direct focus to the blank textbox. So far I've been doing it like this: if (txt1.text == "") { ErrorLabel.text = "Enter something in txt1"; txt1.focus(); } else if (txt2.text == "") { ErrorLabel.text = "Enter something in txt2"; txt2.focus(); } else if (txt3.text == "") { ErrorLabel.text = "Enter something in txt3"; txt3.focus(); } else if (... and so on, and so on for all 7 textboxes) Is there a way to do it with less code?

    T Offline
    T Offline
    Thomas Stockwell
    wrote on last edited by
    #3

    if you don't want to go with databinding which is a good option, you can combine all the if statements: if(txt1.Text=="" OR txt2.text=="" OR txt3.text="" OR ...) { ErrorLabel.text="All fields must be entered to continue"; } Their still may be better options though.

    Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios[^]

    G 1 Reply Last reply
    0
    • T Thomas Stockwell

      if you don't want to go with databinding which is a good option, you can combine all the if statements: if(txt1.Text=="" OR txt2.text=="" OR txt3.text="" OR ...) { ErrorLabel.text="All fields must be entered to continue"; } Their still may be better options though.

      Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios[^]

      G Offline
      G Offline
      gspiteri
      wrote on last edited by
      #4

      Why dont you just use required field validators?

      R 1 Reply Last reply
      0
      • P Pete OHanlon

        Realistically you should create a class that implements the data that you want, and then put a binding against the textbox. With a bit of the magic listed here[^] you can save yourself a lot of work keeping validation and items in sync.

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #5

        Pete O'Hanlon wrote:

        With a bit of the magic listed here[^] you can save yourself a lot of work keeping validation and items in sync.

        That's a great article. Do you know any similar approach available for web application ?

        All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

        P 1 Reply Last reply
        0
        • G gspiteri

          Why dont you just use required field validators?

          R Offline
          R Offline
          Reelix
          wrote on last edited by
          #6

          *Clicks the Back Button2* "Please fill in all the fields to continue" >:( The reason I've stopped using RequiredFieldValidators....

          1 Reply Last reply
          0
          • N N a v a n e e t h

            Pete O'Hanlon wrote:

            With a bit of the magic listed here[^] you can save yourself a lot of work keeping validation and items in sync.

            That's a great article. Do you know any similar approach available for web application ?

            All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #7

            You could adapt it using techniques described in this[^] article.

            Deja View - the feeling that you've seen this post before.

            My blog | My articles

            1 Reply Last reply
            0
            • V Vertekal

              I'm hoping there's a more streamlined approach to this. In a form, I have 7 textboxes and a button. When the button is clicked, each textbox needs to be looked at to ensure it contains something. If the textbox is blank, I have a label on the forum to alert the user of such. Then I direct focus to the blank textbox. So far I've been doing it like this: if (txt1.text == "") { ErrorLabel.text = "Enter something in txt1"; txt1.focus(); } else if (txt2.text == "") { ErrorLabel.text = "Enter something in txt2"; txt2.focus(); } else if (txt3.text == "") { ErrorLabel.text = "Enter something in txt3"; txt3.focus(); } else if (... and so on, and so on for all 7 textboxes) Is there a way to do it with less code?

              P Offline
              P Offline
              Phil Martin
              wrote on last edited by
              #8

              How about this: foreach (var tb in new TextBox[] { txt1, txt2, txt3, txt4, txt5, txt6, txt7 }) { if (tb.Text == "") { ErrorLabel.text = string.format("Enter something in {0}", tb.Name); tb.Focus(); break; } } It aint pretty, and I'd prefer data binding, but it'd work?

              G P V 3 Replies Last reply
              0
              • P Phil Martin

                How about this: foreach (var tb in new TextBox[] { txt1, txt2, txt3, txt4, txt5, txt6, txt7 }) { if (tb.Text == "") { ErrorLabel.text = string.format("Enter something in {0}", tb.Name); tb.Focus(); break; } } It aint pretty, and I'd prefer data binding, but it'd work?

                G Offline
                G Offline
                gspiteri
                wrote on last edited by
                #9

                If you set required field validators when tabbing to each textbox it instantly does validation i dont see the issue perhaps ive misinterperted your query

                A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty I am a Optimist

                1 Reply Last reply
                0
                • P Phil Martin

                  How about this: foreach (var tb in new TextBox[] { txt1, txt2, txt3, txt4, txt5, txt6, txt7 }) { if (tb.Text == "") { ErrorLabel.text = string.format("Enter something in {0}", tb.Name); tb.Focus(); break; } } It aint pretty, and I'd prefer data binding, but it'd work?

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #10

                  That's what I would do too... so it must be wrong somehow. :laugh:

                  1 Reply Last reply
                  0
                  • V Vertekal

                    I'm hoping there's a more streamlined approach to this. In a form, I have 7 textboxes and a button. When the button is clicked, each textbox needs to be looked at to ensure it contains something. If the textbox is blank, I have a label on the forum to alert the user of such. Then I direct focus to the blank textbox. So far I've been doing it like this: if (txt1.text == "") { ErrorLabel.text = "Enter something in txt1"; txt1.focus(); } else if (txt2.text == "") { ErrorLabel.text = "Enter something in txt2"; txt2.focus(); } else if (txt3.text == "") { ErrorLabel.text = "Enter something in txt3"; txt3.focus(); } else if (... and so on, and so on for all 7 textboxes) Is there a way to do it with less code?

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #11

                    Or set the Button's Enabled property btn.Enabled = ( txt1.Text.Length > 0 ) && ... ;

                    1 Reply Last reply
                    0
                    • P Phil Martin

                      How about this: foreach (var tb in new TextBox[] { txt1, txt2, txt3, txt4, txt5, txt6, txt7 }) { if (tb.Text == "") { ErrorLabel.text = string.format("Enter something in {0}", tb.Name); tb.Focus(); break; } } It aint pretty, and I'd prefer data binding, but it'd work?

                      V Offline
                      V Offline
                      Vertekal
                      wrote on last edited by
                      #12

                      I'll give this a shot .. looks interesting I'd do databinding, but not sure how. I'm still new at this. If I created the DB within Visual Studio I'd be able to do it, but it's created at runtime (SQL CE) EDIT: I just tried it, and it worked great. Thanks!!!

                      modified on Friday, April 4, 2008 1:36 PM

                      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