Reputation: 227
I need to change component options by clicking on it but I need to implement this function for many objects that I create dynamically.
I need to do something like this:
Object_I_clicked_on.brush.color:= clred; ...
this is not good because there will be many components and all components will have the same function
BAD: shape1.brush.color:= clred;
Is there some way to do this? Something with sender (variable) etc.
Upvotes: 1
Views: 1474
Reputation: 108929
Create a new VCL project. Add a TShape
to it. Ctrl+C and Ctrl+V so you get many of them on the form. Select them all and select the OnMouseDown
event on the Events tab of the Object Inspector. Type ShapeMouseDown
and press Enter. Then write
procedure TForm1.ShapeMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Sender is TShape then
TShape(Sender).Brush.Color := clHighlight;
end;
If you have a lot of dynamically-created controls, the idea is the same. For instance, if you got an array FShapes: array of TShapes
containing dynamically-created shapes, you need to give them all the same event handler:
for i := 0 to high(FShapes) do
FShapes[i].OnMouseDown := ShapeMouseDown;
Upvotes: 2