如何通过控制菜单的enabled属性而控制用户的权限?(100分)

  • 主题发起人 主题发起人 xueyong
  • 开始时间 开始时间
X

xueyong

Unregistered / Unconfirmed
GUEST, unregistred user!
我是通过控制主控窗体菜单的enabled属性控制用户权限的,
下面的一段代码也通过了编译,
但是通过用户登录窗体登录后主控的菜单却没有随它的enabled属性而变化,
请朋友们帮我看看是什么地方出了问题,谢谢!
unit Unit7;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, Mask, ExtCtrls;

type
Tyonghudenglufrm = class(TForm)
CheckBox1: TCheckBox;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
Panel1: TPanel;
MaskEdit1: TMaskEdit;
Label1: TLabel;
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
yonghudenglufrm: Tyonghudenglufrm;
pwdfile:textfile;
pwdstr:string ;
times:integer;
implementation
uses unit1;


{$R *.dfm}

procedure Tyonghudenglufrm.BitBtn2Click(Sender: TObject);
begin
if checkbox1.Checked=true then assignfile(pwdfile,'d:/data/guanliyuan.cfg')
else
assignfile(pwdfile,'d:/data/weihu.cfg');
reset(pwdfile);
readln(pwdfile,pwdstr);
closefile(pwdfile);
times:=times+1;
if Maskedit1.Text=pwdstr then
begin
if checkbox1.Checked =true then
begin

zhukongfrm.MainMenu1.Items[0].Enabled:=true;
zhukongfrm.MainMenu1.Items[1].Enabled:=true;
zhukongfrm.MainMenu1.Items[2].Enabled:=true;
zhukongfrm.MainMenu1.Items[3].Enabled:=true;
zhukongfrm.MainMenu1.Items[4].Enabled:=true;
zhukongfrm.MainMenu1.Items[5][0].Enabled:=false;
zhukongfrm.MainMenu1.Items[5][1].Enabled:=true;
zhukongfrm.MainMenu1.Items[5][2].Enabled:=false;
end
else
begin

zhukongfrm.MainMenu1.Items[0].Enabled:=true;
zhukongfrm.MainMenu1.Items[1].Enabled:=true;
zhukongfrm.MainMenu1.Items[2].Enabled:=true;
zhukongfrm.MainMenu1.Items[3].Enabled:=false;
zhukongfrm.MainMenu1.Items[4].Enabled:=false;
zhukongfrm.MainMenu1.Items[5].Enabled:=false;

end;
close;
end
else
begin
if MessageDlg('密码输入错误。是否退出?',mtConfirmation,[mbYes,mbNo],0)
= mrYes then Close
else if times<3 then Maskedit1.SetFocus
else
begin
MessageDlg('对不起,密码输入严重错误!请退出。',mtInformation,[mbOk],0);
application.Terminate ;
end;
end;
end;

procedure Tyonghudenglufrm.BitBtn1Click(Sender: TObject);
begin

zhukongfrm.MainMenu1.Items[0].Enabled:=true;
zhukongfrm.MainMenu1.Items[1].Enabled:=true;
zhukongfrm.MainMenu1.Items[2].Enabled:=false;
zhukongfrm.MainMenu1.Items[3].Enabled:=false;
zhukongfrm.MainMenu1.Items[4].Enabled:=false;
zhukongfrm.MainMenu1.Items[5][1].Enabled:=false;
zhukongfrm.MainMenu1.Items[5][2].Enabled:=false;
close;
end;

procedure Tyonghudenglufrm.FormShow(Sender: TObject);
begin
Maskedit1.Text:='';
end;

procedure Tyonghudenglufrm.FormActivate(Sender: TObject);
begin
Maskedit1.SetFocus;
end;

end.
 
真是奇怪,为什么要那么麻烦?每个菜单项不是都有个NAME吗,直接用N1.Enabled:=false;
这里的N1就是指菜单项的NAME,我曾用过这种方法。
 
隐藏/显示菜单项语句有误。
最直接的修改方法:MainForm.N1.Enabled := true
 
