[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

Store Combobox.Items Into Windows Registry


I want to store all items in combobox into registry.

read from the registry when the app loads and write to registry all the items into registry when the app close

thanx
0
karen021897
Asked:
karen021897
1 Solution
 
kifahCommented:
It's much beter to use a file to save the items.
by using ComboBox.Items.SaveToFile(FileName);

Sure you can use registry for that, but is not recomended (in my openion).

Tell me if you still need to use the registry, so I can give a simple code, which will make your job.
0
 
almillerCommented:
I'm afraid I would have to disagree with kifah. It _is_ recommened to use the Registry instead of local files, that is what the Registry is all about. It's here to replace the use of the old .ini files. The fact that it is getting too big and disturbs the work-flow of Windows is another matter.

The .SaveToxxxx methods are Streams. You can find many freeware components adding stream capabilities including the Registry, thus allowing SaveToRegistry for components. Check out one of the many Delphi components pages.
0
 
intheCommented:
Hi
this should do it:
Regards Barry


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,Registry;

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  R : TRegistry;

 implementation

{$R *.DFM}

Function GetKeyCount : Integer;
begin
Result := 50;
end;

procedure TForm1.FormCreate(Sender: TObject);
var i : integer;
begin
Combobox1.Clear;
R :=TRegistry.Create;
 with R do  try
  Begin
 Rootkey:=HKEY_CURRENT_USER;
  CreateKey('\Software\MySoftware');
   OpenKey('\Software\MySoftware', True);
    for i := 0 to GetKeyCount do begin
  Combobox1.Items[i] := R.ReadString('Combobox Item'+inttostr(i));
    end;
   R.CloseKey;
  end;
   finally
  R.Free;
   end;
   for i := 0 to combobox1.items.count - 1 do
   begin
   if combobox1.items.Strings[i] = '' then
   combobox1.items.Delete(i);
   end;
  Combobox1.Text := Combobox1.Items[0];
end;

procedure TForm1.FormDestroy(Sender: TObject);
var i : integer;
begin
R :=TRegistry.Create;
 with R do  try
  Begin
 Rootkey:=HKEY_CURRENT_USER;
  CreateKey('\Software\MySoftware');
   OpenKey('\Software\MySoftware', True);
    for i := 0 to combobox1.Items.Count -1 do
     begin
     if combobox1.items.Strings[i] <> '' then
  R. WriteString('Combobox Item'+inttostr(i),Combobox1.items[i]);
       end;
   R.CloseKey;
  end;
   finally
  R.Free;
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Combobox1.Items.Loadfromfile('c:\autoexec.bat');
end;

end.
0
 
karen021897Author Commented:
Inthe, thanks

kifah : do you have any code?

0
 
karen021897Author Commented:
Thanks
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now