在我的项目(C# windows 应用程序)中,我有一个登录页面、mdiparent(Consist menustrips) 表单和 5-6 个子表单。 我想要的是,当用户填写登录凭据时,将显示父表单,其中一些菜单项根据其凭据被禁用或隐藏。
为了展示,我创建了一个父表单的参数化构造函数,并在其中传递了用户名和用户类型。
登录页面代码:
smsparent sp = new smsparent(usertype, username);
sp.Show();
this.Hide();
家长表格代码:
public smsparent(string usertype, string username)
{
InitializeComponent();
this.usertype= usertype;
this.username = username;
if (string.Compare(usertype,"Accountant") == 0)
{
administratorToolStripMenuItem.Enabled = false;
}
}
问题是 if block 没有被执行。我试过 .CompareTo()、Equals(),但都没有用。
最佳答案
如果你想比较你的字符串,试试
if(usertype == "Accountant")
{
administratorToolStripMenuItem.Enabled = false;
}
编辑
如果你想让你的比较更可靠一点,你可以这样做
if(usertype.Trim().ToLower() == "accountant")
{
administratorToolStripMenuItem.Enabled = false;
}
这将删除 usertype
之前或之后的所有空格,并将所有字母变为小写,然后与 "accountant"
关于c# - 根据登录凭证隐藏/显示菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33828459/