Topic: Changing Label Text?

After changing label text, keeping changed label text after restart the application; without the use of the registry?


Any solution please?

Post's attachments

Attachment icon LabelChange.zip 3.63 kb, 275 downloads since 2019-08-11 

Adam
God... please help me become the person my dog thinks I am.

2 (edited by derek 2019-08-11 11:32:23)

Re: Changing Label Text?

Hi Adam,
From your attached example, I'm not sure why you want to save each change of label as a new record in the 'label' table (unless you're going to subsequently refer to them).
Anyway, in the attachment are two suggestions.  In the first option, each label change is saved as a new record (as per your example).
In the second option, it inserts a record into the 'label' table only if that table is empty and subsequently just overwrites the record with whatever you want the changed label to be.
I'm sure there are other ways as well.
Derek.

Post's attachments

Attachment icon label change.zip 673.09 kb, 305 downloads since 2019-08-11 

3 (edited by AD1408 2019-08-11 12:52:02)

Re: Changing Label Text?

Hi Derek,


Great stuff as usual...........
I Liked your solution, which is the correct approach - I see it now. However, thank you very much for both version...........
Truly appreciated.........................

Adam
God... please help me become the person my dog thinks I am.

Re: Changing Label Text?

Hello AD1408 and Derek,


I do not post very often these days, but still read the forum when a new question is posted, and this one is interesting.
As Derek said, there are other ways you can do that, and that is by using ini files.


A LITTLE THEORY ABOUT INI FILES


You already have ini files in your application folder :

  • settings.ini

  • tables.ini

and you are free to add some more if you want.


They are basically text files in which you can save infos, settings or whatever you want.
Because they are plain text, I would not advise saving sensible data into them (like passwords for example), but as they are usually saved into user owned folders, they can be used to set different parameters for different users for the same application.


By using commands like (not sure MVD handles that yet)

GetSpecialFolderPath(CSIDL_APPDATA, False)

or

GetSpecialFolderPath(CSIDL_PERSONAL, False)

you can save those preferences into folders like C:\Users\XXX\AppData\Roaming or C:\Users\XXX\Documents and let every user choose it's own parameters.


Anyway, back to MVD now.

There is something a little confusing about ini files in Delphi, and it as that, EVEN IF THE FILE EXISTS ON THE DISK, you have to create it. Yes, said like this, is sounds a little dumb, but you'll understand.


The ini file might already exist somewhere, but in order for MVD to use it, it must be instantiated in memory, that's why it is created as a kind of data container in memory. It can then be accessed for reading, or saving updated data or new data.


The structure of an ini file is composed of :

  • sections, that are enclosed into [ and ], and are like "chapters" if you want, a convenient way to order data by family

  • keys, that actually contain the data you want to read or save


Here is an example (that should not be used, it is just for demonstration) :

[DATABASE]
login=root
password=mypassword

[SERVER]
address=192.168.0.155
port=80

As you can see, there are two sections in this file, because it is clearer for organizing data, but it is optionnal.
keys are associated to their values by the = sign.


Getting the database password in this example is just a matter of reading the password key in the DATABASE section. And it is done very simply by the following command :

IFile.Readstring('DATABASE','password','');

Here is the detail of the instruction :

  • IFile is the name I chose to give to the instance of Tinifile

  • the first part of the parenthesis is the name of the section where to find the info

  • the second part of the parenthesis is the name of the key we want to read

  • the last part of the parenthesis is a default value


Writing data to the ini file is as easy, and is done like this :

IFile.WriteString('DATABASE','password','myNewPassword');

Here is the detail of the instruction :

  • IFile is the name I chose to give to the instance of Tinifile

  • the first part of the parenthesis is the name of the section where you want to writeo

  • the second part of the parenthesis is the name of the key for which you want to change the value

  • the last part of the parenthesis is a value you want to save


NOTE : if the key already exists, it will be updated by overwriting the previous value, and if it does not exist, it will be added to the corresponding section.


Now, to come back to you example, changing labels and retaining those captions after each restart, here is how you could do it :

  • on application start (well onshow form in MVD), check if the ini file exists

  • if the file exists, create it's "copy" in memory

  • read the key value in the corresponding section

  • apply the change to the label

  • Free the memory you located to the ini file


//-01-LOADING THE INIFILE ON SHOW
procedure Form1_OnShow (Sender: TObject; Action: string);
var
    IFile : Tinifile;
    LabelCaption : String;
begin
    //IF THE INI FILE EXISTS, THE SOFTWARE WILL READ IT TO LOAD AN ALTERNATE CAPTION FOR LABEL1
    //IF NOT, THE DEFAULT CAPTION WILL BE DISPLAYED
    If FileExists(ExtractFilePath(Application.ExeName)+'labels.ini') then
        begin
            IFile := TIniFile.Create(ExtractFilePath(Application.ExeName)+'labels.ini');
                try
                    LabelCaption := IFile.Readstring('LABELS', 'label1','');
                    Form1.Label1.Caption := LabelCaption;
                finally
                    IFile.Free;
                end;
        end;
end;

Saving a new label is done nearly in the same way, with a little difference : if the ini file does not exist, it will be automatically created, and if it already exists, it will be updated, so no need to check it's existence.


//-02-WRITING NEW DATA TO THE INIFILE
procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
var
    IFile : Tinifile;
    NewLabelCaption : String;
begin
    NewlabelCaption := Trim(Form1.Edit1.Text);
        if Length(NewlabelCaption) > 0 then
           begin
               IFile := TIniFile.Create(ExtractFilePath(Application.ExeName)+'labels.ini');
                   try
                        IFile.WriteString('LABELS', 'label1',NewlabelCaption);
                        Form1.Label1.Caption := NewlabelCaption;
                    finally
                        IFile.Free;
                    end;
           end
        else if Length(NewlabelCaption) = 0 then
            begin
                ShowMessage('New text is empty, current caption will not be changed');
            end;
end;

I attach the example to my answer (MVD 5.5 trial version, I have not renewed my licence yet).
When this example application starts, there is not ini file in the folder, but as soon as you type in a new caption, the ini file is created and the data saved.
When you restart the application, the default caption is changed to the saved value.
This example is very simple, but you can use as many sections and keys you want.


One last thing : ini files must be readable, so they can not be stored under "program files" folders.


Have a good day both of you and talk to you soon


Cheers



Mathias

Post's attachments

Attachment icon INIFiles.zip 333.98 kb, 306 downloads since 2019-08-12 

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: Changing Label Text?

HI Mathias,


Glad to see you around...


Thank you very much for the great tutorial about INI files and example.


Enjoy your days and weeks.......

Adam
God... please help me become the person my dog thinks I am.