![]() |
Dynamische Komponenten ansteuern
Hallo miteinander,
Ich stehe gerade vor einem kleinen Problem bei dem ich nicht so recht weiter weiß. Ich erstelle über eine Function einige kleine Shapes auf meinem Form. Diese Shapes möchste ich jedoch zur Laufzeit gerne verschieben. Hier ist auch schon das Problem: Dadurch das der Name variabel vergeben wird kann ich diese nicht direkt ansteuern. Gibt es eine Methode um diese dennoch zu verwalten? Hier die Function:
Delphi-Quellcode:
Der Name des Shapes wird als Parameter übergeben und derzeit von einem entferntem TCP Server vergeben. Der Name ist jedoch innerhalb der Applikation bekannt.
function Shape_gen(Name: String):String;
var Shape : TShape; begin Shape := TShape.Create(Form1); With Shape do begin Parent := Form1; Left := Round(Form1.Width/2); Top := Round(Form1.Height/2); Width := 30; Height := 30; Shape := stCircle; Name := Name; end; end; Vielen Dank im voraus. |
AW: Dynamische Komponenten ansteuern
Dann bennen den Parameter nicht Name oder verwende das böse WITH nicht.
Und warum ist Shape_gen keine Methode von TForm1? Und wieso heißt das immernoch Form1? |
AW: Dynamische Komponenten ansteuern
Warum sollte es denn nicht Form1 heißen?
Wie würdest du es denn realisieren?:-D |
AW: Dynamische Komponenten ansteuern
Hallo,
Zitat:
Delphi-Quellcode:
die entsprechende TShape-Instanz über deren Namen suchen.
FindComponent
Zitat:
Delphi-Quellcode:
, dann stellt sich dieses Problem nicht.
Self
Zitat:
Delphi-Quellcode:
Gruß
type
TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); private { Private-Deklarationen } FSelectedShape : TShape; function CreateShape (const ShapeName : string) : TShape; function GetShapeByName (const ShapeName : string) : TShape; procedure SetSelectedShape (const Shape : TShape); procedure ShapeMouseDown (Sender : TObject; Button : TMouseButton; Shift : TShiftState; X, Y : Integer); public { Public-Deklarationen } end; procedure TForm1.Button1Click(Sender: TObject); begin if not Assigned (GetShapeByNAme ('Shape1')) then CreateShape ('Shape1') end; procedure TForm1.Button2Click(Sender: TObject); begin if not Assigned (GetShapeByNAme ('Shape2')) then CreateShape ('Shape2') end; procedure TForm1.Button3Click(Sender: TObject); var s : TShape; begin s := GetShapeByName ('Shape2'); if Assigned (s) then begin SetSelectedShape (s); s.Left := s.Left + 10 end end; procedure TForm1.Button4Click(Sender: TObject); begin if Assigned (FSelectedShape) then FSelectedShape.Top := FSelectedShape.Top - 10 end; function TForm1.CreateShape (const ShapeName : string) : TShape; // Jetzt als Methode der Form begin Result := TShape.Create (Self); Result.Parent := Self; Result.Left := Width div 2; Result.Top := Height div 2; Result.Width := 30; Result.Height := 30; Result.Shape := stCircle; Result.Name := ShapeName; Result.OnMouseDown := ShapeMouseDown end; function TForm1.GetShapeByName (const ShapeName : string) : TShape; begin Result := TShape (FindComponent (ShapeName)) end; procedure TForm1.SetSelectedShape (const Shape : TShape); begin if Assigned (FSelectedShape) then FSelectedShape.Brush.Color := clWhite; FSelectedShape := Shape; FSelectedShape.Brush.Color := clRed end; procedure TForm1.ShapeMouseDown (Sender : TObject; Button : TMouseButton; Shift : TShiftState; X, Y : Integer); begin if Sender is TShape then SetSelectedShape (TShape (Sender)) end; |
AW: Dynamische Komponenten ansteuern
Zitat:
|
AW: Dynamische Komponenten ansteuern
Vielen Dank für deine Hilfe. Das hat mir wirklich sehr geholfen:-D
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 11:12 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz