如何使用 Foreach 语句对我的文本框执行某些操作?
foreach (Control X in this.Controls)
{
Check if the controls is a TextBox, if it is delete it's .Text letters.
}
如果您使用 C# 3.0 或更高版本,您可以执行以下操作
foreach ( TextBox tb in this.Controls.OfType<TextBox>()) {
..
}
如果没有 C# 3.0,您可以执行以下操作
foreach ( Control c in this.Controls ) {
TextBox tb = c as TextBox;
if ( null != tb ) {
...
}
}
或者更好的是,用 C# 2.0 编写 OfType。
public static IEnumerable<T> OfType<T>(IEnumerable e) where T : class {
foreach ( object cur in e ) {
T val = cur as T;
if ( val != null ) {
yield return val;
}
}
}
foreach ( TextBox tb in OfType<TextBox>(this.Controls)) {
..
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)