Einzelnen Beitrag anzeigen

Volker Z.

Registriert seit: 3. Dez 2012
Ort: Augsburg, Bayern, Süddeutschland
419 Beiträge
 
Delphi XE4 Ultimate
 
#4

AW: Dynamische Komponenten ansteuern

  Alt 25. Apr 2014, 23:09
Hallo,

Zitat:
[...] Hier ist auch schon das Problem: Dadurch das der Name variabel vergeben wird kann ich diese nicht direkt ansteuern. [...] Der Name ist jedoch innerhalb der Applikation bekannt
Dann kannst Du über die Methode FindComponent die entsprechende TShape-Instanz über deren Namen suchen.

Zitat:
Warum sollte es denn nicht Form1 heißen?
Naja, Du gehst davon aus, dass Form1 auch wirklich instanziiert ist - ist dem nicht so, dann geht das böse in die Hose. Besser: Du packst die Funktion als Methode in Dein Form ein und beziehst Dich dann auf Self , dann stellt sich dieses Problem nicht.

Zitat:
Wie würdest du es denn realisieren?
Keine Ahnung wie himitsu das angehen würde, als Ansatz mal:
Delphi-Quellcode:
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;
Gruß
Volker Zeller

Geändert von Volker Z. (25. Apr 2014 um 23:13 Uhr)
  Mit Zitat antworten Zitat