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.
  • 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