我写了一个动态菜单生成器,但是最后有个问题解决不了,哪位高手帮我一把:
有一个数据库,里面存了一些数据,数据的结构如下:
create talbe t01
(
f01 varchar(4), /*菜单名称*/
f02 varchar(10), /*菜单标题*/
f03 varchar(4), /*上一级菜单名称*/
f04 varchar(20) /*对应FORM名称*/
)当我知道点击了那个菜单项,根据菜单项显示对应的FORM,该怎么写呢?怎样把从数据库中得到的FORM名称转化成TFORM类,使对应的菜单显示呢?
我的程序如下:
procedure TForm1.MenuClick(Sender: TObject);
var
i: integer;
j: integer;
Position : integer;
SubStr: string;
ComponentIndex: integer;
form: TForm;
begin
showmessage('aaa');
Position :=Pos('(', TMenuItem(Sender).Caption);
SubStr:=copy(TMenuItem(Sender).Caption,0,(position-1));
showmessage('SubStr ' + SubStr);
if SubStr ='帮助' then
begin
{
showmessage('bbbb');
for j:=0 to Application.ComponentCount-1 do
begin
if Application.Components[j] is TForm then
begin
showmessage(Application.Components[j].Name);
if Application.Components[j].Name = 'form2' then
begin
ComponentIndex:=j;
end;
end;
end;
}
Showmessage('Component ' + inttostr(ComponentIndex));
//form:=TForm(Application.Components [ComponentIndex].Name).Create(Application);
//form:=TForm2.create(Application);
Application.CreateForm(Form,ADOQuery1.FieldByName('f05').AsString);
form.Show ;
end;
end;请各位高手指点!
有一个数据库,里面存了一些数据,数据的结构如下:
create talbe t01
(
f01 varchar(4), /*菜单名称*/
f02 varchar(10), /*菜单标题*/
f03 varchar(4), /*上一级菜单名称*/
f04 varchar(20) /*对应FORM名称*/
)当我知道点击了那个菜单项,根据菜单项显示对应的FORM,该怎么写呢?怎样把从数据库中得到的FORM名称转化成TFORM类,使对应的菜单显示呢?
我的程序如下:
procedure TForm1.MenuClick(Sender: TObject);
var
i: integer;
j: integer;
Position : integer;
SubStr: string;
ComponentIndex: integer;
form: TForm;
begin
showmessage('aaa');
Position :=Pos('(', TMenuItem(Sender).Caption);
SubStr:=copy(TMenuItem(Sender).Caption,0,(position-1));
showmessage('SubStr ' + SubStr);
if SubStr ='帮助' then
begin
{
showmessage('bbbb');
for j:=0 to Application.ComponentCount-1 do
begin
if Application.Components[j] is TForm then
begin
showmessage(Application.Components[j].Name);
if Application.Components[j].Name = 'form2' then
begin
ComponentIndex:=j;
end;
end;
end;
}
Showmessage('Component ' + inttostr(ComponentIndex));
//form:=TForm(Application.Components [ComponentIndex].Name).Create(Application);
//form:=TForm2.create(Application);
Application.CreateForm(Form,ADOQuery1.FieldByName('f05').AsString);
form.Show ;
end;
end;请各位高手指点!
... ... ... ...
menu_2 “标题一” null TForm2 Form2
... ... ... ... 点击的那个菜单容易确定吧,那么,取得这个name,然后,根据这个name查找相应的f04对应的字段TForm2,就是了。
然后用这个取出的“字符串”Application.CreateForm(TForm2,Form2);
即可了。
首先在程序伊始,执行RegisterClasses注册所有的将要动态创建的Form的类,例如:
RegisterClasses([TForm1, TForm2, TForm3]);动态创建时:(设aFormClassName: string保存着类名),只需执行TCustomFormClass(GetClass(aFormClassName)).Create(Application).Show;
sss:tmenuitem;
begin
sss:=Sender as tmenuitem;
with query1 do
begin
close;
sql.clear;
sql.add('SELECT f04,f05 FROM t01 WHERE f01 = '''+sss.Name+'''');
open;
end;
Application.CreateForm(query1.fieldbyname('f04').asstring,query1.fieldbyname('f04').asstring);
...
足够了吗?