saving delphi routines and memory

748 views Asked by At

This question is about being able to save routines, and being able to select them from a list…. When you select one it knows what to link where etc. Just for understanding. not the actual program

Say I want to create a routine in a Delphi form. And I want to create several different ones. They wont be exactly the same but some might be similar. I want to know how you can save things in Delphi and when you close or terminate the application they will remain remembered when you reopen it. I have no idea where to start and how to work this. Any help would be great. Just a hint or a direction, maybe a website with more info or even examples. I’m going to try to give a simpler description below about how it would look on the form…. Just for the idea and I think if I understand this then it would be enough, or a good start at least.

The form will contain a list box a save button and 4 different edit boxes. Lets say I type in edit1;1 and edit2;2 and edit3;3 and edit4;4. Then click the save button and it remembers these 4 value to each edit box and lets say saves in under the value in the list box of ≔edit1.text + ‘to’ + edit4.text. Hope it makes sense so far and then I type in the edit boxes everything the wrong way around. edit1;4 and edit2;3 and edit3;2 and edit4;1. And click save button and it does that again (≔edit1.text + ‘to’ + edit4.text) into the list box. Then I want to close the application. Open it again and still have this in there and still be able to add more of these odd samples….

Can anyone help me?

Edit question, might make it more clear....

I'm going to place the following elements on the form: 2 listboxes(with each 3 lines, in the first listbox: wood, plastic and glass. in the second listbox: tree,cup,window.) Now I want to link the correct ones, they are in order here, but what is they were not. In a table or in a memory of the application which is not visible on the form I want to link them. Then if i were to put two edit boxes on the form as wel and I type in the first one wood or tree, it places the other one in the other edit box. So in a way I suppose you are creating a table which knows which one correcsponds with which but also looksup up when you type in edit box. hope that makes sense

1

There are 1 answers

9
Patrick Echterbruch On

From what you wrote I assume that you already know how to add the values to remember to your list box, so I won't deal with this part here. Starting with this answer, you should already have a clue to what to look at in the delphi help files. Please be aware that I didn't test the example, so there may be typos in it.

To store data in the computers registry, delphi provides you with the unit Registry, which contains a class TRegistry.

TRegistry has all the methods needed to retrieve and store values. Following is a short example without any practical use, just to give you the picture. Please be aware that, like mentioned in the comments, there's plenty of room left for you to optimise it. It would, for example, be better to create the TRegistry object only once and then repeatedly call the methods. And - while this plain old delphi unit I wrote is syntactically valid - you might be better off using a more object-oriented approach, like deriving a new class from TRegistry. Please do also check the documentation for the return values of the methods, as some (like OpenKey) simply return false when they are unable to fulfill your request while others (like ReadString) can throw an execption.

unit Unit1;

interface
uses TRegistry, Windows;

procedure saveStringToRegistry(name : String; value : String);
function readIntegerFromRegistry(name : String) : Integer;

implementation

procedure saveStringToRegistry(name : String; value : String);
var r : TRegistry;
begin
   r := TRegistry.Create;
   try
      r.RootKey := HKEY_CURRENT_USER; // Defined in unit Windows, as well as HKEY_LOCAL_MACHINE and others.
      r.OpenKey('Software\Manufacturer Name\Produkt Name\Other\Folders',true); // TRUE allows to create the key if it doesn't already exist
      r.WriteString(name,value); //Store the contents of variable 'value' in the entry specified by 'name'
      r.CloseKey;
   finally
      r.Free;
   end;
end;

function readIntegerFromRegistry(name : String) : Integer;
var r : TRegistry;
begin
   r := TRegistry.Create;
   try
      r.RootKey := HKEY_LOCAL_MACHINE; // Defined in unit Windows
      r.OpenKey('Software\Manufacturer Name\Produkt Name\Other\Folders',false); // FALSE: do not create the key, fail if it doesn't already exist
      result := r.ReadInteger(name);
      r.CloseKey;
   finally
      r.Free;
   end;
end;

end.

OK, now you could use a for loop in your application to process all the items of your list box and save it to the registry using the procedure above, like this:

for i := 0 to ListBox1.Count -1 do
   saveStringToRegistry('Entry' + IntToStr(i),ListBox1.Items[i];

Then, you would probably save the number of items you have (of course you would have to define the procedure saveIntegerToRegistry):

saveIntegerToRegistry('NumberOfEntries',ListBox1.Count);

When you need to reload the data, you could do:

ListBox1.Clear;
for i := 0 to readIntegerFromRegistry('NumberOfEntries') -1 do
   ListBox1.Items.Add(readStringFromRegistry('Entry' + IntToStr(i));

OK, it's a very basic example, but should give you pointer in the right direction. It could of course need some exception handling (imagine the user accidently deleted Entry42 from the registry, but NumberOfEntries still says there are 55 entries).