아래 소스는 메뉴의 내용을 런타임상에서 더
추가 시킬 수 있는 간단한 예제입니다.
근데 만일
myITEM := TMenuItem.Create(nil); 에서 nil아니고
Form1이나 MenuItem1을 넣고 메뉴 내용을 더 추가시키면
내용이 넣으면 넣을수록 제곱이 되어서 들어가 지더군요.
왜 그런걸까요? 아직 제가 상속에 대한 개념이 덜 잡힌거 같습니다.
좋은 답변 바랍니다.
=======================================================================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Label1: TLabel;
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure myClickEvent(Sender: TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
myITEM : TMenuItem;
i : Integer;
IsExist : boolean;
begin
IsExist := False;
for i:=1 to MainMenu1.Items[0].Count do begin
if MainMenu1.Items[0].Items[i-1].Caption= edit1.text then begin
IsExist := true;
break;
end;
if not IsExist then begin
myITEM := TMenuItem.Create(); //만일 nil이 아니고 다른 값이라면?
myITEM.onClick := myClickEvent;
myITEM.caption := edit1.text;
MainMenu1.items[0].Add(myITEM);
end
else ShowMessage('이미 존재하는 메뉴 입니다.');
end;
end;
// 런 타임 중에 생성된 메뉴를 클릭했을 때 발생하는 이벤트 프로시저
procedure TForm1.myClickEvent(Sender: TObject);
begin
ShowMessage(TMenuItem(Sender).Caption+'이 눌렸습니다');
end;
end.
i : Integer;
myITEM : TMenuItem;
begin
MainMenu1.Items[0].RethinkLines;
for i:=0 to MainMenu1.Items[0].Count-1 do begin
if MainMenu1.Items[0].Items[i].Caption = Edit1.Text then begin
ShowMessage('존재하는 메뉴');
Exit;
end;
end;
myITEM := TMenuItem.Create(MainMenu1);
myITEM.onClick := myClickEvent;
myITEM.caption := edit1.text;
MainMenu1.items[0].Add(myITEM);
end;
// 런 타임 중에 생성된 메뉴를 클릭했을 때 발생하는 이벤트 프로시저
procedure TForm1.myClickEvent(Sender: TObject);
begin
ShowMessage(TMenuItem(Sender).Caption+'이 눌렸습니다');
end;
즐푸~~