保存delphi例程和内存

发布于 2024-10-16 00:54:35 字数 917 浏览 6 评论 0原文

这个问题是关于能够保存例程,并能够从列表中选择它们……。当您选择一个时,它知道要链接什么内容等。只是为了理解。不是实际的程序

假设我想以 Delphi 形式创建一个例程。我想创建几个不同的。它们不会完全相同,但有些可能相似。我想知道如何在 Delphi 中保存内容,并且当您关闭或终止应用程序时,当您重新打开它时它们将被记住。我不知道从哪里开始以及如何开展这项工作。任何帮助都会很棒。只是一个提示或方向,也许是一个包含更多信息甚至示例的网站。我将尝试在下面给出关于它在表单上的外观的更简单的描述......只是为了这个想法,我想如果我理解了这一点那就足够了,或者至少是一个好的开始。

该表单将包含一个列表框、一个保存按钮和 4 个不同的编辑框。假设我输入 edit1;1 和 edit2;2 以及 edit3;3 和 edit4;4。然后单击“保存”按钮,它会将这 4 个值记住到每个编辑框,并保存在 ≔edit1.text + 'to' + edit4.text 列表框中的值下。希望到目前为止它是有意义的,然后我在编辑框中输入所有错误的方式。编辑1;4和编辑2;3和编辑3;2和编辑4;1。然后单击保存按钮,它会再次执行此操作(≔edit1.text + 'to' + edit4.text)到列表框中。然后我想关闭该应用程序。再次打开它,仍然有这个,并且仍然能够添加更多这些奇怪的样本......。

谁能帮助我吗?

编辑问题,可能会更清楚......

我将在表单上放置以下元素:2个列表框(每3行,在第一个列表框中:木材,塑料和玻璃。在第二个列表框中:树、杯子、窗户。) 现在我想链接正确的,它们在这里是按顺序排列的,但它们是什么不是。在表格或应用程序的内存中,在表单上不可见,我想链接它们。 然后,如果我在表单上放置两个编辑框,然后输入第一个木头或树,它会将另一个编辑框放置在另一个编辑框中。因此,在某种程度上,我认为您正在创建一个表格,该表格知道哪个表格与哪个表格相对应,而且当您在编辑框中键入内容时也会进行查找。希望这是有道理的

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事与诗 2024-10-23 00:54:35

根据您所写的内容,我假设您已经知道如何将要记住的值添加到列表框中,因此我不会在这里处理这部分。从这个答案开始,您应该已经知道要在 delphi 帮助文件中查看哪些内容。请注意,我没有测试该示例,因此其中可能存在拼写错误。

为了在计算机注册表中存储数据,delphi 提供了单元Registry,它包含一个TRegistry 类。

TRegistry 拥有检索和存储值所需的所有方法。以下是一个简短的示例,没有任何实际用途,只是为了给您提供图片。请注意,正如评论中提到的,还有足够的空间供您优化。例如,最好只创建一次 TRegistry 对象,然后重复调用这些方法。而且 - 虽然我编写的这个简单的旧 delphi 单元在语法上是有效的 - 你可能最好使用更面向对象的方法,比如从 TRegistry 派生一个新类。另请检查文档以了解方法的返回值,因为有些方法(例如 OpenKey)在无法满足您的请求时仅返回 false,而另一些方法(例如 < code>ReadString) 可以抛出异常。

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.

好的,现在您可以在应用程序中使用 for 循环来处理列表框中的所有项目,并使用上面的过程将其保存到注册表,如下所示:

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

然后,您可能会保存数字您拥有的项目的数量(当然您必须定义过程 saveIntegerToRegistry):

saveIntegerToRegistry('NumberOfEntries',ListBox1.Count);

当您需要重新加载数据时,您可以这样做:

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

好的,这是一个非常基本的示例,但应该为您提供正确方向的指针。当然,它可能需要一些异常处理(想象一下用户意外地从注册表中删除了 Entry42,但 NumberOfEntries 仍然显示有 55 个条目)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文