首页 > 代码库 > [WinForm]FindControl

[WinForm]FindControl

关键代码:

        /// <summary>        /// 按名称查找控件        /// </summary>        /// <param name="parentControl">查找控件的父容器控件</param>        /// <param name="findCtrlName">查找控件名称</param>        /// <returns>若没有查找到返回NULL</returns>        public static Control FindControl(this Control parentControl, string findCtrlName)        {            Control _findedControl = null;            if (!string.IsNullOrEmpty(findCtrlName) && parentControl != null)            {                foreach (Control ctrl in parentControl.Controls)                {                    if (ctrl.Name.Equals(findCtrlName))                    {                        _findedControl = ctrl;                        break;                    }                }            }            return _findedControl;        }        /// <summary>        /// 将Control转换某种控件类型        /// </summary>        /// <typeparam name="T">控件类型</typeparam>        /// <param name="control">Control</param>        /// <param name="result">转换结果</param>        /// <returns>若成功则返回控件;若失败则返回NULL</returns>        public static T Cast<T>(this Control control, out bool result) where T : Control        {            result = false;            T _castCtrl = null;            if (control != null)            {                if (control is T)                {                    try                    {                        _castCtrl = control as T;                        result = true;                    }                    catch (Exception ex)                    {                        Debug.WriteLine(string.Format("将Control转换某种控件类型异常,原因:{0}", ex.Message));                        result = false;                    }                }            }            return _castCtrl;        }    }
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>

<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>测试代码:

            bool _sucess = false;            CheckBox _finded = panel1.FindControl("checkBox1").Cast<CheckBox>(out _sucess);            if (_sucess)            {                MessageBox.Show(_finded.Name);            }            else            {                MessageBox.Show("Not Finded.");            }
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>

希望有所帮助!<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>