1

Is it possible to save a new item in a combobox? (Delphi)

If you say for ex. Combobox1.Items.Add('Item'); then that item will appear in the combobox for the duration of the program's run, however if you close and reopen the program then that item is gone.

Is it possible to somehow add that item permanently through code at runtime?

Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
RaymondSWalters
  • 189
  • 3
  • 5
  • 12
  • 1
    You need to persist the value somewhere. To a file, or the registry. Probably under the user's profile. – David Heffernan Sep 27 '15 at 14:42
  • 1
    You can create "history" of combo box. I think that is David's idea. See this answer : http://stackoverflow.com/questions/5465590/auto-append-complete-from-text-file-to-an-edit-box-delphi – Val Marinov Sep 27 '15 at 14:48

3 Answers3

3

The easiest way is propably:

ComboBox1.Items.SaveToFile('SomeFile.txt');

And then:

ComboBox1.Items.LoadFromFile('SomeFile.txt');

Personally i just use any file database, like mysqlite to store program options , comboboxes, and stuff. At first it might appear bit over the top, but very often this "tiny piece of software" grows and it's much more convenient to store stuff in database, where You could e.g. order combobox items, assign them additional values and so on.

Best regards

Vancalar
  • 963
  • 1
  • 7
  • 15
3

A combo box, when first created, is always empty. That is, the TComboBox.Create constructor doesn't put anything in the list. However, you've probably never called that constructor directly. Instead, you use the IDE to place controls on forms, and then those controls appear on your forms at run time. The form adds items to the combo box as it loads the form description out of the form's DFM data.

You might have noticed DFM files in your project directory. The IDE creates those when you design your form with the Form Designer. When you compile your program, those files are linked to you EXE as resources.

If you want to change anything about how your form looks or what data it contains when first loaded, then you need to modify that resource. Windows provides ways of modifying resources, and Delphi provides ways of converting DFM data into and out of the binary format used in resources. Load the resource data, convert it to text, make your modifications, convert it back to binary, and replace the old resource with your new one.

A slight problem is that the OS won't allow modifications to a program that's still running, even if the modifications are initiated by the program itself. That can be worked around, though.

So, although the answer to the question you asked is yes, in the end you're going to find it much easier to simply store your combo-box items just about anywhere else (e.g., an external file, the registry, a database, even the cloud), and then load and add them yourself after you create the form but before you display it to the user. The user will never know the difference.

To run code after the form is loaded but before it's shown, override the form's Loaded method.

Community
  • 1
  • 1
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • I see so. I'm only still in high school and am not that experienced in programming so I was just wondering if there was a way however I see now the only way is to save the items elsewhere and add them again at the start of the program – RaymondSWalters Sep 28 '15 at 13:20
0

You can load your items from a textfile that you read at program initialization

  • I was afraid that you would say that... I was just hoping there was an easier way to do it... Isn't there ANY other way? – RaymondSWalters Sep 27 '15 at 14:45
  • 2
    You are hoping for magic. You want the computer to read your mind. That does not happen. Hence the need for programmers. – David Heffernan Sep 27 '15 at 14:46
  • @RaymondSWalters I think you can also try to hack the Form's DFM inside the PE. I don't remember if Windows will allow you to patch a running executable, so you might need a separate application. So, after all, I guess it is possible to do it without persisting to external file... but why would you? – GabrielF Sep 28 '15 at 12:56