window forms遍历窗体所有控件
2011-10-21 13:15
253 查看
/// <summary>
/// 只遍历控件的子控件,不遍历孙控件
///当控件有子控件时,需要用递归的方法遍历,才能全部列出控件上的控件
/// </summary>
/// <typeparam name="T">要匹配的控件类型</typeparam>
/// <param name="control">要遍历的了控件</param>
/// <param name="controlsName">要匹配的控件名</param>
/// <returns></returns>
public static Control GetControl<T>(Control control, string controlsName)
{
if (control == null) return null;
Control _control;
for (int i = 0; i < control.Controls.Count; i++)
{
_control = control.Controls[i];
if (_control == null) return null;
if (_control.Name == controlsName && _control is T)
return _control;
if (_control.HasChildren)
{
_control = GetControl<T>(_control, controlsName);
if (_control != null)
return _control;
}
}
return null;
}
/// <summary>
/// 遍历窗体所有控件
/// </summary>
/// <typeparam name="T">要匹配的控件类型</typeparam>
/// <param name="form">窗体名</param>
/// <param name="controlsName">要匹配的控件名</param>
/// <returns></returns>
public static Control GetControl<T>(Form form, string controlsName)
{
Control _Control = null;
for (int i = 0; i < form.Controls.Count; i++)
{
_Control = GetControl<T>(form.Controls[i], controlsName);
if (_Control != null)
return _Control;
}
return null;
}
使用方法:
Control _control;
_control = GetControl<TextBox>(customer, "txtValue");
if(_control!=null)
((TextBox) _control).Text = "text";
_control = GetControl<ComboBox>(customer, "ddl");
if (_control != null)
((ComboBox)_control).SelectedIndex = 0;
/// 只遍历控件的子控件,不遍历孙控件
///当控件有子控件时,需要用递归的方法遍历,才能全部列出控件上的控件
/// </summary>
/// <typeparam name="T">要匹配的控件类型</typeparam>
/// <param name="control">要遍历的了控件</param>
/// <param name="controlsName">要匹配的控件名</param>
/// <returns></returns>
public static Control GetControl<T>(Control control, string controlsName)
{
if (control == null) return null;
Control _control;
for (int i = 0; i < control.Controls.Count; i++)
{
_control = control.Controls[i];
if (_control == null) return null;
if (_control.Name == controlsName && _control is T)
return _control;
if (_control.HasChildren)
{
_control = GetControl<T>(_control, controlsName);
if (_control != null)
return _control;
}
}
return null;
}
/// <summary>
/// 遍历窗体所有控件
/// </summary>
/// <typeparam name="T">要匹配的控件类型</typeparam>
/// <param name="form">窗体名</param>
/// <param name="controlsName">要匹配的控件名</param>
/// <returns></returns>
public static Control GetControl<T>(Form form, string controlsName)
{
Control _Control = null;
for (int i = 0; i < form.Controls.Count; i++)
{
_Control = GetControl<T>(form.Controls[i], controlsName);
if (_Control != null)
return _Control;
}
return null;
}
使用方法:
Control _control;
_control = GetControl<TextBox>(customer, "txtValue");
if(_control!=null)
((TextBox) _control).Text = "text";
_control = GetControl<ComboBox>(customer, "ddl");
if (_control != null)
((ComboBox)_control).SelectedIndex = 0;
相关文章推荐
- 遍历窗体上所有CheckBox控件 .net winform
- 遍历窗体中的所有TextBox控件,并为其赋值
- 遍历所有窗体控件
- 遍历winform窗体上的所有控件(包括DevExpress.XtraEditors控件)且对其做相应的处理
- 循环遍历获取窗体或页面所有同类型的控件
- WinForm遍历窗体所有子控件的方法
- C#遍历窗体控件代码,遍历窗体所有按钮控件代码
- MFC遍历窗体所有的控件
- c#WinForm下窗体权限设计(遍历菜单下的所有菜单及所有form、form中的控件)
- 遍历当前窗体下的所有控件
- c#遍历form窗体上的所有控件
- Vb.net遍历一个窗口中的所有某类对象 (窗体中的控件) 的方法
- 再谈如何遍历Asp.net窗体下所有的控件
- c#WinForm下窗体权限设计(遍历菜单下的所有菜单及所有form、form中的控件)
- C#遍历窗体所有控件或某类型所有控件
- 遍历窗体上所有控件(遍历子控件)
- 用递归的方式遍历窗体下的所有控件
- 关于类的入门例子(8): 遍历窗体中所有控件的函数
- wpf遍历窗体中所有控件方法
- 功能: 遍历窗体内部所有的控件 (学习日记 2017-07-06)