그것은 양식 (코드는 않는)의 일부가 아닌 TButton
어디를 만들 수 많은 이해가되지 않습니다. 그와
는 그것을 참조하기 위해 나중에 무료로, 당신은 어딘가에에 대한 참조를 저장해야했다되고.
는 "여러 버튼"을 참조하여 삭제 루틴에서 배열 코드를 사용하고 있기 때문에
, 나는 당신이 아마 버튼의 배열을 추적하고자하는 것 같아요. 여기에 바로 그 일의 예는 다음과 같습니다
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject); // Add via Object Inspector Events tab
private
{ Private declarations }
// Add these yourself
BtnArray: array of TButton;
procedure CreateButtons(const NumBtns: Integer);
procedure DeleteBtn(BtnToDel: TButton);
procedure BtnClicked(Sender: TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.DeleteBtn(BtnToDel: TButton);
var
i: Integer;
begin
// Check each button in the array to see if it's BtnToDel. If so,
// remove it and set the array entry to nil so it can't be deleted
// again.
for i := Low(BtnArray) to High(BtnArray) do
begin
if BtnArray[i] = BtnToDel then
begin
FreeAndNil(BtnArray[i]);
Break;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// Create 10 buttons on the form
CreateButtons(10);
end;
// Called when each button is clicked. Assigned in CreateButtons() below
procedure TForm1.BtnClicked(Sender: TObject);
begin
// Delete the button clicked
if (Sender is TButton) then
DeleteBtn(TButton(Sender));
end;
procedure TForm1.CreateButtons(const NumBtns: Integer);
var
i: Integer;
begin
// Allocate storage for the indicated number of buttons
SetLength(BtnArray, NumBtns);
// For each available array item
for i := Low(BtnArray) to High(BtnArray) do
begin
BtnArray[i] := TButton.Create(nil); // Create a button
BtnArray[i].Parent := Self; // Tell it where to display
BtnArray[i].Top := i * (BtnArray[i].Height + 2); // Set the top edge so they show
BtnArray[i].Name := Format('BtnArray%d', [i]); // Give it a name (not needed)
BtnArray[i].Caption := Format('Btn %d', [i]); // Set a caption for it
BtnArray[i].OnClick := BtnClicked; // Assign the OnClick event
end;
end;
새 빈 VCL이 코드는 응용 프로그램을 형성 넣고 실행하면 폼에 10 개 버튼 ('щࣉ = 0 through
щࣉ =를 9`) 볼 수 있습니다. 버튼을 클릭하면 양식 (및 배열)에서 제거됩니다.
귀하의 질문은 불완전하다. "특정 인스턴스를 어떻게 참조 할 수 있습니까?" 어떻게 레퍼런스를 저장하고 있습니까? 만약 어딘가에서 레퍼런스를 기억하지 못한다면 어떻게 참조 할 수 있을까요? 어떻게 식별하고 싶습니까? –