CheckListBox Status Persistence lazarus / 31-LCL ---------------------------------------------------------------------- greertr · 2017-09-07 10:47 Hi all, Is there a way to ensure persistence of a checklist box each time an application is executed?  For example, if a couple of boxes are checked, then next time the program is run, I want those boxes to still be checked. thx in adv ---------------------------------------------------------------------- minesadorada · 2017-09-07 11:36 My suggestion: If the application is run locally (i.e. not a web/network app) then you can store the checkbox state (as a bool value) in a config file in formOnClose, and read the values in at form OnLoad. Use GetAppConfigFile function for the config file's name and location, and TINIFiles unit to read/write. This is Windows and Linux compatible. ---------------------------------------------------------------------- greertr · 2017-09-07 13:17 ...umm realizing how ignorant of Lazarus I truly am  Well, I have the checklistbox1 saving an alpha character ('x'), but i can't get tp reload when i start the program again. The Save code seems to work, I checked the xml file and did see the 'x' in there. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm4.Button3Click(Sender: TObject);begin   if Checklistbox1.Checked[0]    then form4.stringgrid1.cells[0,0] := 'x'  else form4.stringgrid1.cells[0,0] := '' ;   if Checklistbox1.Checked[1]    then form4.stringgrid1.cells[1,0] := 'x'  else form4.stringgrid1.cells[1,0] := '' ;   if Checklistbox1.Checked[2]    then form4.stringgrid1.cells[2,0] := 'x'  else form4.stringgrid1.cells[2,0] := '' ;   if Checklistbox1.Checked[3]    then form4.stringgrid1.cells[3,0] := 'x'  else form4.stringgrid1.cells[3,0] := '' ;     if Checklistbox1.Checked[4]    then form4.stringgrid1.cells[4,0] := 'x'  else form4.stringgrid1.cells[4,0] := '' ;     if Checklistbox1.Checked[5]    then form4.stringgrid1.cells[5,0] := 'x'  else form4.stringgrid1.cells[5,0] := '' ;     stringgrid1.SaveOptions := [soDesign,soPosition,soAttributes,soContent];    form4.StringGrid1.SaveToFile('trgtasks.xml'); end; But the Load code no worky... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm4.FormCreate(Sender: TObject);begin  form4.StringGrid1.LoadFromFile('trgtasks.xml');   if (form4.stringgrid1.cells[0,0] = 'x')    then  Checklistbox1.Checked[0];end; The xml snippet... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i                             ---------------------------------------------------------------------- howardpc · 2017-09-07 15:58 A very simple-minded approach would be something like this. Drop a checklistbox on a form in a new Lazarus project, add a few items to it, and then add OnCreate and OnClose form handlers as follows: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, CheckLst; type   TForm1 = class(TForm)    CheckListBox1: TCheckListBox;    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);    procedure FormCreate(Sender: TObject);  private    const      ChkLBFileDataName = 'checklistbox.dat';    procedure SaveListBoxSettings(aChkLB: TCheckListBox);    procedure LoadCheckListBoxSettings(aChkLB: TCheckListBox);  end; var  Form1: TForm1; implementation {$R *.lfm} procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);begin  SaveListBoxSettings(CheckListBox1);end; procedure TForm1.FormCreate(Sender: TObject);begin  LoadCheckListBoxSettings(CheckListBox1);end; procedure TForm1.LoadCheckListBoxSettings(aChkLB: TCheckListBox);var  sl: TStringList;  i: Integer = 0;  s: String;begin  if Assigned(aChkLB) and FileExists(ChkLBFileDataName) then begin    sl:=TStringList.Create;    try      sl.LoadFromFile(ChkLBFileDataName);      Assert(sl.Count=aChkLB.Items.Count,'CheckListbox has changed since last data saved');      for s in sl do begin        case Length(s) of          0: aChkLB.Checked[i]:=False;          else aChkLB.Checked[i]:=True;        end;        Inc(i);      end;    finally      sl.Free;    end;  end;end; procedure TForm1.SaveListBoxSettings(aChkLB: TCheckListBox);var  sl: TStringList;  i: Integer;  s: String;begin  if Assigned(aChkLB) then begin    sl:=TStringList.Create;    try      for i:=0 to aChkLB.Items.Count-1 do begin        if aChkLB.Checked[i] then        s:='x'        else s:='';        sl.Add(s);      end;      sl.SaveToFile(ChkLBFileDataName);    finally      sl.Free;    end;  end;end; end. ---------------------------------------------------------------------- greertr · 2017-09-07 17:34 Thx a lot howardpc!  The example works perfectly and the code appears to be something I can work with and adapt ---------------------------------------------------------------------- PatBayford · 2017-09-07 20:05 In Delhi this would have been handled by the IDE, using the built-in persistence models. This was sometimes a real nuisance if you did not want the data to be persistent! However, I don't think I ever used a CheckListBox for real, so it's behaviour may have been different. Do I assume that the default Lazarus behaviour is different? And if so, why not just use the equivalent of SaveComponent/LoadComponent? The actual methods are :- TStream.WriteComponent( Instance: TComponent); Description WriteComponent writes the published properties of Instance to the stream, so they can later be read with TStream.ReadComponent. This method is intended to be used by an IDE, to preserve the state of a form or datamodule as designed in the IDE. WriteComponent simply calls WriteDescendant with Nil ancestor. AND TStream.ReadComponent        Reads component data from a stream ---------------------------------------------------------------------- howardpc · 2017-09-08 07:27 Quote from: PatBayford on September 07, 2017, 10:05:45 pm...why not just use the equivalent of SaveComponent/LoadComponent? The actual methods are :- TStream.WriteComponent( Instance: TComponent); AND TStream.ReadComponent      There are nearly always multiple ways to accomplish a programming task. ReadComponent/WriteComponent is certainly a good way, though it obviously creates a slightly bigger data file (about 200 bytes for all published properties) than a customised solution that saves only the boolean data the OP asked about (whose file size is typically about 10 bytes depending on the number of checkbox items). ---------------------------------------------------------------------- greertr · 2017-09-08 12:17 i got the above problem resolved with howard pc's assistance, and now for the next issue - if all the boxes are checked, i want an image to show (it does), and then if a box is later unchecked, i want the image to vanish.  I've almost got it working, but when i uncheck a box, the image still shows Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm13.Button2Click(Sender: TObject);var i,j: integer;        // Update button routinebegin    SaveListBoxSettings(CheckListBox1);    LoadCheckListBoxSettings(CheckListBox1);    i := 0;    j := 0;    for i := 0 to listbox1.count - 1 do    begin      if checklistbox1.checked[i] then inc (j);      if (j = listbox1.count) then form1.image2.visible := true;      if (j = listbox1.count) then form1.Label8.visible := true;      if (j < listbox1.count) then form1.image2.visible := false;      if (j < listbox1.count) then form1.Label8.visible := false;    end;end; ---------------------------------------------------------------------- taazz · 2017-09-08 12:36 1) check for the count after the loop has finished counting them Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     procedure TForm13.Button2Click(Sender: TObject);    var     i,j: integer;    begin        SaveListBoxSettings(CheckListBox1);        LoadCheckListBoxSettings(CheckListBox1);        i := 0;        j := 0;        for i := 0 to listbox1.count - 1 do        begin          if checklistbox1.checked[i] then inc (j);          //if not checklistbox1.checked[i] then dec (j);        end;        if (j = listbox1.count) then form1.image2.visible := true;        if (j < listbox1.count) then form1.image2.visible := false;        if (j = listbox1.count) then form1.Label8.visible := true;        if (j < listbox1.count) then form1.Label8.visible := false;        //invalidate;    end;  2) You can simplify the code to Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i         form1.image2.visible := (j = listbox1.count);        form1.Label8.visible := (j = listbox1.count); if it still does not hide the image and label then 1) search your code if you activate the image somewhere else or you have the same image in an other tImage etc. 2) uncomment the invalidate call on my code and see if that helps. ---------------------------------------------------------------------- fred · 2017-09-08 12:36 You can try this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i j := 0;for i := 0 to listbox1.count - 1 dobegin  if checklistbox1.checked[i]   then inc (j);end;// you don't need a begin end because there is only one statement in the for loop but it might be more clear// you could name j as checked_count to make it more clear form1.image2.visible := (j = listbox1.count);form1.Label8.visible := form1.image2.visible; ---------------------------------------------------------------------- howardpc · 2017-09-08 12:38 What is the relationship between listbox1 and checkListbox1? ---------------------------------------------------------------------- taazz · 2017-09-08 12:40 Quote from: howardpc on September 08, 2017, 02:38:10 pmWhat is the relationship between listbox1 and checkListbox1? lol, and I missed too.  good catch! ---------------------------------------------------------------------- greertr · 2017-09-08 12:42 none, i was just using code from another posting... guess i thought list box would do the trick laz seems fuzzy to me at times lol i  changed list box to checklistbox and it works correctly now -- thx