本文介绍了浏览所有文本框,组合框...并清除其内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何遍历所有控件,而不管它们位于面板或组框中,...
现在我有了这样的东西:
我添加了另一个类,并且在该类中是代码:

How to loop through all the controls regardless of them being on a panel or groupbox,...
Now i have something like this:
I added another class and in this class is the code:

 public void New(Form1 f1)
        {
 foreach (Control c in f1.Controls)
            {

                if (c is TextBox || c is ComboBox)
                {
                    c.Text = "";
                }
}

            }


推荐答案

foreach(Control ctrl in this.Controls)
{
    if(ctrl is TextBox)
        string str = ((TextBox)ctrl).Text;

    if(ctrl is ComboBox)
        string str = ((ComboBox) ctrl).SelectedItem.ToString();

    // and so on...
}


-KR


-KR


// requires Linq

// clear Text in all TextBoxes
foreach (TextBox theTextBox in (SpecialMethods.GetAllControls(this)).OfType<TextBox>().ToList())
{
    theTextBox.Clear();
}

// clear ComboBoxItems in all ComboBoxes
foreach (ComboBox theComboBox in (SpecialMethods.GetAllControls(this)).OfType<ComboBox>().ToList())
{
    theComboBox.Items.Clear();
}


foreach (Control objControl in this.Controls)
            {

                if (objControl.GetType() == typeof(TextBox))
                {
                    TextBox objTxt = (TextBox)objControl;
                    objTxt.Text = "";
                }
            }




尝试以其他方式进行其他控制.
谢谢




Try others control with same way.
Thanks


这篇关于浏览所有文本框,组合框...并清除其内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 15:49
查看更多