Delphi and Kylix

Moderators: pritaeas
Number of threads: 7264
Number of posts: 19073

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Save current state. Posted by porodoro on 30 Dec 2005 at 10:21 PM
Hi all and Happy holidays! :).

1)How can i save the state (each object,button pos ,form's caption etc) in another file (during runtime)?

2)How can i send files through Sockets ?


---

thanks !
Report
Re: Save current state. Posted by zibadian on 31 Dec 2005 at 12:51 AM
: Hi all and Happy holidays! :).
:
: 1)How can i save the state (each object,button pos ,form's caption etc) in another file (during runtime)?
:
: 2)How can i send files through Sockets ?
:
:
: ---
:
: thanks !
:
1: You need to loop through each component and check its type. Then you can type class it to access its properties:
  for i := 0 to ComponentCount-1 do
  begin
    if Components[i] = TEdit then
      with TEdit(Components[i]) do 
      begin
        // Save properties here
        // Example: writeln(f, Text);
      end
    else if Components[i] = TLabel then
      with TLabel(Components[i]) do 
      begin
        // Save properties here
        // Example: writeln(f, Caption);
      end
  end;

This is the easiest way, but a bit bulky. There are more elegant ways, which make use of the inheritance of the objects or even the streaming capabilities.

2: That greatly depends on which components you use. In the TCustomWinSocket component you can use the SendStream() with a TFileStream object as parameter. Receiving the file is done using a TWinSocketStream. You need to call the WaitForData() and if the data is received, copy the stream into a TFileStream object to save the file to disk.
Indy's TidTIdTCPConnection defines a WriteStream() and ReadStream() with the same capabilities.
Report
Re: Save current state. Posted by porodoro on 31 Dec 2005 at 7:16 AM
This message was edited by porodoro at 2005-12-31 7:18:25

This message was edited by porodoro at 2005-12-31 7:17:48

This message was edited by porodoro at 2005-12-31 7:16:54

I've tryed this :

VAR I:INTEGER;
VAR F:TEXTFILE;
begin
AssignFile(F, 'C:\STATE.TXT');
Reset(F);
for i := 0 to ComponentCount-1 do
begin

if (Components[i]) = (TEdit) then

with TEdit(Components[i]) do
begin
// X
writeln(f, inttostr(TEdit(Components[i]).Left));
// Y
writeln(f, inttostr(TEdit(Components[i]).Left));
// WIDTH
writeln(f, inttostr(TEdit(Components[i]).WIDTH));
// HEIGHT
writeln(f, inttostr(TEdit(Components[i]).HEIGHT));
//text
writeln(f, TEdit(Components[i]).text);
end;
END;

Compiler's return error at the highlighted line/

error :

[i] [Error] Unit1.pas(38): Incompatible types [/i]



And one more thing. How to restore the position ?


Report
Re: Save current state. Posted by porodoro on 31 Dec 2005 at 11:29 AM
I found a better way :
ini:=tinifile.Create(
extractfilepath(application.ExeName)
+'ProgrammersHeavenTest.INI'
);

for i := 0 to ComponentCount-1 do
begin
if Components[i] is (TEdit) then
try
with TEdit(Components[i]) do
begin

ini.WriteString('TXT',name,text);
ini.WriteInteger('POSX',name,left);
ini.WriteInteger('POSY',name,top);
//
ini.WriteIntegeR('Count','totalObjects',i);
end;
except end;
end; end;

---
reading that value :

ini.WriteIntegeR('Count','totalObjects',i);

its easy to find pos(x,y) , text etc of each object.

Anyway thanks.

Report
Re: Save current state. Posted by zibadian on 1 Jan 2006 at 9:39 AM
: This message was edited by porodoro at 2005-12-31 7:18:25

: This message was edited by porodoro at 2005-12-31 7:17:48

: This message was edited by porodoro at 2005-12-31 7:16:54

: I've tryed this :
:
: VAR I:INTEGER;
: VAR F:TEXTFILE;
: begin
: AssignFile(F, 'C:\STATE.TXT');
: Reset(F);
: for i := 0 to ComponentCount-1 do
: begin
:
: if (Components[i]) = (TEdit) then
:
: with TEdit(Components[i]) do
: begin
: // X
: writeln(f, inttostr(TEdit(Components[i]).Left));
: // Y
: writeln(f, inttostr(TEdit(Components[i]).Left));
: // WIDTH
: writeln(f, inttostr(TEdit(Components[i]).WIDTH));
: // HEIGHT
: writeln(f, inttostr(TEdit(Components[i]).HEIGHT));
: //text
: writeln(f, TEdit(Components[i]).text);
: end;
: END;
:
: Compiler's return error at the highlighted line/
:
: error :
:
: [i] [Error] Unit1.pas(38): Incompatible types [/i]
:
:
:
: And one more thing. How to restore the position ?
:
:
:
Sorry, I made a small mistake the bold line should be:
 if Components[i] is TEdit then

Similar with the other if-then.
The IntToStr()'s in the writeln()'s are not necessary, because Writeln() can take any number of nearly any variable type as parameters. Also you need to use Rewrite() instead of Reset() when writing textfiles.
Reading and restoring the position is done using the Readln().
Report
Re: Save current state. Posted by porodoro on 1 Jan 2006 at 11:17 AM
: : This message was edited by porodoro at 2005-12-31 7:18:25

: : This message was edited by porodoro at 2005-12-31 7:17:48

: : This message was edited by porodoro at 2005-12-31 7:16:54

: : I've tryed this :
: :
: : VAR I:INTEGER;
: : VAR F:TEXTFILE;
: : begin
: : AssignFile(F, 'C:\STATE.TXT');
: : Reset(F);
: : for i := 0 to ComponentCount-1 do
: : begin
: :
: : if (Components[i]) = (TEdit) then
: :
: : with TEdit(Components[i]) do
: : begin
: : // X
: : writeln(f, inttostr(TEdit(Components[i]).Left));
: : // Y
: : writeln(f, inttostr(TEdit(Components[i]).Left));
: : // WIDTH
: : writeln(f, inttostr(TEdit(Components[i]).WIDTH));
: : // HEIGHT
: : writeln(f, inttostr(TEdit(Components[i]).HEIGHT));
: : //text
: : writeln(f, TEdit(Components[i]).text);
: : end;
: : END;
: :
: : Compiler's return error at the highlighted line/
: :
: : error :
: :
: : [i] [Error] Unit1.pas(38): Incompatible types [/i]
: :
: :
: :
: : And one more thing. How to restore the position ?
: :
: :
: :
: Sorry, I made a small mistake the bold line should be:
:
:  if Components[i] is TEdit then
: 

: Similar with the other if-then.
: The IntToStr()'s in the writeln()'s are not necessary, because Writeln() can take any number of nearly any variable type as parameters. Also you need to use Rewrite() instead of Reset() when writing textfiles.
: Reading and restoring the position is done using the Readln().
:
I think i will use ini's for mass restore.
Anyway , thanks !.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.