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
karen021897Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
karen021897Author Commented:
Inthe, thanks

kifah : do you have any code?

0
karen021897Author Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.