控件集合 [英] collection of controls

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

问题描述

我正在使用TableLayoutPanel并在运行时将不同的控件添加到它中
TableLayoutPanel1.Controls.Add(new Textbox());
现在我要添加控件,有多少控件以及如何获得该控件

i am using a TableLayoutPanel and add difrent controls are added to it at run time i e
TableLayoutPanel1.Controls.Add(new Textbox());
now i want wich control are added, how many and how to get that controls
any one can help me plz.

推荐答案

Controls属性是TableLayoutControlCollection类,因此您可以使用该类的方法和属性来解析控件的数量等.有关更多信息,请参见: http://msdn.microsoft.com/zh-CN /library/system.windows.forms.tablelayoutcontrolcollection.aspx [ ^ ]

TableLayout.Controls本身也可以与索引器一起使用,例如:
The Controls property is a TableLayoutControlCollection class so you can use the methods and properties of that class to resolve the amount of controls etc. For more info see: http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutcontrolcollection.aspx[^]

Also the TableLayout.Controls itself can be used with an indexer, like:
TableLayoutPanel1.Controls[counter]...


您可以使用TableLayoutPanel1.Controls.Count获取多少,并循环遍历TableLayoutpanel1.Controls集合,并尝试检查如下所示的控件类型,可以使用漂亮的开关来替换所有的if语句.
you can use TableLayoutPanel1.Controls.Count for how many, and loop around the TableLayoutpanel1.Controls collection, and try to check the control type like below, you can use nice switch to do replace all the if statements.
foreach(control ctrl in TableLayoutPanel1.Controls)
{
   if (ctrl is TextBox())
      TextBox objTxtBox = ctrl as TextBox();
if (ctrl is CheckBox())
      CheckBox objChkBox = ctrl as CheckBox();
}



[edit]添加了前置标记[/edit]



[edit] Added the pre tags[/edit]


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

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