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

后端 未结 14 1512
野的像风
野的像风 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:53

    Just add other control types:

    public static void ClearControls(Control c)
    {
    
        foreach (Control Ctrl in c.Controls)
        {
            //Console.WriteLine(Ctrl.GetType().ToString());
            //MessageBox.Show ( (Ctrl.GetType().ToString())) ;
            switch (Ctrl.GetType().ToString())
    
            {
                case "System.Windows.Forms.CheckBox":
                    ((CheckBox)Ctrl).Checked = false;
                    break;
    
                case "System.Windows.Forms.TextBox":
                    ((TextBox)Ctrl).Text = "";
                    break;
    
                case "System.Windows.Forms.RichTextBox":
                    ((RichTextBox)Ctrl).Text = "";
                    break;
    
                case "System.Windows.Forms.ComboBox":
                    ((ComboBox)Ctrl).SelectedIndex = -1;
                    ((ComboBox)Ctrl).SelectedIndex = -1;
                    break;
    
                case "System.Windows.Forms.MaskedTextBox":
    
                    ((MaskedTextBox)Ctrl).Text = "";
                    break;
    
                case "Infragistics.Win.UltraWinMaskedEdit.UltraMaskedEdit":
                    ((UltraMaskedEdit)Ctrl).Text = "";
                    break;
    
                case "Infragistics.Win.UltraWinEditors.UltraDateTimeEditor":
                    DateTime dt = DateTime.Now;
                    string shortDate = dt.ToShortDateString();
                    ((UltraDateTimeEditor)Ctrl).Text = shortDate;
                    break;
    
                case "System.Windows.Forms.RichTextBox":
                    ((RichTextBox)Ctrl).Text = "";
                    break;
    
    
                case " Infragistics.Win.UltraWinGrid.UltraCombo":
                    ((UltraCombo)Ctrl).Text = "";
                    break;
    
                case "Infragistics.Win.UltraWinEditors.UltraCurrencyEditor":
                    ((UltraCurrencyEditor)Ctrl).Value = 0.0m;
                    break;
    
                default:
                    if (Ctrl.Controls.Count > 0)
                        ClearControls(Ctrl);
                    break;
    
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-11-28 08:57

    I found this to work very well, but initially I have my textboxes on a panel so none of my textboxes cleared as expected so I added

    this.panel1.Controls.....  
    

    Which got me to thinking that is you have lots of textboxes for different functions and only some need to be cleared out, perhaps using the multiple panel hierarchies you can target just a few and not all.

    foreach ( TextBox tb in this.Controls.OfType<TextBox>()) {
      ..
    }
    
    
    0 讨论(0)
提交回复
热议问题