Dialog CheckBox Checked Property Setting By Text File

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
MadQuestion
Pro Scripter
Posts: 70
Joined: Wed Nov 08, 2017 6:54 pm

Dialog CheckBox Checked Property Setting By Text File

Post by MadQuestion » Fri Dec 08, 2017 2:49 am

Hello Im trying to save the checked value of a dialog check box object into a txt file in the temp dir the file is created and then when the dialog loads I wanted it to load the check box property Checked from the txt file it created. If I manually go properties Checked and set it to True I have a checked CheckBox when it loads but I want a routine to do this by creating a Dialog handler to load the checked variable. See code below.

Code: Select all


Dialog>Dialog1
object Dialog1: TForm
  Left = 529
  Top = 174
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 212
  ClientWidth = 476
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object Test_CheckBox1: TCheckBox
    Left = 13
    Top = 15
    Width = 97
    Height = 17
    Caption = 'Test Check Box'
    TabOrder = 0
  end
end
EndDialog>Dialog1

//Save The Checked Or Not To File
SRT>SAVECHECKEDTESTFILE
GetDialogProperty>Dialog1,Test_CheckBox1,Checked,CHECKSAVE
IfFileExists>%TEMP_DIR%\TESTSAVEFILE.txt,SAVEFOUND,NOTSAVED
  Label>SAVEFOUND
  DeleteFile>%TEMP_DIR%\TESTSAVEFILE.txt
  Label>NOTSAVED
  WriteLn>%TEMP_DIR%\TESTSAVEFILE.txt,nWLNRes,%CHECKSAVE%
Endif
END>SAVECHECKEDTESTFILE

//Load The Checked Save File
SRT>LOADCHECKSAVEFILE
IfFileExists>%TEMP_DIR%\TESTSAVEFILE.txt,SAVEEXISTS,SAVEDENIED
  Label>SAVEEXISTS
  ReadFile>%TEMP_DIR%\TESTSAVEFILE.txt,CHECKVARLOADED
  SetDialogProperty>Dialog1,Test_CheckBox1,Checked,%CHECKVARLOADED%
  Label>SAVEDENIED
Endif
END>LOADCHECKSAVEFILE

AddDialogHandler>Dialog1,,OnActivate,LOADCHECKSAVEFILE
AddDialogHandler>Dialog1,Test_CheckBox1,OnClick,SAVECHECKEDTESTFILE
Show>Dialog1,res3
I hope someone can point out why I cant set the Checked Property of the checkBox so that I can save if it is selected or not to a text file and then load it from there....

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Dialog CheckBox Checked Property Setting By Text File

Post by Marcus Tettmar » Fri Dec 08, 2017 7:52 am

Could be because you are using OnActivate to set the value and it deletes the file. Activate is called whenever someone switches to the form, not just on startup. It could be called a million times. Why not just load the stored values before opening the form?

I'm not sure why you have all those labels either. Try this example, it works for me:

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  object chkRememberMe: TCheckBox
    Left = 34
    Top = 30
    Width = 97
    Height = 17
    Caption = 'Remember Me'
    TabOrder = 8
  end
end
EndDialog>Dialog1

//restore the checkbox if settings file exists ..
GoSub>LoadStoredValues

//add a handler to store value when clicked (though since dialog is modal we could just do this after the Show instead of using a handler)
AddDialogHandler>Dialog1,chkRememberMe,OnClick,StoreVal
Show>Dialog1,r

//the OnClick handler
SRT>StoreVal
  GetDialogProperty>Dialog1,chkRememberMe,Checked,strChecked
  IfNotFileExists>%SCRIPT_DIR%\settings.ini
    WriteLn>%SCRIPT_DIR%\settings.ini,res,[settings]
  Endif
  EditIniFile>%SCRIPT_DIR%\settings.ini,settings,RememberMe,strChecked
END>StoreVal

//subroutine to load stored values from settings file
SRT>LoadStoredValues
IfFileExists>%SCRIPT_DIR%\settings.ini
  ReadIniFile>%SCRIPT_DIR%\settings.ini,settings,RememberMe,CheckVal
  SetDialogProperty>Dialog1,chkRememberMe,Checked,CheckVal
Endif
End>LoadStoredValues
Paste this into a new macro and run. No need to change anything. Run it, check the checkbox and close the dialog. Then run the macro again and you'll see the checkbox is now checked. Etc.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

MadQuestion
Pro Scripter
Posts: 70
Joined: Wed Nov 08, 2017 6:54 pm

Re: Dialog CheckBox Checked Property Setting By Text File

Post by MadQuestion » Sat Dec 09, 2017 3:14 am

Thanks Marcus One More Question in the line of code below what are the settings and RememberMe parameters and what are they referring to? MIght be a silly question but i was just wondering as I have never thought about using an ini file as i was unsure as to what exactly they are used for which is why I was using ReadFile> and WriteLn>

Code: Select all

ReadIniFile>%SCRIPT_DIR%\settings.ini,settings,RememberMe,CheckVal

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Dialog CheckBox Checked Property Setting By Text File

Post by Marcus Tettmar » Sat Dec 09, 2017 9:49 am

An ini file looks like this:

[section_name]
settingname=value

So I used:

[settings]
RememberMe=True/False

So 'settings' is the name of the section and RememberMe is the name of the value I want to store. I could have used anything but wanted to use something sensible. But perhaps it was still too esoteric for you.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts