Solved

error msg when reading data from registry

Posted on 2004-09-10
5
241 Views
Last Modified: 2010-04-05
when i reading the data from the registry i got the following error: "EConvertError".

this code writes the data to registry:
procedure TForm1.FormDestroy(Sender: TObject);
var clbx: integer;
    buf: string;
begin
 with TRegistry.Create do begin
  RootKey := HKEY_CURRENT_USER;
  try
  if OpenKey('\Software\Test\', True) then begin
   for clbx := 0 to CheckListBox1.Items.Count - 1 do begin
    if CheckListBox1.Checked[clbx] = True then buf := buf + IntToStr(clbx) + ' ';
   end;
   WriteString('items', buf);
  end;
  finally
  CloseKey;
  Free;
  end;
 end;
end;

this code reads the data:
procedure TForm1.FormCreate(Sender: TObject);
var buf: string;
    intbuf: integer;
begin
 with TRegistry.Create do begin
  RootKey := HKEY_CURRENT_USER;
  try
  if OpenKey('\Software\Test\', false) then begin
    buf := ReadString('items');
    for intbuf := 0 to Length(buf) - 1 do begin
     if buf[intbuf] <> ' ' then CheckListBox1.Checked[StrToInt(buf[intbuf])] := True;
    end;
  end;
  finally
  CloseKey;
  Free;
  end;
 end;
end;

something is wrong with this code: "if buf[intbuf] <> ' ' then CheckListBox1.Checked[StrToInt(buf[intbuf])] := True;" ?
i use delphi 4.
0
Comment
Question by:din345
5 Comments
 
LVL 4

Accepted Solution

by:
LMuadDIb earned 30 total points
ID: 12029982
I usually use "0" or "1" to designate checked or not in checklists.
Here is the code.....

uses
   StrUtils;


procedure TForm1.FormDestroy(Sender: TObject);
var clbx: integer;
    buf: string;
begin
 with TRegistry.Create do begin
  RootKey := HKEY_CURRENT_USER;
  try
  if OpenKey('\Software\Test\', True) then begin
   for clbx := 0 to CheckListBox1.Items.Count - 1 do begin
    if CheckListBox1.Checked[clbx] = True then
      buf := buf + '1'
    else
      buf := buf + '0';
   end;
   WriteString('items', buf);
  end;
  finally
  CloseKey;
  Free;
  end;
 end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var buf: string;
    intbuf: integer;
begin
 with TRegistry.Create do begin
  RootKey := HKEY_CURRENT_USER;
  try
  if OpenKey('\Software\Test\', false) then begin
    buf := ReadString('items');
    for intbuf := 0 to Length(buf) - 1 do begin
     if midstr(buf,intbuf+1,1) <> '1' then
      CheckListBox1.Checked[intbuf] := False
     else
      CheckListBox1.Checked[intbuf] := True;
    end;
  end;
  finally
  CloseKey;
  Free;
  end;
 end;
end;


hope this helps
Frank
0
 

Assisted Solution

by:meadorj
meadorj earned 20 total points
ID: 12030133
You can use the strtointdef function and set the default number to -1.  That should solve the convert error.  What string value does the registry return when you try to read it?
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 12031308
try:

if buf[intbuf] <> ' ' then CheckListBox1.Checked[StrToIntDef(buf[intbuf], 0)] := True;
0
 
LVL 6

Assisted Solution

by:vadim_ti
vadim_ti earned 30 total points
ID: 12033312
buf is string, in your loop you need to use
    for intbuf := 1 to Length(buf)  do begin


    buf := ReadString('items');
    for intbuf := 0 to Length(buf) - 1 do begin
     if buf[intbuf] <> ' ' then CheckListBox1.Checked[StrToInt(buf[intbuf])] := True;
    end;


0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 12035264
vadim is right.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now