Foreach Control in form, how can I do something to all the TextBoxes in my Form?

后端 未结 14 1513
野的像风
野的像风 2020-11-28 08:13

How can I use a Foreach Statement to do something to my TextBoxes?

foreach (Control X in this.Controls)
{
    Check if the controls is a TextBox, if it is de         


        
相关标签:
14条回答
  • 2020-11-28 08:39

    You can do the following:

    foreach (Control X in this.Controls)
    {
        TextBox tb = X as TextBox;
        if (tb != null)
        {
            string text = tb.Text;
            // Do something to text...
            tb.Text = string.Empty; // Clears it out...
        }
    }
    
    0 讨论(0)
  • 2020-11-28 08:39

    Even better, you can encapsule this to clear any type of controls you want in one method, like this:

    public static void EstadoControles<T>(object control, bool estado, bool limpiar = false) where T : Control
            {
                foreach (var textEdits in ((T)control).Controls.OfType<TextEdit>()) textEdits.Enabled = estado;
                foreach (var textLookUpEdits in ((T)control).Controls.OfType<LookUpEdit>()) textLookUpEdits.Enabled = estado;
    
                if (!limpiar) return;
                {
                    foreach (var textEdits in ((T)control).Controls.OfType<TextEdit>()) textEdits.Text = string.Empty;
                    foreach (var textLookUpEdits in ((T)control).Controls.OfType<LookUpEdit>()) textLookUpEdits.EditValue = @"-1";
                }
            }
    
    0 讨论(0)
  • 2020-11-28 08:44

    If you are using C# 3.0 or higher you can do the following

    foreach ( TextBox tb in this.Controls.OfType<TextBox>()) {
      ..
    }
    

    Without C# 3.0 you can do the following

    foreach ( Control c in this.Controls ) {
      TextBox tb = c as TextBox;
      if ( null != tb ) {
        ...
      }
    }
    

    Or even better, write OfType in C# 2.0.

    public static IEnumerable<T> OfType<T>(IEnumerable e) where T : class { 
      foreach ( object cur in e ) {
        T val = cur as T;
        if ( val != null ) {
          yield return val;
        }
      }
    }
    
    foreach ( TextBox tb in OfType<TextBox>(this.Controls)) {
      ..
    }
    
    0 讨论(0)
  • 2020-11-28 08:46

    Check this:

    foreach (Control x in this.Controls)
    {
        if (x is TextBox)
        {
            x.Text = "";
        }
    }
    
    0 讨论(0)
  • 2020-11-28 08:47
    foreach (Control X in this.Controls)
    {
      if (X is TextBox)
      {
        (X as TextBox).Text = string.Empty;
      }
    }
    
    0 讨论(0)
  • 2020-11-28 08:50

    simple using linq, change as you see fit for whatever control your dealing with.

            private void DisableButtons()
        {
            foreach (var ctl in Controls.OfType<Button>())
            {
                ctl.Enabled = false;
            }
        }
    
        private void EnableButtons()
        {
            foreach (var ctl in Controls.OfType<Button>())
            {
                ctl.Enabled = true;
            }
        }
    
    0 讨论(0)
提交回复
热议问题