一、实现效果
在 Winform中窗体(Form)就像一个容器,可以包含所有的控件(比如:panel、button、textedit等控件);如果把窗体看做是 Winform 中最大的容器,那么Panel可以看做第二,Panel控件则是专门用于界面的排版布局,相当于 Web 开发中的层(div),几乎所有控件都可以包含到 Panel 中,包括窗体。
①获取到指定窗体下的所有控件;
②获取到指定Panel下的所有控件;
③获取到窗体下的所有TextEdit类型控件;
④根据控件名称获取到该控件;
二、实现方法
/// <summary>
/// 获取指定窗体或面板下的所有控件
/// </summary>
/// <typeparam name="T">控件类型</typeparam>
/// <param name="containChildControlWindow">包含子控件的窗体(比如窗体是[Controls]、panel是[panel.controls])</param>
/// <returns></returns>
private Stack<Control> GetAllControlOfWindows(Control.ControlCollection containChildControlWindow)
{
Stack<Control> tmpStack = new Stack<Control>();
foreach (Control control in containChildControlWindow)
{
tmpStack.Push(control);
}
return tmpStack;
}
/// <summary>
/// 获取指定窗体或面板下的指定类型的控件
/// </summary>
/// <typeparam name="T">控件类型</typeparam>
/// <param name="containChildControlWindow">包含子控件的窗体(比如窗体是[Controls]、panel是[panel.controls])</param>
/// <returns></returns>
private Stack<T> GetAppointControlOfWindows<T>(Control.ControlCollection containChildControlWindow)
{
Stack<T> tmpStack = new Stack<T>();
foreach (var control in containChildControlWindow)
{
if (control is T)
{
tmpStack.Push((T)control);
}
}
return tmpStack;
}
/// <summary>
/// 通过控件名称获取到控件
/// </summary>
/// <param name="controlName">控件名称</param>
/// <param name="containChildControlWindow">包含子控件的窗体(比如窗体是[Controls]、panel是[panel.controls])</param>
/// <returns></returns>
private Control GetControlOfName(string controlName, Control.ControlCollection containChildControlWindow)
{
if (string.IsNullOrEmpty(controlName)) return null;
foreach (Control item in containChildControlWindow)
{
if (item.Name.Equals(controlName))
{
return item;
}
}
return null;
}
三、调用方法
//获取到窗体下包含的所有控件
private void btn_getAllControlOfWindow_Click(object sender, EventArgs e)
{
Stack<Control> controls = GetAllControlOfWindows(this.Controls);
ShowControlInfo(controls);
}
//获取到Panel下包含的所有控件
private void btn_GetAllControlOfPanel_Click(object sender, EventArgs e)
{
Stack<Control> controls = GetAllControlOfWindows(this.panel1.Controls);
ShowControlInfo(controls);
}
//获取窗体下的所有TextEdit类型控件
private void btn_GetAllTextEditControlOfWindow_Click(object sender, EventArgs e)
{
Stack<TextEdit> controls = GetAppointControlOfWindows<TextEdit>(this.Controls);
ShowControlInfo(controls);
}
//根据输入的控件名称获取控件
private void btn_GetControlOfName_Click(object sender, EventArgs e)
{
//通过控件名称获取控件
string inputControlName = textEdit1.Text.Trim();
Control control = GetControlOfName(inputControlName, this.Controls);
if (control!=null)
{
string strInfo = $"名称为【simpleButton1】的控件信息如为:{control.Name} {control.Text} {control.Size} {control.Enabled = false}";
ShowInfo(strInfo);
}
}
/// <summary>
/// 显示控件信息
/// </summary>
/// <typeparam name="T">控件类型</typeparam>
/// <param name="controlList">控件列表</param>
private void ShowControlInfo<T>(Stack<T> controlList)
{
if (controlList == null || controlList.Count <= 0)
return;
string strTmp = string.Empty;
foreach (var item in controlList)
{
if (item is Control)
{
Control control = item as Control;
strTmp += $"控件名称【{control.Name}】 控件描述【{control.Text}】" + "\r\n";
}
}
strTmp += $"\r\n 共有:{controlList.Count} 个控件";
ShowInfo(strTmp);
}
/// <summary>
/// 显示信息
/// </summary>
/// <param name="info">信息</param>
private void ShowInfo(string info)
{
memoEdit_ShowInfo.Text = info;
}
标签:control,控件,获取,窗体,Control,Stack From: https://www.cnblogs.com/5miyuan/p/17168150.html