枚举表单中的所有控件 [英] Enumerate all controls in the form

查看:131
本文介绍了枚举表单中的所有控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void EnableControls(bool enable)
        {
            foreach (TextBox t in Page.Form.Controls.OfType<TextBox>())
            {
                t.ReadOnly = !enable;
            }
            chkSameAsCurrent.Enabled = enable;
        }

以上code正常运行在没有任何主页一个简单的页面,但如果我在一个ContentPage运行它,我不能一一列举的文本框,甚至没有在任何形式的控制。

The above code runs fine in a simple page not having any master page, but if I run it in a ContentPage I can not enumerate the TextBoxes and not even any control in the form.

推荐答案

试试这个。我想,这应该工作。

Try this. I think this should work.

 private void RecursiveLoopThroughControls(Control root)
 {
      foreach (Control control in root.Controls)
      {
          RecursiveLoopThroughControls(control);
          //process the control.
      }
 }

使用调用方法

Call the method using

 RecursiveLoopThroughControls(Page)

这篇关于枚举表单中的所有控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