winform查找控件
/// <summary> /// 查找指定类型控件 /// </summary> /// <typeparam name="T">待查找控件类型</typeparam> /// <param name="ctl">控件所在容器</param> /// <param name="act">找到后执行的操作</param> private void FindCtl<T>(Control ctl, Action<T> act) where T : Control { T tmp = null; Stack<Control> ctls = new Stack<Control>(); for (int i = 0; i < ctl.Controls.Count; i++) { if (ctl.Controls[i].Controls.Count > 0 || (ctl.Controls[i] as T) != null) { ctls.Push(ctl.Controls[i]); } } do { Control tmpCtl = ctls.Pop(); tmp = tmpCtl as T; if (tmp == null && tmpCtl.Controls.Count > 0) { for (int i = 0; i < tmpCtl.Controls.Count; i++) { ctls.Push(tmpCtl.Controls[i]); } } if (tmp != null) { act(tmp); } } while (ctls.Count > 0); }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步