Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Save current state.

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 !

Comments

  • zibadianzibadian Posts: 6,349Member
    : 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:
    [code]
    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;
    [/code]
    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.
  • porodoroporodoro Posts: 230Member
    [b][red]This message was edited by porodoro at 2005-12-31 7:18:25[/red][/b][hr]
    [b][red]This message was edited by porodoro at 2005-12-31 7:17:48[/red][/b][hr]
    [b][red]This message was edited by porodoro at 2005-12-31 7:16:54[/red][/b][hr]
    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

    [b] [color=red]if (Components[i]) = (TEdit) then[/color] [/b]

    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 [b]highlighted[/b] line/

    error :

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



    And one more thing. How to restore the position ?


  • porodoroporodoro Posts: 230Member
    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.

  • zibadianzibadian Posts: 6,349Member
    : [b][red]This message was edited by porodoro at 2005-12-31 7:18:25[/red][/b][hr]
    : [b][red]This message was edited by porodoro at 2005-12-31 7:17:48[/red][/b][hr]
    : [b][red]This message was edited by porodoro at 2005-12-31 7:16:54[/red][/b][hr]
    : 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
    :
    : [b] [color=red]if (Components[i]) = (TEdit) then[/color] [/b]
    :
    : 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 [b]highlighted[/b] 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:
    [code]
    if Components[i] [b]is[/b] TEdit then
    [/code]
    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().
  • porodoroporodoro Posts: 230Member
    : : [b][red]This message was edited by porodoro at 2005-12-31 7:18:25[/red][/b][hr]
    : : [b][red]This message was edited by porodoro at 2005-12-31 7:17:48[/red][/b][hr]
    : : [b][red]This message was edited by porodoro at 2005-12-31 7:16:54[/red][/b][hr]
    : : 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
    : :
    : : [b] [color=red]if (Components[i]) = (TEdit) then[/color] [/b]
    : :
    : : 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 [b]highlighted[/b] 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:
    : [code]
    : if Components[i] [b]is[/b] TEdit then
    : [/code]
    : 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 !.

Sign In or Register to comment.