Entwickler-Ecke

Delphi Language (Object-Pascal) / CLX - Frage über FindComponent


Ruffstaarr - Mi 01.04.09 13:18
Titel: Frage über FindComponent
Hallo
Ich habe in meinem Programm 81 Labels in einem Fenster, und 81 Edits in einem Anderen.
Bevor ich wusste das es FindComponent gibt, habe ich alle manuell Manipuliert und kam mir ZIEMLICH DOOF dabei vor.
Aber Irgendwie Funtioniert es trotzem nicht...
hier mal der code

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
procedure TForm2.Button1Click(Sender: TObject);
var

  i: integer;
  begin


  for i := 1 to 81 do
    begin
     (FindComponent('form1.label' + IntToStr(i)) as TLabel).caption:=(FindComponent('edit1'+ IntToStr(i)) as TEdit).text;
    end;
  {
ANSTATT:
   Form1.label1.caption:=  edit1.text;

  form1.label2.caption:=  edit2.text;
  form1.label3.caption:=  edit3.text;
  form1.label4.caption:=  edit4.text;
  form1.label5.caption:=  edit5.text;
  form1.label6.caption:=  edit6.text;
  form1.label7.caption:=  edit7.text;
  form1.label8.caption:=  edit8.text;
  form1.label9.caption:=  edit9.text;
  form1.label10.caption:=  edit10.text;
  form1.label11.caption:=  edit11.text;
  form1.label12.caption:=  edit12.text;
  form1.label13.caption:=  edit13.text;
  form1.label14.caption:=  edit14.text;
  form1.label15.caption:=  edit15.text;
  form1.label16.caption:=  edit15.text;
  form1.label17.caption:=  edit17.text;
  form1.label18.caption:=  edit18.text;
  form1.label19.caption:=  edit19.text;
  form1.label20.caption:=  edit20.text;
  form1.label21.caption:=  edit21.text;
  form1.label22.caption:=  edit22.text;
  form1.label23.caption:=  edit23.text;
  form1.label24.caption:=  edit24.text;
  form1.label25.caption:=  edit25.text;
  form1.label26.caption:=  edit26.text;
  form1.label27.caption:=  edit27.text;
  form1.label28.caption:=  edit28.text;
  form1.label29.caption:=  edit29.text;
  form1.label30.caption:=  edit30.text;
  form1.label31.caption:=  edit31.text;
  form1.label32.caption:=  edit32.text;
  form1.label33.caption:=  edit33.text;
  form1.label34.caption:=  edit34.text;
  form1.label35.caption:=  edit35.text;
  form1.label36.caption:=  edit36.text;
  form1.label37.caption:=  edit37.text;
  form1.label38.caption:=  edit38.text;
  form1.label39.caption:=  edit39.text;
  form1.label40.caption:=  edit40.text;
  form1.label41.caption:=  edit41.text;
  form1.label42.caption:=  edit42.text;
  form1.label43.caption:=  edit43.text;
  form1.label44.caption:=  edit44.text;
  form1.label45.caption:=  edit45.text;
  form1.label46.caption:=  edit46.text;
  form1.label47.caption:=  edit47.text;
  form1.label48.caption:=  edit48.text;
  form1.label49.caption:=  edit49.text;
  form1.label50.caption:=  edit50.text;
  form1.label51.caption:=  edit5.text;
  form1.label52.caption:=  edit52.text;
  form1.label53.caption:=  edit53.text;
  form1.label54.caption:=  edit54.text;
  form1.label55.caption:=  edit55.text;
  form1.label56.caption:=  edit56.text;
  form1.label57.caption:=  edit57.text;
  form1.label58.caption:=  edit58.text;
  form1.label59.caption:=  edit59.text;
  form1.label60.caption:=  edit60.text;
  form1.label61.caption:=  edit6.text;
  form1.label62.caption:=  edit62.text;
  form1.label63.caption:=  edit63.text;
  form1.label64.caption:=  edit64.text;
  form1.label65.caption:=  edit65.text;
  form1.label66.caption:=  edit66.text;
  form1.label67.caption:=  edit67.text;
  form1.label68.caption:=  edit68.text;
  form1.label69.caption:=  edit69.text;
  form1.label70.caption:=  edit70.text;
  form1.label71.caption:=  edit7.text;
  form1.label72.caption:=  edit72.text;
  form1.label73.caption:=  edit73.text;
  form1.label74.caption:=  edit74.text;
  form1.label75.caption:=  edit75.text;
  form1.label76.caption:=  edit76.text;
  form1.label77.caption:=  edit77.text;
  form1.label78.caption:=  edit78.text;
  form1.label79.caption:=  edit79.text;
  form1.label80.caption:=  edit80.text;
  form1.label81.caption:=  edit8.text; 
}

  close;
end;

end.

wäre Nett wenn jemand helfen könnte

Moderiert von user profile iconGausi: Code- durch Delphi-Tags ersetzt


BenBE - Mi 01.04.09 13:34

FindComponent sucht direkt bei dem Objekt, auf dass es sich bezieht. Wenn Du also Form1.FindComponent machst, sucht er nach Componenten, deren Owner deine Form ist. Du brauchstdarfst hier, da es sich um einen relativen Pfad handelt, keinen vollqualifizierten Bezeichner angeben.


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm2.Button1Click(Sender: TObject);
var
  i: integer;
