c#取项目名称-----和-----根据窗体上的控件名称取控件

来源:互联网 发布:淘宝热点乱接错位 编辑:程序博客网 时间:2024/05/16 15:13

取项目名称:

        static string AppName()
        {
            string fullstr = Assembly.GetExecutingAssembly().FullName;
            return fullstr.Substring(0, fullstr.IndexOf(","));
        }

 

根据窗体上的控件名称取控件

 

 

 

        public static Control GetControl(Control ctrl, string controlName)
        {
            Control tempControl = null;
            //if the input control's name equals the input controlName,return the control
            if (ctrl.Name == controlName)
            {
                tempControl = ctrl;
            }
            else if (ctrl.Controls.Count != 0)//if the ctrl is not suitable,get its sub controls
            {
                foreach (Control subCtrl in ctrl.Controls)
                {
                    Control tb = GetControl(subCtrl, controlName);
                    if (tb != null)
                    {
                        tempControl = tb;
                        break;
                    }
                }
            }
            return tempControl;
        }

 

-----------------------------------------------

例:引入代码

比如窗体上有N个Panel, 我想操作名称为"panel2"的Panel控件。

 

            Panel pa = (Panel)GetControl(this, "panel2");
            pa.Height = 100;

0 0