- private void button2_Click(object sender, EventArgs e)
- {
- Control col = GetControl(panel1, "button3");
- Button btn = (Button)col;
- btn.Text = "成功";
- }
- /// <summary>
- /// 通过控件名获取控件
- /// </summary>
- /// <param name="strName"></param>
- /// <returns></returns>
- private Control GetPbControl(string strName)
- {
- string pbName = strName;
- return GetControl(this, pbName);
- }
- /// <summary>
- /// 通过控件名获取控件
- /// </summary>
- /// <param name="ct">控件所在的容器或者窗体</param>
- /// <param name="name">需要查找的控件名</param>
- /// <returns></returns>
- public static Control GetControl(Control ct, string name)
- {
- Control[] ctls = ct.Controls.Find(name, false);
- if (ctls.Length > 0)
- {
- return ctls[0];
- }
- else
- {
- return null;
- }
- }
复制代码
凑合着看下吧,没有判断控件为不为空,如果不判断,可能会异常掉,所以,大家自己加上吧
|