begin
  for i := 1 to 81 do
  begin
    (Form1.FindComponent('label' + IntToStr(i)as TLabel).caption:=(Form1.FindComponent('edit1'+ IntToStr(i)as TEdit).text;
  end;
end;


Wenn Du nicht ständig Form1 schreiben möchtest, geht auch:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TForm2.Button1Click(Sender: TObject);
var
  i: integer;
begin
  for i := 1 to 81 do
  begin
    With Form1 Do
    Begin
      (FindComponent('label' + IntToStr(i)) as TLabel).caption:=(FindComponent('edit1' + IntToStr(i)) as TEdit).text;
    end;
  end;
end;


Weiterhin kannst Du (wenn du dir absolut sicher bist, dass Du Objekte der richtigen Klasse unter diesem Namen findest) auch explizite Typecasts nutzen (ACHTUNG: Delphi prüft dann keine Typen, was zu schwer lokalisierbaren Fehlern kann, wenn man nicht weiß, was man tut!):


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TForm2.Button1Click(Sender: TObject);
var
  i: integer;
begin
  for i := 1 to 81 do
  begin
    With Form1 Do
    Begin
      TLabel(FindComponent('label' + IntToStr(i))).caption:=TEdit(FindComponent('edit1' + IntToStr(i))).text;
    end;
  end;
end;


delfiphan - Mi 01.04.09 23:16

Du hast 81 Labels und 81 Edit Felder. Sind die in einer eigenen GroupBox oder Panel drin? Falls ja, kannst du auch mit einer Forschleife über MyGroup.Components[] / MyGroup.ComponentCount iterieren. Dürfte einiges schneller sein als über Stringbezeichner zu gehen.


jaenicke - Do 02.04.09 00:38

Dei Frage ist doch eher wofür so viele Labels und Edits irgendwie sinnvoll sein sollen... Ich kann mir da keine sinnvolle Anwendung vorstellen, schließlich gibts ja auch TListView, TStringGrid, usw.


delfiphan - Do 02.04.09 17:38

Ich tippe auf sowas wie Sudoku. Ganz klar, 81 Edit Felder sind nicht sinnvoll, nur schon weil der Zugriff auf die Felder nicht wirklich elegant möglich ist.


Ruffstaarr - Do 02.04.09 22:20

Hallo
Danke erstmal für die schnellen Antworten...
leider bin ich TOTALER Delphi Anfänger, und, ja die sind für ein Sodoku....
habe schon ein fertiges in Turbo Pascal....
als ich versucht habe unter
Type
Label: array [1..3, 1..3, 1..9] of TLabel;

bekam ich keine Labels angezeigt...
also hab ich halt Label mit copy & paste die Labels erzeugt... ;-)

Wenn jemand ein gutes online Tutorial hat, für jemanden mit Pascal Kenntnissen wäre ich auch nicht uninterresiert

@BenBe
Zitat:
...:=(Form1.FindComponent('edit1'+ IntToStr(i)) as TEdit).text;

hinter := ... Die Edit Felder liegen doch in Form 2... muss ich trotztem Form1 schreiben?
und gehen dann die anderen codes darunter auch?


jaenicke - Do 02.04.09 22:23

Ich habe ja bereits ein TStringGrid als Alternative genannt. Wie das dann z.B. aussieht, siehst du in meinem Programm zum Lösen und Generieren von Sudokus:
http://www.delphi-forum.de/viewtopic.php?t=48160


BenBE - Do 02.04.09 22:46

user profile iconRuffstaarr hat folgendes geschrieben Zum zitierten Posting springen:
leider bin ich TOTALER Delphi Anfänger,

Das brauchst Du nicht schreiben; das kann man in der Regel aus den Postings auch so herauslesen; ansonsten wird nach deinem Kenntnis-Stand gefragt, sollte das wichtig sein. Solche Formulierungen blamieren einfach nur dich selbst ;-)

user profile iconRuffstaarr hat folgendes geschrieben Zum zitierten Posting springen:
als ich versucht habe unter

Delphi-Quelltext
1:
2:
Type
Labelarray [1..31..31..9of TLabel;


bekam ich keine Labels angezeigt...

Du musst Label nicht nur deklarieren (was übrigens mit var geht und nicht mit type, sondern diese auch Instantiieren und auf das Formular legen; Im Forum und der Bibliothek sollte es dazu eine Reihe von Beispielen geben.

user profile iconRuffstaarr hat folgendes geschrieben Zum zitierten Posting springen:
also hab ich halt Label mit copy & paste die Labels erzeugt... ;-)

Dünn-Brett-Coder :P

user profile iconRuffstaarr hat folgendes geschrieben Zum zitierten Posting springen:
Wenn jemand ein gutes online Tutorial hat, für jemanden mit Pascal Kenntnissen wäre ich auch nicht uninterresiert

:arrow: Crashkurs Delphi [http://www.christian-stelzmann.de/artikel/crashkurs.htm] von user profile iconChristian S.

user profile iconRuffstaarr hat folgendes geschrieben Zum zitierten Posting springen:
@BenBE
Zitat:

Delphi-Quelltext
1:
...:=(Form1.FindComponent('edit1'+ IntToStr(i)) as TEdit).text;                    


hinter := ... Die Edit Felder liegen doch in Form 2... muss ich trotztem Form1 schreiben?
und gehen dann die anderen codes darunter auch?

Ich bin nach deinem Source oben gegangen, was Du in dem FindComponent angegeben hattest ... und das war Form1. Wenn der Quelltext in Form2 ist, und die Edits auch dort liegen, kann die explizite Angabe des Objektes (FormXY) entfallen --- Sollte sogar für die Portabilität des Quelltextes.