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. How would I implement this?

How would I implement this?

Scheduled Pinned Locked Moved C#
helpquestion
17 Posts 5 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.
  • D deanoA

    I have 20 textboxes. Lets say, they are txtbox1 to txtbox20 Now, I would like to check them one by one if their text is null. If txtbox1.Text is null, i would stop checking the other textboxes right away. I can easily perform this using an if-else but, its not gonna be that readable. So I want to use a switch statement. The problem is, how? Please help. :( "To teach is to learn twice"

    N Offline
    N Offline
    Nick Parker
    wrote on last edited by
    #2

    daljv wrote: So I want to use a switch statement. The problem is, how? Why not just do soomething like this:

    ControlCollection col = (ControlCollection)this.Controls;
    foreach(Control t in col)
    {
    if(t is TextBox)
    {
    if(t.Text != String.Empty)
    {
    // Do something here.
    break;
    }
    }
    }

    -Nick Parker

    F P 2 Replies Last reply
    0
    • N Nick Parker

      daljv wrote: So I want to use a switch statement. The problem is, how? Why not just do soomething like this:

      ControlCollection col = (ControlCollection)this.Controls;
      foreach(Control t in col)
      {
      if(t is TextBox)
      {
      if(t.Text != String.Empty)
      {
      // Do something here.
      break;
      }
      }
      }

      -Nick Parker

      F Offline
      F Offline
      Frank Olorin Rizzi
      wrote on last edited by
      #3

      Nick Parker wrote: ControlCollection col = (ControlCollection)this.Controls;foreach(Control t in col){ if(t is TextBox) { if(t.Text != String.Empty) { // Do something here. break; } }} Hmmm... I can't help myself.... 1~ he wanted to stop as soon as one text box had no text (actually he mentioned only the first text box, but I'll assume it was an example). Thus //... if(t.Text == String.Empty) break; //... Also, why not keep your own collection of controls containing only the text boxes so that you would not have to parse through all of the controls in the form? Actually, since they are all text boxes, make it a collection of text boxes... So, when you add them to the form's Controls in your initializer, also add them to myBoxes; then: foreach(TextBox t in myBoxes) { if(t.Text == String.Empty) break; //Do whatever you need here... } HTH, F.O.R.

      N 1 Reply Last reply
      0
      • F Frank Olorin Rizzi

        Nick Parker wrote: ControlCollection col = (ControlCollection)this.Controls;foreach(Control t in col){ if(t is TextBox) { if(t.Text != String.Empty) { // Do something here. break; } }} Hmmm... I can't help myself.... 1~ he wanted to stop as soon as one text box had no text (actually he mentioned only the first text box, but I'll assume it was an example). Thus //... if(t.Text == String.Empty) break; //... Also, why not keep your own collection of controls containing only the text boxes so that you would not have to parse through all of the controls in the form? Actually, since they are all text boxes, make it a collection of text boxes... So, when you add them to the form's Controls in your initializer, also add them to myBoxes; then: foreach(TextBox t in myBoxes) { if(t.Text == String.Empty) break; //Do whatever you need here... } HTH, F.O.R.

        N Offline
        N Offline
        Nick Parker
        wrote on last edited by
        #4

        Frank Olorin Rizzi wrote: if(t.Text == String.Empty) break; Woops, I guess I flip-flopped that. :-O Ah well, I think he got the main idea which was important. Frank Olorin Rizzi wrote: Also, why not keep your own collection of controls containing only the text boxes so that you would not have to parse through all of the controls in the form? Because then he would have to maintain that collection where as the Form already maintains a collection of all controls applied to it. This is more extensible and easier to maintain. Granted there are other ways to do this, they simple require more work and don't provide much more in the efficiency department. :) -Nick Parker

        R F 2 Replies Last reply
        0
        • N Nick Parker

          Frank Olorin Rizzi wrote: if(t.Text == String.Empty) break; Woops, I guess I flip-flopped that. :-O Ah well, I think he got the main idea which was important. Frank Olorin Rizzi wrote: Also, why not keep your own collection of controls containing only the text boxes so that you would not have to parse through all of the controls in the form? Because then he would have to maintain that collection where as the Form already maintains a collection of all controls applied to it. This is more extensible and easier to maintain. Granted there are other ways to do this, they simple require more work and don't provide much more in the efficiency department. :) -Nick Parker

          R Offline
          R Offline
          Roland Bar
          wrote on last edited by
          #5

          Nick Parker wrote: Because then he would have to maintain that collection where as the Form already maintains a collection of all controls applied to it. This is more extensible and easier to maintain. Granted there are other ways to do this, they simple require more work and don't provide much more in the efficiency department. This argument lacks, if there are more text boxes in th esame form... Roland Bär

          N 1 Reply Last reply
          0
          • N Nick Parker

            Frank Olorin Rizzi wrote: if(t.Text == String.Empty) break; Woops, I guess I flip-flopped that. :-O Ah well, I think he got the main idea which was important. Frank Olorin Rizzi wrote: Also, why not keep your own collection of controls containing only the text boxes so that you would not have to parse through all of the controls in the form? Because then he would have to maintain that collection where as the Form already maintains a collection of all controls applied to it. This is more extensible and easier to maintain. Granted there are other ways to do this, they simple require more work and don't provide much more in the efficiency department. :) -Nick Parker

            F Offline
            F Offline
            Frank Olorin Rizzi
            wrote on last edited by
            #6

            Nick Parker wrote: Because then he would have to maintain that collection where as the Form already maintains a collection of all controls applied to it hmm.. yes, I see what you mean. ...speaking of which, let me ask this: If I picked the TextBox object that gets added to the Form's Controls collection (say it is named tBox1), and simply add it to my own collection (say an Hashtable) like so: myTable.Add(myTable.Count, tBox1); Wouldn't I be adding just a reference? Thus the impact on the memory would be relatively small? Or am I missing something here?

            N 1 Reply Last reply
            0
            • R Roland Bar

              Nick Parker wrote: Because then he would have to maintain that collection where as the Form already maintains a collection of all controls applied to it. This is more extensible and easier to maintain. Granted there are other ways to do this, they simple require more work and don't provide much more in the efficiency department. This argument lacks, if there are more text boxes in th esame form... Roland Bär

              N Offline
              N Offline
              Nick Parker
              wrote on last edited by
              #7

              Roland Bär wrote: This argument lacks, if there are more text boxes in th esame form... Show me an example please. -Nick Parker

              R 1 Reply Last reply
              0
              • N Nick Parker

                daljv wrote: So I want to use a switch statement. The problem is, how? Why not just do soomething like this:

                ControlCollection col = (ControlCollection)this.Controls;
                foreach(Control t in col)
                {
                if(t is TextBox)
                {
                if(t.Text != String.Empty)
                {
                // Do something here.
                break;
                }
                }
                }

                -Nick Parker

                P Offline
                P Offline
                Philip Fitzsimons
                wrote on last edited by
                #8

                this won't work - t is TextBox, t would still be a Control which does not have a .Text field so use "as". also why are you casting this.Controls? its a ControlCollection already... foreach(Control t in this.Controls) {  TextBox tb = t as TextBox;  if(tb != null && tb.Text == String.Empty)  {   // Do something here   break;  } }


                "When the only tool you have is a hammer, a sore thumb you will have."

                N 1 Reply Last reply
                0
                • P Philip Fitzsimons

                  this won't work - t is TextBox, t would still be a Control which does not have a .Text field so use "as". also why are you casting this.Controls? its a ControlCollection already... foreach(Control t in this.Controls) {  TextBox tb = t as TextBox;  if(tb != null && tb.Text == String.Empty)  {   // Do something here   break;  } }


                  "When the only tool you have is a hammer, a sore thumb you will have."

                  N Offline
                  N Offline
                  Nick Parker
                  wrote on last edited by
                  #9

                  Philip Fitzsimons wrote: this won't work Yes, it will, try it. Philip Fitzsimons wrote: also why are you casting this.Controls? its a ControlCollection already... Test your code, it will fail if you don't cast it, mine did, I previously wasn't casting it. :) -Nick Parker

                  P 1 Reply Last reply
                  0
                  • N Nick Parker

                    Roland Bär wrote: This argument lacks, if there are more text boxes in th esame form... Show me an example please. -Nick Parker

                    R Offline
                    R Offline
                    Roland Bar
                    wrote on last edited by
                    #10

                    Sorry, not precice enough ... :-O if you have the 20 textboxes, that should be tested, and then also other textboxes in the same form, that should not be checked, it is better to have the 20 textboxes in an own collection. Like this you can iterate over your collection without checking if it is a textbox to be checked or not. Hope it is clear what I mean. :~ greets Roland

                    N 1 Reply Last reply
                    0
                    • N Nick Parker

                      Philip Fitzsimons wrote: this won't work Yes, it will, try it. Philip Fitzsimons wrote: also why are you casting this.Controls? its a ControlCollection already... Test your code, it will fail if you don't cast it, mine did, I previously wasn't casting it. :) -Nick Parker

                      P Offline
                      P Offline
                      Philip Fitzsimons
                      wrote on last edited by
                      #11

                      I can't see who it would work - it won't work on my computer... "t" is a Control. "t.Text" is not valid on a Control. how does it compile?


                      "When the only tool you have is a hammer, a sore thumb you will have."

                      N 1 Reply Last reply
                      0
                      • R Roland Bar

                        Sorry, not precice enough ... :-O if you have the 20 textboxes, that should be tested, and then also other textboxes in the same form, that should not be checked, it is better to have the 20 textboxes in an own collection. Like this you can iterate over your collection without checking if it is a textbox to be checked or not. Hope it is clear what I mean. :~ greets Roland

                        N Offline
                        N Offline
                        Nick Parker
                        wrote on last edited by
                        #12

                        Roland Bär wrote: if you have the 20 textboxes, that should be tested, and then also other textboxes in the same form, that should not be checked, it is better to have the 20 textboxes in an own collection. Like this you can iterate over your collection without checking if it is a textbox to be checked or not. There are other ways around your problem, however my answer still identifies how to accomplish what he was asking for. -Nick Parker

                        1 Reply Last reply
                        0
                        • P Philip Fitzsimons

                          I can't see who it would work - it won't work on my computer... "t" is a Control. "t.Text" is not valid on a Control. how does it compile?


                          "When the only tool you have is a hammer, a sore thumb you will have."

                          N Offline
                          N Offline
                          Nick Parker
                          wrote on last edited by
                          #13

                          Philip Fitzsimons wrote: how does it compile? Drop my code onto a form, it works. :-D -Nick Parker

                          P 1 Reply Last reply
                          0
                          • F Frank Olorin Rizzi

                            Nick Parker wrote: Because then he would have to maintain that collection where as the Form already maintains a collection of all controls applied to it hmm.. yes, I see what you mean. ...speaking of which, let me ask this: If I picked the TextBox object that gets added to the Form's Controls collection (say it is named tBox1), and simply add it to my own collection (say an Hashtable) like so: myTable.Add(myTable.Count, tBox1); Wouldn't I be adding just a reference? Thus the impact on the memory would be relatively small? Or am I missing something here?

                            N Offline
                            N Offline
                            Nick Parker
                            wrote on last edited by
                            #14

                            There are many ways in which you can tackle this, depending on the exact circumstances you should consider using a specific method. My method is rather abstract. :) -Nick Parker

                            1 Reply Last reply
                            0
                            • N Nick Parker

                              Philip Fitzsimons wrote: how does it compile? Drop my code onto a form, it works. :-D -Nick Parker

                              P Offline
                              P Offline
                              Philip Fitzsimons
                              wrote on last edited by
                              #15

                              no it does not: error CS0117: 'System.Web.UI.Control' does not contain a definition for 'Text'


                              "When the only tool you have is a hammer, a sore thumb you will have."

                              N 1 Reply Last reply
                              0
                              • P Philip Fitzsimons

                                no it does not: error CS0117: 'System.Web.UI.Control' does not contain a definition for 'Text'


                                "When the only tool you have is a hammer, a sore thumb you will have."

                                N Offline
                                N Offline
                                Nick Parker
                                wrote on last edited by
                                #16

                                Philip Fitzsimons wrote: no it does not: Ah, I see you are doing this on an ASP.NET webform. This works on a Windows Form. ;) -Nick Parker

                                P 1 Reply Last reply
                                0
                                • N Nick Parker

                                  Philip Fitzsimons wrote: no it does not: Ah, I see you are doing this on an ASP.NET webform. This works on a Windows Form. ;) -Nick Parker

                                  P Offline
                                  P Offline
                                  Philip Fitzsimons
                                  wrote on last edited by
                                  #17

                                  :-D


                                  "When the only tool you have is a hammer, a sore thumb you will have."

                                  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