上面的代码应该没有问题。
要看你显示主窗口的过程,中途是否将主窗口释放后重新建立了一下。
主窗口是 Application 自动建立的,还是你自己建立的?

把显示主窗口的过程贴出来。
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, DBTables, DB, ADODB;

type
Tzhukongfrm = class(TForm)
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
N8: TMenuItem;
N9: TMenuItem;
N10: TMenuItem;
N11: TMenuItem;
N12: TMenuItem;
ADOConnection1: TADOConnection;
ADODataSet1: TADODataSet;
DataSource1: TDataSource;
ADOTable1: TADOTable;
procedure N8Click(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure N4Click(Sender: TObject);
procedure N5Click(Sender: TObject);
procedure N9Click(Sender: TObject);
procedure N10Click(Sender: TObject);
procedure N11Click(Sender: TObject);
procedure N12Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
zhukongfrm: Tzhukongfrm;

implementation

uses Unit2, Unit3, Unit4, Unit6, Unit7, Unit8, Unit9, Unit10, Unit5;

{$R *.dfm}

procedure Tzhukongfrm.N8Click(Sender: TObject);
begin
close;
end;

procedure Tzhukongfrm.N1Click(Sender: TObject);
begin
shujulurufrm.show;
end;

procedure Tzhukongfrm.N2Click(Sender: TObject);
begin
shujuchaxunfrm.show;
end;

procedure Tzhukongfrm.N3Click(Sender: TObject);
begin
shujudayinfrm.show;
end;

procedure Tzhukongfrm.N4Click(Sender: TObject);
begin
shujubeifenfrm.show;
end;

procedure Tzhukongfrm.N5Click(Sender: TObject);
begin
shujukuweihufrm.show;
end;

procedure Tzhukongfrm.N9Click(Sender: TObject);
begin
yonghudenglufrm.show;
end;

procedure Tzhukongfrm.N10Click(Sender: TObject);
begin
genggaimimafrm.show;
end;

procedure Tzhukongfrm.N11Click(Sender: TObject);
begin
quanxianguanlifrm.show;
end;

procedure Tzhukongfrm.N12Click(Sender: TObject);
begin
guanyufrm.show;
end;

procedure Tzhukongfrm.FormCreate(Sender: TObject);
begin
zhukongfrm.MainMenu1.Items[0].Enabled:=false;
zhukongfrm.MainMenu1.Items[1].Enabled:=false;
zhukongfrm.MainMenu1.Items[2].Enabled:=false;
zhukongfrm.MainMenu1.Items[3].Enabled:=false;
zhukongfrm.MainMenu1.Items[4].Enabled:=false;
zhukongfrm.MainMenu1.Items[5][1].Enabled:=false;
zhukongfrm.MainMenu1.Items[5][2].Enabled:=false;
end;

end.
jsxjd:以上是主窗口的,谢谢你,
 
我试了一下你的代码,没有问题,都是通的,
你可以在第一次贴出的代码 “zhukongfrm.MainMenu1.Items[0].Enabled:=true;”前
加上 “showmessage('Ok?');”,共有两个地方。
看看相应的代码执行了没有。

另外提一点,虽然在这里不是问题,但最好把 Tzhukongfrm.FormCreate 中
以下语句的 zhukongfrm. 全部去掉。
zhukongfrm.MainMenu1.Items[0].Enabled:=false;
zhukongfrm.MainMenu1.Items[1].Enabled:=false;
zhukongfrm.MainMenu1.Items[2].Enabled:=false;
zhukongfrm.MainMenu1.Items[3].Enabled:=false;
zhukongfrm.MainMenu1.Items[4].Enabled:=false;
zhukongfrm.MainMenu1.Items[5][1].Enabled:=false;
zhukongfrm.MainMenu1.Items[5][2].Enabled:=false;

以下是我调式的代码,你可以参考一下:
program Project2;

uses
Forms,
Unit1 in 'Unit1.pas' {zhukongfrm},
Unit2 in 'Unit2.pas' {yonghudenglufrm};

{$R *.RES}

begin
Application.Initialize;
Application.CreateForm(Tzhukongfrm, zhukongfrm);
Application.CreateForm(Tyonghudenglufrm, yonghudenglufrm);
Application.Run;
end.
/////////////////////////
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus;

type
Tzhukongfrm = class(TForm)
MainMenu1: TMainMenu;
aaaa1: TMenuItem;
aaa11: TMenuItem;
aa21: TMenuItem;
bb11: TMenuItem;
bb21: TMenuItem;
bb31: TMenuItem;
bb41: TMenuItem;
bb42: TMenuItem;
bb61: TMenuItem;
bb71: TMenuItem;
bb81: TMenuItem;
bb91: TMenuItem;
bbbb1: TMenuItem;
bbbb11: TMenuItem;
bbb21: TMenuItem;
bbbb51: TMenuItem;
procedure aa21Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
zhukongfrm: Tzhukongfrm;
implementation

uses Unit2;

{$R *.DFM}

procedure Tzhukongfrm.aa21Click(Sender: TObject);
begin
yonghudenglufrm.show;
end;

procedure Tzhukongfrm.FormCreate(Sender: TObject);
begin
//zhukongfrm.MainMenu1.Items[0].Enabled:=false;
MainMenu1.Items[1].Enabled:=false;
MainMenu1.Items[2].Enabled:=false;
MainMenu1.Items[3].Enabled:=false;
MainMenu1.Items[4].Enabled:=false;
MainMenu1.Items[5][1].Enabled:=false;
MainMenu1.Items[5][2].Enabled:=false;
end;

end.
/////////////////////////////////////////////

unit Unit2;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons;

type
Tyonghudenglufrm = class(TForm)
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
CheckBox1: TCheckBox;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
yonghudenglufrm: Tyonghudenglufrm;

implementation

uses Unit1;

{$R *.DFM}

procedure Tyonghudenglufrm.BitBtn1Click(Sender: TObject);
begin
zhukongfrm.MainMenu1.Items[0].Enabled:=true;
zhukongfrm.MainMenu1.Items[1].Enabled:=true;
zhukongfrm.MainMenu1.Items[2].Enabled:=false;
zhukongfrm.MainMenu1.Items[3].Enabled:=false;
zhukongfrm.MainMenu1.Items[4].Enabled:=false;
zhukongfrm.MainMenu1.Items[5][1].Enabled:=false;
zhukongfrm.MainMenu1.Items[5][2].Enabled:=false;
close;

end;

procedure Tyonghudenglufrm.BitBtn2Click(Sender: TObject);
begin
if checkbox1.Checked then
begin
zhukongfrm.MainMenu1.Items[0].Enabled:=true;
zhukongfrm.MainMenu1.Items[1].Enabled:=true;
zhukongfrm.MainMenu1.Items[2].Enabled:=true;
zhukongfrm.MainMenu1.Items[3].Enabled:=true;
zhukongfrm.MainMenu1.Items[4].Enabled:=true;
zhukongfrm.MainMenu1.Items[5][0].Enabled:=false;
zhukongfrm.MainMenu1.Items[5][1].Enabled:=true;
zhukongfrm.MainMenu1.Items[5][2].Enabled:=false;
end
else
begin
zhukongfrm.MainMenu1.Items[0].Enabled:=true;
zhukongfrm.MainMenu1.Items[1].Enabled:=true;
zhukongfrm.MainMenu1.Items[2].Enabled:=true;
zhukongfrm.MainMenu1.Items[3].Enabled:=false;
zhukongfrm.MainMenu1.Items[4].Enabled:=false;
zhukongfrm.MainMenu1.Items[5].Enabled:=false;
end;
close;
end;

end.






 
按你的方法试试还是不行,加showmessage后出现信息框,说明代码也运行了,怎么办呀
 
我这边可是一点问题都没有。
 
把事件写在 FORMSHOW里面,就能解决问题
 
谢谢各位,我按以前的方法又做了一遍,一切ok,看来找问题太难了,真的不知哪里出了错。
 
后退
顶部