C#遍曆菜單並設定許可權

來源:互聯網
上載者:User

轉載自 http://hi.baidu.com/grounder/blog/item/ad0755ca7121d815bf09e624.html

 

一、加入資料1、建立資料表
create table t_GroupFormsAuthority(
--FGfaID numeric(6,0) identity primary key ,
FUGroupNo varchar(50) not null ,
FSFormNo varchar(100) not null ,--系統視窗、報表代碼
FGfaIsOk char(1) default '1' ,
FStatus char(1) default '1',---
FFlag char(1) default '1',---
FCreateDate datetime default getdate() ,
FCreator varchar(20) null ,
FModifyDate datetime default getdate() ,
FModifier varchar(20) null ,
FMemo text null ,
constraint PK_t_GroupFormsAuthority primary key(FUGroupNo,FSFormNo)
)
go
2、插入資料
Admin MenuItemBase 1 1
Admin MenuItemBase_01 1 1
Admin MenuItemFile 1 1
Admin MenuItemFile_Exit 1 1
Admin MenuItemFile_Print 1 1
Admin MenuItemHelp 1 1
Admin MenuItemWindow 1 1

 

二、建立項目1、介面,加入功能表項目並命名為:menuMain2、程式碼:建立代碼檔案:setMenu.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using DBClassLib;namespace ERP
{
public class SetMenu
{
private MenuStrip msMain; /// <summary>
/// 建構函式
/// </summary>
/// <param name="mMain"></param>
public SetMenu(MenuStrip mMain)
{
msMain = mMain;
msMain.Visible = true;

} /// <summary>
/// 設定菜單許可權
/// </summary>
public void SetMenuPower()
{
string strSql;
int mItemCount, i, j;
mItemCount = msMain.Items.Count;
strSql = "SELECT FSFormNo,FGfaIsOk FROM t_GroupFormsAuthority WHERE FStatus='1' AND FUGroupNo='" + Global.userInfo[2] + "'";
DBOperator db = DBOperator.GetDBOperator(Global.strConnection);
DataTable dt = new DataTable();
db.Open();
dt = db.GetDataTable(strSql);
if (dt.Rows.Count > 0)
{
for (i = 0; i < mItemCount; i++)
{
for (j = 0; j < dt.Rows.Count; j++)
{
if ((msMain.Items[i].Name.ToString() == dt.Rows[j]["FSFormNo"].ToString()) && (dt.Rows[j][1].ToString() == "1"))
{
SetMenuItemUsability(msMain.Items[i], true);
GetSubMenu(dt,(ToolStripMenuItem) msMain.Items[i]);
break;
}
else
{
SetMenuItemUsability(msMain.Items[i], false);
}
}
}
}
else
{
msMain.Visible = false;
} db.Close(); } private void GetSubMenu(DataTable dt, ToolStripMenuItem tsmItem)//ToolStripItem mItems)
{
//ToolStripMenuItem tsmItem=new ToolStripMenuItem() ;
int i,j,mItemsCount;
mItemsCount = tsmItem.DropDownItems.Count; if (mItemsCount > 0)
{
for (i = 0; i < mItemsCount; i++)
{
for (j = 0; j < dt.Rows.Count; j++)
{
if (tsmItem.DropDownItems[i].Name == dt.Rows[j][0].ToString() && dt.Rows[j][1].ToString() == "1")
{
SetMenuItemUsability(tsmItem.DropDownItems[i], true);
break; }
else
{
SetMenuItemUsability(tsmItem.DropDownItems[i], false);
}
}
}
}
} /// <summary>
/// 設定菜單可用性

 

3、主視窗調用:
SetMenu sm = new SetMenu(menuMain);
sm.SetMenuPower();

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.