可以通过
foreach (Control ctrl in this.Controls)
            {
                if (ctrl.Name = "btnOk")
                {
                    ctrl.Text = "login";
                }
            }
来遍历窗体中的控件——其他帖子看到的。
但是,我现在如何得到它是什么控件呢?比如“TEXT BOX”、“BUTTON”、“LABEL”呢?

解决方案 »

  1.   

    if (ctrl is TextBox)
    这样来判断
      

  2.   

    Sample code as follows:
    foreach (Control ctrl in this.Controls)
    {
           if (ctrl is TextBox )
          {
              //It is a textbox
           }
    }
      

  3.   


    foreach (Control ctrl in this.Controls)
    {
    string thiscontroltype = ctrl.GetType().ToString();
    //打印thiscontroltype出来看看就知道了
    }
      

  4.   

    顶Knight94(愚翁) ,就是通过IS来判断
      

  5.   

    this.Controls? 我现在如果抽象到另外一个类里面,应该不能够用THIS关键字吧?
    我现在有个FORM,名称为FORM1,但是我不能够使用FORM1.CONTROLS来实现,也不能用Form1.ControlCollection实现.怎么办?做个涵数:
    void function BrowseControls(Form frmTarget)
    {}一个通用函数,用来遍历传递给他FORM中的所有控件.怎么写?
      

  6.   

    你自己已经分析对了,
    void function BrowseControls(Form frmTarget)
    函数中对控件类需要穷举判断。