Solved

Writing to the Registery

Posted on 2004-09-23
7
242 Views
Last Modified: 2010-04-05
Hey,

Is there a way so when I enter some random word into a form and then click on a button it will store it in the registrey and then say the next day i enter the same word into another form and click ok it will find the word in the registery and then say found and if it didnt it would say not found.

Is there a way?

Cheers,

Computer Programer
0
Comment
Question by:computerprogramer
  • 3
  • 2
  • 2
7 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12140646
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12140649
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12140662
Well, my example:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Function CheckRandomWord(RandomWord: STring; var Buffer: String): Boolean;
var
reg: TRegistry;
begin
   Result := False;
   Buffer := Format('Word %s was not found',[RandomWord]);
   Reg := TRegistry.Create;
   try
      with Reg do
         begin
            RootKEy := HKEY_LOCAL_MACHINE;
            If KeyExists('\Software\MyApplication\RandomWord') then
            begin
               OpenKey('\Software\MyApplication\RandomWord',False);
               result := ReadString('Value') = RandomWord;
               If Result then
                  Buffer := Format('Word %s was found',[RandomWord]);

               CLoseKEy;
            end else begin
               OpenKey('\Software\MyApplication\RandomWord',True);
               WriteString('Value',RandomWord);
               Buffer := Format('Word %s was stored',[RandomWord]);
               CLoseKEy;
            end;
         end;
   finally
      reg.Free;
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Msg: STring;
begin
   CheckRandomWord(Edit1.Text,Msg);
   ShowMessage(Msg);
end;
end.

F68 ;-)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:computerprogramer
ID: 12141162
Hi,

Thanks for the replys. F68 is there a way that you could put something in the code so if a different word was entred and there already is a word stored there is would delete the older word and enter the newer word?
0
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 50 total points
ID: 12141260
mmm...

What about this?

Function CheckRandomWord(RandomWord: STring;Restore: Boolean; var Buffer: String): Integer; //0 = Found, 1 = STored, 2 = Not Found;
var
reg: TRegistry;
begin
   Result := 2;
   Buffer := Format('Word %s was not found',[RandomWord]);
   Reg := TRegistry.Create;
   try
      with Reg do
         begin
            RootKEy := HKEY_LOCAL_MACHINE;
            If restore then DeleteKey('\Software\MyApplication\RandomWord');
            If KeyExists('\Software\MyApplication\RandomWord') then
            begin
               OpenKey('\Software\MyApplication\RandomWord',False);
               If ReadString('Value') = RandomWord then
                  begin
                     Result := 0;
                     Buffer := Format('Word %s was found',[RandomWord]);
                  end;
               CLoseKEy;
            end else begin
               Result := 1;
               OpenKey('\Software\MyApplication\RandomWord',True);
               WriteString('Value',RandomWord);
               Buffer := Format('Word %s was stored',[RandomWord]);
               CLoseKEy;
            end;
         end;
   finally
      reg.Free;
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Msg: STring;
begin
   If CheckRandomWord(Edit1.Text,False,Msg) = 2 then
      If Messagedlg(Msg+'. Do you want to replace it?',mtConfirmation,[mbYes,mbNo],0) = idYes then
         CheckRandomWord(Edit1.text,True,Msg);
   ShowMessage(Msg);
end;
0
 

Author Comment

by:computerprogramer
ID: 12141317
Thanks Mate!
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12141321
Glad to have helped you :)

F68 ;-)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi : could not find program, '...exe' 2 212
LAN or WAN ? 11 105
Firemonkey android show image from resource ? 1 55
Delphi and Access based Enumeration 9 77
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question