[英]hide/show menu item based on logon credential
在我的項目(C#Windows應用程序)中,我有一個登錄頁面,mdiparent(由Consistist 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塊沒有被執行。 我已經嘗試過.CompareTo(),Equals(),但都沒有工作。
如果要比較字符串,請嘗試
if(usertype == "Accountant")
{
administratorToolStripMenuItem.Enabled = false;
}
編輯
如果您想提高比較的可靠性,可以這樣做
if(usertype.Trim().ToLower() == "accountant")
{
administratorToolStripMenuItem.Enabled = false;
}
這會前或后刪除任何空格usertype
,也為所有字母為小寫字母,然后比較,以"accountant"
if (String.Equals(usertype,"Accountant"))
確保您在此方法中進展順利(日志)
我將首先檢查usertype是否不為null,然后進行相等檢查。
if (!String.IsNullOrEmpty(usertype) && String.Equals(usertype.ToLower(),"accountant"))
{
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.