你所有的操作都是在组织一个字符串当然不行了.
试试下面的代码
DataTable dtPer = GetUserPermission(frmLogin.userName);
foreach(Control ctl in this.Controls)
{
for(int i = 0; i < dtPer.Rows.Count; i++)
{
if(ctl.Name == dtPer.Rows[i][1].ToString())
{
ctl.Enabled = false;
}
}
}
试试下面的代码
DataTable dtPer = GetUserPermission(frmLogin.userName);
foreach(Control ctl in this.Controls)
{
for(int i = 0; i < dtPer.Rows.Count; i++)
{
if(ctl.Name == dtPer.Rows[i][1].ToString())
{
ctl.Enabled = false;
}
}
}
解决方案 »
- 请教,ListBox中有没有一个可以侦测到Items.count发生变化后的引发事件
- C# 读取xml
- 不能上传视频文件
- SQL调用外部的dll(用C#生成的)
- 如何比较时间大小
- 简单问题,请问这个xml XPath 寻找路径怎么写??内有xml的具体表示文档。
- 查询数据库时,DataSet和Output输出参数,哪个更快?
- 根据文档内容(图片、Word、Txt、PPT、Video)制作文档缩略图
- 关于web应用程序连接SQL Server 数据库的问题,等待解答!
- 如何用c#关闭win2000系统的功能
- cs bs程序中是否都可以用一个Connection
- 怎么使用C#在应用程序间发送消息?
DataTable dtPer = GetUserPermission(frmLogin.userName);
bool flag = false;
for(int i = 0; i < dtPer.Rows.Count; i++)
{
string permission = dtPer.Rows[i][1].ToString();
object obj=this.GetType().GetField(permission ).GetValue(this);
if (obj !=null)
{
Button bt=obj as Button;
bt.Enabled = flag ;
}
}
原来的for(int i = 0; i < dtPer.Rows.Count; i++)
循环的是DataTable的行数和,这里要换成所有控件的数量和。怎么得到界面上所有控件的数量呢?
this.Controls.Count
foreach (Control c in SomeForm.Controls)
{
c.GetType().Name
}
{
if(ctl is TextBox)
{
//do something
} }
object obj=this.GetType().GetField("button1").GetValue(this);
if (obj !=null)
{
if(obj as System.Windows.Forms.Control)
{
Control ctl=obj as Control;
ctl.Enabled = false;
}
else if(obj as System.Windows.Forms.MenuItem)
{
MenuItem mnu=obj as MenuItem;
mnu.Visible = false;
}
}
DataTable dtPer = GetUserPermission(frmLogin.userName);
bool flag = false;
for(int i = 0; i < dtPer.Rows.Count; i++)
{
string permission = dtPer.Rows[i][1].ToString();
object obj=this.GetType().GetField(permission ).GetValue(this);
if(obj as System.Windows.Forms.Control)
{
Control ctl=obj as Control;
ctl.Enabled = false;
}
else if(obj as System.Windows.Forms.MenuItem)
{
MenuItem mnu=obj as MenuItem;
mnu.Visible = false;
}
}
if(Convert.ToBoolean(obj as System.Windows.Forms.Control))else if(Convert.ToBoolean(obj as System.Windows.Forms.MenuItem))其他没变。编译动过了,可运行到
object obj=this.GetType().GetField(permission ).GetValue(this);的时候,报错:
{"未将对象引用设置到对象的实例。" } System.NullReferenceException
从Form.Controls中只能获得控件的容器就是Form的控件,其他panel中的控件你得不到, 但可以使用递归法,得到Control.Controls的所有控件,请注意 ,MenuItem,和ToolBarButton还是得不到.其他问题,参考你的贴http://community.csdn.net/Expert/topic/3738/3738857.xml?temp=.3866388以下这个是获得窗口中所有控件的方法,包括MenuItem,ToolBarButton,Panel中的子控件public static System.Collections.ArrayList GetChildControls(System.Windows.Forms.Control ctrl )
{
System.Collections.ArrayList list=new System.Collections.ArrayList ();
Type tp=ctrl.GetType ();
MemberInfo [] members=tp.GetMembers (BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Static );
foreach (MemberInfo mbm in members)
{
object o=tp.InvokeMember (mbm.Name ,BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Static
| BindingFlags.GetProperty | BindingFlags.GetField ,null,ctrl,null);
if (o is System.Windows.Forms.Control)
list.Add (o);
}
return list;
}
"你所有的操作都是在组织一个字符串当然不行了."不要误人子弟啊,你用的办法是最笨的
不要说只操作一个"Enabled"字串,
用字串创建类,表单,获得程序集中的任一个类,类的成员,执行方法,委托事件,而且无论是公有的还是私有的,有哪样.net做不到的
你用记事本写一段C#代码,描述窗口对象也好,类对象也好,都可以在在程序运行时完完整整地得到你描述的对象, 不信么?