Autor Beitrag
Grafix
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 122

Windows 7 Professional
Delphi 2009, PHP, Prolog, Python, Java
BeitragVerfasst: Mo 02.02.09 15:35 
Hey,
kann ich einen Identifier variable machen? z.B.:

ausblenden Delphi-Quelltext
1:
Form1.Label.Caption := 'blubb';					


wobie Label eine Variable ist.
Danke für eure Hilfe =)

Grafix


Moderiert von user profile iconNarses: Topic aus Sonstiges (Delphi) verschoben am Mo 02.02.2009 um 16:03
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Mo 02.02.09 15:40 
Entweder, du suchst FindComponent (Forensuche), oder sowas hier:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
var MyLabel: TLabel;
begin
  MyLabel := Label1;
  MyLabel.Caption := 'blah';

  MyLabel := Label2;
  MyLabel.Caption := 'blah';

  // usw.
end;

?

Beispiel ist nun nicht so sinnig.. macht bei Funktionen mit einem Label als Parameter mehr Sinn.
Worauf läuft das denn hinaus?

_________________
PROGRAMMER: A device for converting coffee into software.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mo 02.02.09 15:44 
Ich rate einfach mal ins Blaue und schmeiße das Stichwort FindComponent in die Runde. :D

Edit: Ach Mist, das steht ja schon da. :oops:

_________________
We are, we were and will not be.
Grafix Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 122

Windows 7 Professional
Delphi 2009, PHP, Prolog, Python, Java
BeitragVerfasst: Mo 02.02.09 15:51 
Also mit FindComponent komm ich nicht zurecht...
Also ich möchte ein Label, dessen Name mir per Variable in eine Funktion übergeben wird, verändern

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.L1Click(Sender: TObject);
begin
  kontrolliere_Zahlen(Label1.Name);
end;

procedure kontrolliere_Zahlen(Name: String);
begin
  Form1.Name.Caption := 'bla'; / (FindComponent(Name as TLabel).Caption := 'bla';
end;


etwa so... geht das? also der Identifier ist eine Variable, aber so kann ich das nicht kompillieren?!
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mo 02.02.09 15:56 
Erster Versuch einer Korrektur:

ausblenden Delphi-Quelltext
1:
(FindComponent(Name) as TLabel).Caption := 'bla';					


Könnte aber sein, dass ich noch was vergessen habe. :gruebel:

_________________
We are, we were and will not be.
Grafix Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 122

Windows 7 Professional
Delphi 2009, PHP, Prolog, Python, Java
BeitragVerfasst: Mo 02.02.09 16:05 
hm, es funktioniert nicht -.-

ausblenden Delphi-Quelltext
1:
Form1.(FindComponent(Name) as TLabel).Caption := 'bla';					


da sagt er:
Identifier expected but ( found
Operator not applicable to this operand type
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Mo 02.02.09 16:15 
Steht doch schon angedeutet im Fehlertext: Nach dem . keine (

aber besser: lass das Form1. weg

_________________
PROGRAMMER: A device for converting coffee into software.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mo 02.02.09 16:27 
user profile iconGrafix hat folgendes geschrieben Zum zitierten Posting springen:
hm, es funktioniert nicht -.-

ausblenden Delphi-Quelltext
1:
Form1.(FindComponent(Name) as TLabel).Caption := 'bla';					


da sagt er:
Identifier expected but ( found
Operator not applicable to this operand type

Wenn dann (Form1.FindComponent(Name) as TLabel).Caption := 'bla';, aber wie schon gesagt:

user profile iconXentar hat folgendes geschrieben Zum zitierten Posting springen:
Steht doch schon angedeutet im Fehlertext: Nach dem . keine (

aber besser: lass das Form1. weg

Hängt damit zusammen, ohne Form1 wird die aktuelle Formular-Instanz genommen, mit Form1 eine spezielle. Das führt genau dann zu Problemen, wenn Du mehrere Instanzen dieser Formularklasse erzeugen willst (aus welchem Grund auch immer).

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Di 03.02.09 02:21 
Dazu musst du kontrolliere_Zahlen zum Formular hinzufügen, damit es direkt auf die Elemente des Formulars zugreifen kann:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
type
  TForm1 = class(TForm)
  //...
  private
    procedure kontrolliere_Zahlen(Name: String);
  //...
  end;
    
//...

procedure TForm1.kontrolliere_Zahlen(Name: String);
begin

end;

Aber was soll der ganze Aufwand überhaupt?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.L1Click(Sender: TObject);
begin
  kontrolliere_Zahlen(Label1.Name);
end;

procedure kontrolliere_Zahlen(Name: String);
begin
  Form1.Name.Caption := 'bla'; / (FindComponent(Name as TLabel).Caption := 'bla';
end;
Du übergibst den Namen einer Komponente und muss die danach wieder suchen?!? :shock:
Du hattest die Komponente doch schon und nicht nur deren Namen.

Warum nicht einfach so? ;-)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.L1Click(Sender: TObject);
begin
  kontrolliere_Zahlen(Label1);
end;

procedure kontrolliere_Zahlen(MyLabel: TLabel);
begin
  MyLabel.Caption := 'bla';
end;
Wobei sich dann fragt wozu du die zusätzliche Prozedur brauchst.

Willst du vielleicht mehrere OnClicks zusammenfassen?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.LXClick(Sender: TObject);
begin
  (Sender as TLabel).Caption := 'bla';
end;
Dieses OnClick (LXClick) kannst du im Objektinspektor unter Ereignisse einfach bei allen Labels eintragen.

Wobei eine eigene Prozedur schon nicht schlecht ist. Aber wenn du nur das erreichen willst ist sie gar nicht nötig.