0

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

dave123
  • 421
  • 1
  • 6
  • 15
  • How many different routines do you expect to save? It it's only a couple of them, then you can use the registry or a flat file, if there might be hundreds or thousands then a database would probably be the best choice. – Patrick Echterbruch Feb 06 '11 at 16:07
  • 1
    I can't make any sense out of this. What is a routine? – David Heffernan Feb 06 '11 at 16:28
  • Are you wanting to save snippets of code? What about a text editor? I recommend Notepad++. – David Heffernan Feb 06 '11 at 16:37
  • @Partick Echterbruch - Lets say only 10 saves. Nothing too extreem. I don't want to save hundreds or thousands. Do you have examples of how to use registry or flat file? or a site.... – dave123 Feb 06 '11 at 16:51
  • @David Heffernan - not snippets of code, just a memory of the program. which i can go back to later after having closed it. I just want it to remember a certain routine and when I select that routine and i fill in the data it knows what to connect to what. – dave123 Feb 06 '11 at 16:53
  • @ployo60 I'm sorry I think I get it. You want to remember state. Normally this is saved to the user profile. Either registry or a file within the user profile. Which do you prefer? Both are pretty easy. Registry is wrapped by `TRegistry`. For a file I would use `TMemIniFile`. – David Heffernan Feb 06 '11 at 16:57
  • Super thank you, i'm gonna do some research on that, do you possible have examples on a site? – dave123 Feb 06 '11 at 17:10
  • @ployo just do a web search and read the delphi doc on those classes – David Heffernan Feb 06 '11 at 17:28

1 Answers1

0

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).

  • 1
    A few problems: Your read/write methods are superfluous, they're wrapping the creation and destruction of a TRegistry instance only to call a single method. Using those methods in a loop causes TRegistry calsses to be created and destroy every time: not good. The ReadXXX method doesn't check the return value of `r.OpenKey`: if the key doesn't exist, and it's not created, the following line (r.ReadXXX) will raise an exception. – Cosmin Prund Feb 06 '11 at 17:54
  • @Cosmin Prund - how do you recommend to do this then? – dave123 Feb 06 '11 at 19:25
  • @ployo60 Why don't you try to do it yourself. When you get stuck, ask again. It's really not that hard. – David Heffernan Feb 06 '11 at 19:34
  • @ployo60, follow David's advice: you got a few ideas, give them a try yourself. – Cosmin Prund Feb 06 '11 at 20:18
  • Ok, ill get going on it, let you know when I need more help! thx to you both! – dave123 Feb 06 '11 at 20:41
  • @Cosmin: You're right, but this was never thought to be a complete solution. I just wanted to show how to use the class, because I personally find it easier to look something up in the docs when I have some kind of working example. I will edit the answer to point out that this is not an optimal way. – Patrick Echterbruch Feb 06 '11 at 22:30
  • @Patrick, you didn't address any of the issues I mentioned, all you did was add a comment about the code not being optimal. It's still buggy, not only inefficient. – Cosmin Prund Feb 07 '11 at 05:36
  • @Patrick @Cosmin One of the wrinkles of TRegistry is that attempting to read a missing value results in an exception. When you are dealing with preferences you want to use an interface where you can pass in a default value which will be used in case the value is missing. That's why I personally wrap TRegistry inside my own class. I'm sure that there are open source classes for this sort of thing. JEDI must have a settings class that exposes a more friendly interface. `TMemIniFile` is actually more directly usable. – David Heffernan Feb 07 '11 at 09:51
  • I edited the question for who still feel like giving me a hint – dave123 Feb 10 '11 at 12:06