Search code examples
delphi-7

Need to retain the form's state on Delphi 7


I'm currently working with Borland Delphi 7 and I need to retain the Checkboxes on a Form checked when I close it, for the next time the user wants to make a new filter.

Edit: I have a form, "ordenes de servicios" that shows me Service Orders and their stats. I have many filters, and the date filter option opens up a new form, with checkboxes, so I can choose options as "begin date", "end date" and such. This form, "filtroDatas", when closed send for the "ordenes de servicios" a String that, roughly explaining, is a "WHERE" clause for a query in a Oracle Database. Currently, "filtroDatas", when closed, does not retain the checkboxes and dates used before, but i need to make it retain them. Looking up for data sheets on how the .FormClose works, i have the "caHide" option to just "hide" the form, but it does not retains information. The caMinimize is an invalid option, because the form should "dissapear" from sight.

Note: This is a legacy code that I can't alter too much. I though to do some reverse engeneering, but how the form is summons

procedure TfrmFiltrosData.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  (Owner as TfrmOrdensDeServico).HabilitaDesabilitaTimers(True);
  qryFiltrosOs.Close;
  Action := caHide;
  //frmFiltrosData := nil;
end;

I tried caMinimize as well, but it didn't work. Can some one shed some light on the matter?


Solution

  • As I understand it, you want to remember the checked status of various check boxes and other information such as dates.

    One method, and I stress one method but not the only method is to use an Ini Files.

    For example, in your Form Create you can read in the previous data like this;

    PROCEDURE TForm1.FormCreate(Sender: TObject);
    VAR
      MyIni: TMemIniFile;
    BEGIN
      MyIni := TmemInifile.create('inifile.ini');
      WITH MyIni DO
      BEGIN
        TRY
          checkbox1.Checked := readbool('Checkboxes_State', 'CheckBox1', False);
          checkbox2.Checked := readbool('Checkboxes_State', 'CheckBox2', False);
          checkbox3.Checked := readbool('Checkboxes_State', 'CheckBox3', False);
        FINALLY
          free;
        END;
      END;
    END;
    

    Note that I have not included a full path for the ini file, you should.

    The above reads the SECTION, ID, VALUE and sets a default value if it doesn't exist.

    If you want to save the status of your check boxes, edit boxes, or whatever, in your Form Close do something similar to this;

    PROCEDURE TForm1.FormClose(Sender: TObject; VAR Action: TCloseAction);
    VAR
      MyIni: TMemIniFile;
    BEGIN
      MyIni := TmemInifile.create('inifile.ini');
      WITH MyIni DO
      BEGIN
        TRY
          writebool('Checkboxes_State', 'CheckBox1', Checkbox1.Checked);
          writebool('Checkboxes_State', 'CheckBox2', Checkbox2.Checked);
          writebool('Checkboxes_State', 'CheckBox3', Checkbox3.Checked);
          UpdateFile;
        FINALLY
          free;
        END;
      END;
    END;
    

    When your form is created the values are read and when you exit the status is written.

    NOTE:

    TMemIniFile requires you add Uses IniFiles to either your INTERFACE or IMPLEMENTATION section, depending on your needs. In the case of the above examples I've used IMPLEMENTATION.


    There are various other methods associated with TMemIniFile;

    • ReadString / WriteString
    • ReadDate / WriteDate
    • ReadInteger / WriteInteger

    You can also read an entire section in one go if need be.

    With regard to dates, as David has explained in this answer Delphi inifiles ReadDateTime you need to be aware of local format settings. Dates can easily screw things up if you think you're working with a MONTH but are actually fiddling with the DAY.

    If you consider later upgrading your app to run on a Mobile platform you might want to consider XML Files.

    I hope this has been helpful. I have a feeling I've forgotten something, but someone will point it out if I have.