Mam na formatce group boxa a w nim Label i 3 RadioButtony da się jakoś zrobić aby wczytać captoiny tych obiektow z jednego pliku tekstowego?
var
tf: TextFile;
s1, s2, s3: String;
begin
AssignFile(tf, 'twojplik.txt');
Reset(tf);
Readln(tf, s1); //Zapisujesz do s1 pierwszą linię z pliku
Readln(tf, s2); //Zapisujesz do s2 drugą linię z pliku
Readln(tf, s3); //Zapisujesz do s3 trzecią linię z pliku
//... itd.
CloseFile(tf);
// przypisanie zmiennych s1, s2 i s3 do wartości caption poszczególnych obiektów
end;
Dzieki za odpowiedź, mam jeszcze jedeno pytanie czy da się zrobić aby nieodczytywać linia po lini tylko wszystko bylo w jedenj lini podzielane jakimiś separatorami? Chcailbym zrobić cos w rodzaju testu wyboru, i chodzi mi o to zeby wszytskie pytania znajdowlay sie w jednym pliku. Probowalem zrobić to przez ustalenie pozycji znaku | i przypisaniu captionom wyrazow znajdujacych sie po lewej i prawej stronie znaku | jednak to dzial tylko w przypadku gdy mam 2 wyrazy, a ja chcialbym zawrzeć w jednej lini wszystkie dane o pytaniu np:
Stolica Polski|Wrocław|Kraków|Warszawa|C
Nie dawno był podobny temat na tym forum:
http://4programmers.net/Forum/267108?h=TextFile#267108
Procedura odczytująca:
procedure ReadControls(Parent: TWinControl; FileName: string);
var
f: TextFile;
procedure SearchCtrl(ParentCtrl: TWinControl);
var
i: integer;
s: string;
begin
i:=0;
while (not EOF(f)) do
begin
while (i < ParentCtrl.ControlCount) and
not (ParentCtrl.Controls[i] is TControl) do
Inc(i);
if (i = ParentCtrl.ControlCount) then break;
if ParentCtrl.Controls[i] is TLabel then
begin
Readln(f, s);
TLabel(ParentCtrl.Controls[i]).Caption:= s;
end
else if ParentCtrl.Controls[i] is TEdit then
begin
Readln(f, s);
TEdit(ParentCtrl.Controls[i]).Text:= s;
end
else if ParentCtrl.Controls[i] is TButton then
begin
Readln(f, s);
TButton(ParentCtrl.Controls[i]).Caption:= s;
end
else if ParentCtrl.Controls[i] is TCheckBox then
begin
Readln(f, s);
TCheckBox(ParentCtrl.Controls[i]).Caption:= s;
end
else if ParentCtrl.Controls[i] is TRadioButton then
begin
Readln(f, s);
TRadioButton(ParentCtrl.Controls[i]).Caption:= s;
end
else if ParentCtrl.Controls[i] is TGroupBox then
begin
Readln(f, s);
TGroupBox(ParentCtrl.Controls[i]).Caption:= s;
end
else if ParentCtrl.Controls[i] is TRadioGroup then
begin
Readln(f, s);
TRadioGroup(ParentCtrl.Controls[i]).Caption:= s;
end
else if ParentCtrl.Controls[i] is TPanel then
begin
Readln(f, s);
TPanel(ParentCtrl.Controls[i]).Caption:= s;
end;
if ParentCtrl.Controls[i] is TWinControl then
SearchCtrl(TWinControl(ParentCtrl.Controls[i]));
Inc(i);
end;
end;
begin
AssignFile(f, FileName);
Reset(f);
SearchCtrl(Parent);
CloseFile(f);
end;
Procedura zapisująca:
procedure WriteControls(Parent: TWinControl; FileName: string);
var
f: TextFile;
procedure SearchCtrl(ParentCtrl: TWinControl);
var
i: integer;
s: string;
begin
for i:=0 to ParentCtrl.ControlCount - 1 do
begin
if ParentCtrl.Controls[i] is TLabel then
begin
s:= TLabel(ParentCtrl.Controls[i]).Caption;
Writeln(f, s);
end
else if ParentCtrl.Controls[i] is TEdit then
begin
s:= TEdit(ParentCtrl.Controls[i]).Text;
Writeln(f, s);
end
else if ParentCtrl.Controls[i] is TButton then
begin
s:= TButton(ParentCtrl.Controls[i]).Caption;
Writeln(f, s);
end
else if ParentCtrl.Controls[i] is TCheckBox then
begin
s:= TCheckBox(ParentCtrl.Controls[i]).Caption;
Writeln(f, s);
end
else if ParentCtrl.Controls[i] is TRadioButton then
begin
s:= TRadioButton(ParentCtrl.Controls[i]).Caption;
Writeln(f, s);
end
else if ParentCtrl.Controls[i] is TGroupBox then
begin
s:= TGroupBox(ParentCtrl.Controls[i]).Caption;
Writeln(f, s);
end
else if ParentCtrl.Controls[i] is TRadioGroup then
begin
s:= TRadioGroup(ParentCtrl.Controls[i]).Caption;
Writeln(f, s);
end
else if ParentCtrl.Controls[i] is TPanel then
begin
s:= TPanel(ParentCtrl.Controls[i]).Caption;
Writeln(f, s);
end;
if ParentCtrl.Controls[i] is TWinControl then
SearchCtrl(TWinControl(ParentCtrl.Controls[i]));
end;
end;
begin
AssignFile(f, FileName);
Rewrite(f);
SearchCtrl(Parent);
CloseFile(f);
end;
Oczywiście nie uwzględniłem wszystkich kontrolek, ale to tylko przykład więc jak ktoś chce to może sobie dopisać.
W procedurach zastosowałem rekurencję tak aby przeszukać wszystkie kontrolki które leżą na podanej w 1 parametrze kontrolce.
Przykład użycia:
WriteControls(GroupBox1, 'C:\Test.txt');
ReadControls(GroupBox1, 'C:\Test.txt');
Caption kontrolki podawanej w parametrze (w przykładzie GroupBox1 nie jest zapisywany)
Pewnie można by to zoptymalizować ale mi się nie chce :P
A nie łatwiej użyć plików typowanych i zapisywać dane w postaci rekordów?