Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi Form.FindChildControl('ControlName') findet TLabel nicht (https://www.delphipraxis.net/103271-form-findchildcontrol-controlname-findet-tlabel-nicht.html)

Ares 13. Nov 2007 15:51


Form.FindChildControl('ControlName') findet TLabel nicht
 
Hallo!

Ich habe auf einem Form mehrere Buttons und Labels, deren Caption ich mit folgender Methode ändern:

Delphi-Quellcode:
    procedure SetControlCaption(ControlName, Caption: String);
    begin
      control := MainForm.FindChildControl(ControlName);
      if ((control<>nil) and (control is TLabel)) then
        TLabel(control).Caption := Caption;
      if ((control<>nil) and (control is TButton)) then
        TButton(control).Caption := Caption;
    end;
Ein Aufruf von SetControlCaption('MeinButton', 'Test') ändern nun problemlos die Caption des Buttons "MeinButton" auf "Test". Wenn ich das Gleiche mit einerm Label "MeinLabel" versuche, passiert aber nichts, weil FindChildControl(ControlName) nur nil zurückliefert.

Wenn ich einen Button mit dem Namen "MeinLabel" erstelle, wird dieser prima gefunden und die Caption geändert. Am Namen der Control liegt es also nicht. Warum findet FindChildControl(ControlName) also keine TLabel?

Besten Dank
Ares

Deep-Sea 13. Nov 2007 15:55

Re: Form.FindChildControl('ControlName') findet TLabel nicht
 
Weil FindChildControl nur TWinControl's durchsucht :wink:
Nutze wenn dann FindComponent ...

peschai 14. Nov 2007 05:30

Re: Form.FindChildControl('ControlName') findet TLabel nicht
 
Hallo

In Ergänzung zu Deap-Sea:
Es gibt "controls" die sind echte kleine Windowsfensterl'ein mit eigenem WindowHandle und andere welche von delphi selber verwaltet werden. Ein TLabel wird z.b. nur gezeichnet hat aber kein eigenes Windowhandle ... spart ressourcen ....

Nuclear-Ping 14. Nov 2007 08:49

Re: Form.FindChildControl('ControlName') findet TLabel nicht
 
Afaik ist FindComponent auch nicht rekursiv. Das heisst, wenn dein Label z.B. auf einem Panel liegt, wird Form1.FindComponent das ebenfalls nicht finden. In dem Falle brauchst du eine rekursive Funktion, die alle Components auf der Form durchgeht und sich selbst mit dem Control wieder aufruft, wenn ComponentCount eines Controls > 0 ist.

Deep-Sea 14. Nov 2007 08:52

Re: Form.FindChildControl('ControlName') findet TLabel nicht
 
Zitat:

Zitat von Nuclear-Ping
Afaik ist FindComponent auch nicht rekursiv. Das heisst, wenn dein Label z.B. auf einem Panel liegt, wird Form1.FindComponent das ebenfalls nicht finden. In dem Falle brauchst du eine rekursive Funktion, die alle Components auf der Form durchgeht und sich selbst mit dem Control wieder aufruft, wenn ComponentCount eines Controls > 0 ist.

Rekursiv ist es nicht. Richtig. Aber FindComponent geht vom Owner aus, und wenn man nicht gerade etwas tiefer in Delphi eingreift, ist der Owner jeder Komponente auf einer Form die Form selbst, so das FindComponent sehr wohl alle findet. Was du meinst ist vlt. die Eigenschaft "Controls" :wink:

Nuclear-Ping 14. Nov 2007 08:58

Re: Form.FindChildControl('ControlName') findet TLabel nicht
 
Ah, tatsache ... Wieder was gelernt. :nerd:


Alle Zeitangaben in WEZ +1. Es ist jetzt 22:23 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz