查找窗体界面中的控件,递归查找等控件处理问题,多使用于权限设置管理。

来源:互联网 发布:大学生旅游消费大数据 编辑:程序博客网 时间:2024/06/16 02:06

由于权限管理比较复杂一点。常用控件名称来设置来解决不同用户的权限问题。

读出本窗体里面的子控件的名称,不包括子控件的子控件。如要包括子控件,则应该采用递归recursive处理,
For Each ctl As Control In Me.Controls
Console.WriteLine(ctl.Name)
Next ctl
设置本窗体里面的所有标签控件且控件名称开头三个字母是lbl的标签控件的文本属性为空字符串,不包括子控件的子控件。
系统中大部分标签控件都是以lbl开头的,按钮则是btn开头。
For Each ctl As Control In Me.Controls
If (ctl.Name.StartsWith("lbl")) AndAlso (TypeOf ctl Is Label) Then
Dim lbl As Label = DirectCast(ctl, Label)
lbl.Text = ""
End If
Next ctl

部分代码来源于网络:From web

-----------------------------------------------------------------------
根据名称查找对应的工具栏控件:
private ToolStripMenuItem getToolStripMenuItemByName(string nameParam)
   {
      foreach (Control ctn in this.Controls)
         {
            if (ctn is ToolStripMenuItem)
               {
                   if (ctn.Name = nameParam)
                      {
                         return ctn;
                      }
                }
         }
         return null;
    }
-----------------------------------------------------------------------
动态设置相关控件的名称:
窗体中有一个文本框和按钮
txtName
btnClick
想要实现:
myForm.GetControl("txtName").text = "xxx"
myForm.GetControl("btnClick").text = "xxx"
根据名称查找对应的控件:
private Control FindControlByName(string name)
{
foreach (Control c in this.Controls) //assuming this is a Form
{
if (c.Name == name)
return c; //found
}
return null; //not found
}
如果控件太多, 你可以用Hashtable来存放你的控件,采用控件名做关键字。
在窗体加载时候或者其他事件:用哈希表格存储窗体的所有控件 ,不包括子控件的子控件。

this.controlHashtable = new HashTable();

foreach Control c in this.Controls
{
this.controlHashtable.Add(c.Name, c);
}

private Control GetControlByName(string name)
{
return this.controlHashtable[name] as Control;
}
-----------------------------------------------------------------------
查找控件的名称和索引值。
Control.ControlCollection coll = this.Controls;
  foreach(Control c in coll) {
if(c != null)
 Console.WriteLine(c.Text, "Index numb: " + coll.GetChildIndex(c, false));   
  }
-----------------------------------------------------------------------
显示所有控件名称:递归获取控件名称。
  To get all names of controls on a Form it has to be constructed first. The method to collect names should be recursive, something like this:
private static string GetAllControls(List<Form> forms)
{
if (forms == null)
return "null";
if (forms.Count==0)
return "empty list";
StringBuilder sb = new StringBuilder();
foreach (Form f in forms)
addControlsToBuilder(sb, "", f.Controls);
return sb.ToString();
}
private static void addControlsToBuilder(StringBuilder sb, string parent, Control.ControlCollection controls)
{
foreach (Control c in controls)
{
sb.Append(parent);
sb.Append(c.Name);
sb.AppendLine();
addControlsToBuilder(sb, parent + c.Name + ".", c.Controls);
}
}
(Note that I have avoided the use of static fields.)
建议用反射。
To get all controls of all forms in an assembly without constructing the forms I would use reflection.
-----------------------------------------------------------------------
Something like this might do it.  This is a recursive procedure that will pass the list of controls within itself until it gets all the controls you want.
加载所有控件到一个list里面,采用递归函数。


Code Snippet


private void GetControls(Control topControl, List<Control> list)
{
foreach (Control control in topControl.Controls)
{
if (control is Button)
{ //No problem
}
if (control is TextBox)
{ //No problem
}
if (control is Label)
{ //No problem
}
// if control is TabControl
if (control is TabControl)
{
//Have problem here every tabcontrol have pages and every pages have controls
foreach (TabPage tp in ((TabControl)control).TabPages)
{
GetControls(tp, list);
}
}
// if control is GroupBox then have problem, too
if (control is GroupBox)
{
GetControls(control, list);
}
}
}
 
You would call this void by calling:
 
Code Snippet
List<Control> myControls = new List<Control>();
GetControls(this, myControls);
 
// then use your myControls list however you want.
-----------------------------------------------------------------------