c# - 根据登录凭证隐藏/显示菜单项

标签 c# winforms

在我的项目(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/

相关文章:

c# - 如何过滤包含数字的参数? C# 网络

c# - 在 C# 中,如何从 C++/CLI 类中引用 typedef

C# 将 Excel 图表作为对象(而不是图片)导出到 Word

c# - 尝试使用 Dialogflow V2 API 中的自定义事件更新意图参数,但它没有传递到意图

c# - 未显示窗体时,窗体的保存位图/"screenshot"不起作用

c# - 使用 C# 从 C# 中退出类

c# - C#中的错误消息

c# - 带有 “Yes” 的消息框,C# 中的 “No” 选择?

c# - 为什么将 LinearGradientBrush.WrapMode 设置为 Clamp 会因 ArgumentException ("parameter is not valid"失败)?

c# - 如何防止程序截屏