Eve vardım... Size tam teşekküllü bir örnek hazırladım.
Boş bir proje açın, bir button bir ListBox ekleyin ve aşağıdaki kodları uygulayın.,
Odaklanacağınız ve projenizde kullanacağınız fonksiyon GetFrame() olanı.
function TForm1.GetFrame( aItem: TListBoxItem ): TFrame;
var
i : Integer;
begin
result := nil;
for i := 0 to aItem.Controls.Count-1
do
if aItem.Controls[i].ClassType = TFrame
then begin
result := TFrame( aItem.Controls[i] );
break;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i : Integer;
LItem : TListBoxItem;
LButton : TButton;
LFrame : TFrame;
LRectangle : TRectangle;
begin
while ListBox1.Items.Count > 0
do
FreeAndNil(ListBox1.ListItems[0]);
for i := 1 to 10 do
begin
LItem := TListBoxItem.Create(ListBox1);
With LItem do
begin
Name := Format('ListItem_%.2d', [ListBox1.Items.Count]);
Text := '';
Height := 80;
LRectangle := TRectangle.Create(LItem);
with LRectangle do
begin
Name := Format('Rectangle_%s', [LItem.Name]);
Parent := LItem;
Align := TAlignLayout.Client;
if i mod 2 = 0
then
Fill.Color := TAlphaColorRec.White
else
Fill.Color := TAlphaColorRec.Red;
end;
LFrame := TFrame.Create(LItem);
with LFrame do
begin
Name := Format('Frame_%s', [LItem.Name]);
Parent := LItem;
Align := TAlignLayout.Client;
OnMouseUp := DoFrameMouseUp;
end;
LButton := TButton.Create(LFrame);
With LButton do
begin
Name := Format('Button_%s', [LItem.Name]);
Parent := LFrame;
Position.X := 10;
Position.Y := 10;
Width := 100;
Height := 30;
Text := 'Btn: ' + LItem.Name;
OnKeyUp := DoKeyUp;
OnClick := DoButtonClick;
end;
end;
ListBox1.AddObject(LItem);
end;
end;
function TForm1.GetFrame( aItem: TListBoxItem ): TFrame;
var
i : Integer;
begin
result := nil;
for i := 0 to aItem.Controls.Count-1
do
if aItem.Controls[i].ClassType = TFrame
then begin
result := TFrame( aItem.Controls[i] );
break;
end;
end;
procedure TForm1.DoButtonClick(sender: TObject);
var
LFrame : TFrame;
LItem : TListBoxItem;
begin
LFrame := TFrame( TButton(sender).Parent );
LItem := TListBoxItem( LFrame.Parent );
ShowmessageFmt( '%s Item içindeki' + sLineBreak
+ '%s Frame içindeki' + sLineBreak
+ '%s butonu tıklandı',
[ LItem.Name, LFrame.Name, TButton(sender).Name ]);
end;
procedure TForm1.DoKeyUp(Sender: TObject; var Key: Word;
var KeyChar: Char; Shift: TShiftState);
var
LFrame : TFrame;
LItem : TListBoxItem;
LButton : TButton;
begin
if (Key = vkF2) and (ListBox1.Selected <> nil) then
begin
LItem := ListBox1.Selected;
LFrame := GetFrame( LItem );
LButton := TButton(LFrame.Controls[0]);
ShowmessageFmt( '%s Item içinde' + sLineBreak
+ '%s Frame içinde' + sLineBreak
+ '%s butonu var',
[ LItem.Name, LFrame.Name, LButton.Name ]);
end;
end;
procedure TForm1.DoFrameMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
var
LFrame : TFrame;
LItem : TListBoxItem;
LButton : TButton;
begin
LFrame := TFrame(sender);
LItem := TListBoxItem(LFrame.Parent);
LButton := TButton(LFrame.Controls[0]);
LItem.IsSelected := true;
LButton.SetFocus;
end;
