case tMenuitem(sender).tag of
101: begin
//菜单项为'client'
if f_client = nil then f_client := tf_client.create(application);
f_client.show;
end;
102: begin
//菜单项为'supply'
if f_supply = nil then f_supply := tf_supply.create(application);
f_supply.show;
end;
103:begin
//菜单项为'dept'
if f_dept = nil then f_dept := tf_dept.create(application);
f_dept.show;
end;
。
在主窗口菜单中,有很多菜单项,调用各自的表单,都要写上以上的两行格式一样的代码。
能否编写一个通用函数,在点击菜单项时,将菜单项的名字(如,client, supply, dept)作为参数转化为表单调用如:procedure showform(who: string);
begin
if [who] = nil then [who] := t[who].create(application);
[who].show;
end;
。
end;
101: begin
//菜单项为'client'
if f_client = nil then f_client := tf_client.create(application);
f_client.show;
end;
102: begin
//菜单项为'supply'
if f_supply = nil then f_supply := tf_supply.create(application);
f_supply.show;
end;
103:begin
//菜单项为'dept'
if f_dept = nil then f_dept := tf_dept.create(application);
f_dept.show;
end;
。
在主窗口菜单中,有很多菜单项,调用各自的表单,都要写上以上的两行格式一样的代码。
能否编写一个通用函数,在点击菜单项时,将菜单项的名字(如,client, supply, dept)作为参数转化为表单调用如:procedure showform(who: string);
begin
if [who] = nil then [who] := t[who].create(application);
[who].show;
end;
。
end;
我也遇到类似的问题
已经解决啦
http://expert.csdn.net/Expert/topic/1434/1434327.xml?temp=.5331232
在菜單調用時傳遞菜單名稱參數對數據裡面去找,如存在相對的窗體名就調用它,這樣還可能將
菜單的單擊事件做為一個統一處理。
以下是我在系統中的做法,供參考:聲明一單元,用以存全局變量數據:
unit SysVarType;interface
uses Windows, SysUtils, Forms ;
type
TFormClass = class of TForm;
TFormStyle = (fsMDI, fsModal, fsNon);
TFormInfo = Record
FormName : String;
RunForm : TFormClass;
PriField : String;
FormStyle : TFormStyle;
SForm : TForm;
Hint : String;
end;
Const
MAX_FORM_COUNT = 201;
implementationend.賦值,可集中放於一單元:
pubFormInfo[0,1].FormName := 'FrmBaseType';
pubFormInfo[0,1].RunForm := TFrmBaseType;
pubFormInfo[0,1].PriField := 'Code_No'; //主關鍵字,用以記錄定位時使用
pubFormInfo[0,1].SForm := nil;
pubFormInfo[0,1].FormStyle := fsMDI;
pubFormInfo[0,1].Hint := '料品科目資料';賦事件:
procedure TMainForm.FormShow(Sender: TObject);
begin
SetMenuItemClick;
......
end;procedure TMainForm.SetMenuItemClick;
var I, J: Integer;
locMenuName: String;
begin
for I := 0 to MainForm.ComponentCount - 1 do
if MainForm.Components[I] is TMenuItem then
begin
locMenuName := TMenuItem(MainForm.Components[I]).Name;
for J := 0 to MAX_FORM_COUNT - 1 do
if UpperCase(pubFormInfo[pubI, J].FormName)=UpperCase(Copy(locMenuname,2,Length(locMenuname)-1)) then
begin
TMenuItem(MainForm.Components[I]).OnClick := RunApp;
Break;
end;
end;
end;procedure TMainForm.RunApp(Sender: TObject);
var locMenuName: String;
begin
IF Sender is TMenuItem then locMenuName := (Sender as TMenuItem).Name
else Exit;
RunAppTreeView(UpperCase(Copy(locMenuname,2,Length(locMenuname)-1)),1);
end;procedure TMainForm.RunAppTreeView(SelectedNode: String; NodeType:Integer); //樹形結點/菜單名,類型:菜單還是樹
var I: Integer;
begin
for I := 0 to MAX_FORM_COUNT - 1 do
begin
if ((NodeType=1) and (UpperCase(pubFormInfo[pubI,I].FormName)=SelectedNode))
or ((NodeType=2) and (pubFormInfo[pubI,I].Hint=SelectedNode)) then
begin
if pubFormInfo[pubI,I].SForm = nil then
begin
pubFormInfo[pubI,I].SForm := pubFormInfo[pubI,I].RunForm.Create(Application);
pubFormInfo[pubI,I].SForm.Tag := I;
if pubFormInfo[pubI,I].Hint <> '' then
pubFormInfo[pubI,I].SForm.Caption := pubFormInfo[pubI,I].Hint;
end;
if pubFormInfo[pubI,I].FormStyle = fsMDI then
pubFormInfo[pubI,I].SForm.Show
else pubFormInfo[pubI,I].SForm.ShowModal;
Break;
end;
end; //end for
end;菜單的命名規則為 'X'+窗體名 ,即在對應調用窗體名稱的前面加一個前綴字符
类参数如保传递
var
aFormClass:TformClass;
aForm:Tform;
begin
aFormClass:=TFormClass(GetClass(aFormName));
if (aFormclass<>nil) then begin
aForm:=aFormClass.Create(Application);
aForm.show;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GenerateForm('tform2')
end;在form2中你必须加入
initialization
registerClass(Tform2);finalization
unregisterClass(Tform2);
registerClass(Tform2);finalization
unregisterClass(Tform2);加在哪个位量