How to loop through textboxes?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brett Romero

    #1

    How to loop through textboxes?

    I'd like to loop through all text boxes on a winform and check if they
    are empty.

    private bool emptyTextBoxes( )
    {
    bool isEmpty = true;

    foreach(Control ctl in Controls)
    {
    if (typeof(ctl) == TextBox) //error here
    if(ctl.Text != "")
    isEmpty = false;
    }
    return isEmpty
    }

    I get this error:
    The type or namespace name 'ctl' could not be found (are you missing a
    using directive or an assembly reference?)

    ctl is a typeof Control and I want to make sure it is a TextBox. Why
    is this wrong?

    Thanks,
    Brett

  • Bruce Wood

    #2
    Re: How to loop through textboxes?

    Two things.

    First of all, typeof() converts a _class name_ into a Type object, so
    it's expecting a type name there, for example typeof(TextBox) , not a
    variable.

    In order to get the type of the object referenced by a variable, call
    the GetType() method:

    ctl.GetType()

    Second, there is a much better construct for what you're trying to do:

    foreach(Control ctl in Controls)
    {
    TextBox box = ctl as TextBox;
    if (box != null)
    {
    if (box.Text.Lengt h > 0)
    {
    isEmpty = false;
    }
    }
    }

    Comment

    • Michael Bray

      #3
      Re: How to loop through textboxes?

      "Bruce Wood" <brucewood@cana da.com> wrote in news:1137027138 .003491.127660
      @g43g2000cwa.go oglegroups.com:
      [color=blue]
      > Second, there is a much better construct for what you're trying to do:
      >
      > foreach(Control ctl in Controls)
      > {
      > TextBox box = ctl as TextBox;
      > if (box != null)
      > {
      > if (box.Text.Lengt h > 0)
      > {
      > isEmpty = false;
      > }
      > }
      > }
      >
      >[/color]

      I would also add that you might want to add some recursion to check child
      controls... things like groupBoxes and Panels.

      public void DoSomething(Tex tBox tb)
      {
      if (tb.Text.Length == 0)
      {
      ...
      }
      }

      public void CheckTextBoxes( ControlCollecti on ctrls)
      {
      if (ctrls == null) return;
      foreach(Control c in ctrls)
      {
      if (c is TextBox) DoSomething((Te xtBox)c);
      else CheckTextBoxes( c.Controls);
      }
      }

      -mdb

      Comment

      • u7djo@hotmail.com

        #4
        Re: How to loop through textboxes?

        Change:

        if (typeof(ctl) == TextBox)

        to

        if (ctl.GetType() == typeof(TextBox) )

        and it should work fine.

        Dave

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: How to loop through textboxes?

          u7djo@hotmail.c om wrote:[color=blue]
          > Change:
          >
          > if (typeof(ctl) == TextBox)
          > to
          > if (ctl.GetType() == typeof(TextBox) )
          >
          > and it should work fine.[/color]

          Well, it'll work subtly differently to using "as" or "is". The above
          would return false if you had a control which was derived from TextBox.
          Now, it could be that that's what's actually wanted - but it usually
          isn't.

          Using as/is is generally preferrable, IMO.

          Jon

          Comment

          Working...