1 (edited by abouyahya527911 2015-09-24 22:04:27)

Topic: Change text in label 1 with click

hi
i have button and Textbox and label  i want
when i type text in textbox and click button apply will change label1 and be saved even i close project and open it again

this work for me but the problem when i restart project is not saved go back as was before


procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
begin
   Form1.Label1.Caption := (Form1.Edit1.Text);
   end;

begin



end.

Re: Change text in label 1 with click

You can use system registry for save and restore text, example:

CONST
   APP_NAME = 'MyAppName'; //your unique application name, used as an identifier, to store information in the system registry
   
   
procedure Form1_OnShow (Sender: string; Action: string);
begin
    if LoadText<>'' then Form1.Label1.Caption := LoadText;
end;

procedure Form1_OnClose (Sender: string; Action: string);
begin
     SaveText(Form1.Label1.Caption);
end;   
   

procedure SaveText (s: string);
var
   reg: TRegistry;
begin
     reg := TRegistry.Create;
     reg.Access := KEY_ALL_ACCESS;
     reg.RootKey := HKEY_CURRENT_USER;

     if reg.OpenKey('software\' + APP_NAME, true) then
     begin
          Reg.WriteString('LabelText', s);
          reg.CloseKey;
     end;

     reg.Free;
end;


function LoadText: string;
var
   reg: TRegistry;
begin
     result := '';
     reg := TRegistry.Create;
     reg.Access := KEY_ALL_ACCESS;
     reg.RootKey := HKEY_CURRENT_USER;

     if reg.OpenKey('software\' + APP_NAME, true) then
     begin
          if reg.ValueExists('LabelText') then Result := Reg.ReadString('LabelText');
          reg.CloseKey;
     end;

     reg.Free;
end;
Dmitry.

Re: Change text in label 1 with click

Dmitry nice one Thanks works fine.

Re: Change text in label 1 with click

This looks a little bit "dirty" as if you share your application with somebody else, what has been saved in YOUR registry will not be handed over to another computer.