您的位置:首页 > 编程语言 > C#

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

2010-11-16 15:52 337 查看
取项目名称:

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