saving strings that are 'connected' and reading them and their 'connected' [closed]
This is something I am not at all familiar with.
I want to try to make a simple form with 4 edit boxes, 2 at the top, 2 at the bottom, and a button. Basically what I want to do is type a couple of things in the top two boxes that are related to each other.
When I have them both filled in I click on the button and it saves this information in a database, preferable an external file (doesn't have to be text, I think it would be better if not). So I can do that a couple of times. Saving from the edit fields into a database.
Then when I type one of the words saved in one of the edit fields at the bottom it automatically types the other word in the last edit field. The form should remember to connect to the database every time it's opened so that when I open it another time I can still work the edit fields.
Can anyone advise me on how to do this?
What you are looking for is known as a dictionary, if I understand you correctly. In other languages it is known as an associative array or sometimes a hash.
You are going to want a modern version of Delphi, I'd guess 2010 or XE. If you can't access those then you'd need a 3rd party library, or a home grown based off a TStringList
. In fact TStringList
can operate in a dictionary like mode but it's a bit clunky.
You declare the dictionary as follows:
dict: TDictionary<string,string>;
You can add do it as follows:
dict.Add(box1.Text, box2.Text);
The first parameter is the key. The second is the value. Think of this as an array but indexed with a string rather than an integer.
If you want to recover a value then you use:
dict[key];
In your case you would write:
box4.Text := dict[box3.Text];
If you want to save to a file then you would iterate over the dict:
var
item: TPair<string,string>;
...
for item in dict do
AddToTextFile(item.Key, item.Value);
I've ignored all error handling issues, dealing with adding keys that already exist, asking for keys that are not in the dict, and so on. But this should give you a flavour.
I'd recommend reading up on associative arrays if you aren't already familiar with them. I'm sure there will be a page on Wikipedia and you would do worse than read an tutorial on Python which is sure to cover them – the issues are really the same no matter what language you consider.
精彩评论