开发者

how to read registry key for certain condition only?

i have create 5 dynamic buttons in 1 row and store the button info (caption, isActive, etc) in registry. It shows the button in form when the IsActive = TRUE from registry. The result is as below (each button indicate by a * symbol):

*****

When i set the IsActive = FALSE for button2 and button4, the button being remove and left the space as below:

* ** *

Any idea to solve this problem? The source code is as below:

    procedure TfrmPOS.CreateDynamicBtn;    
    var
      Reg : TRegIniFile;
      lstKey   : TStringList;
      sKeyName : String;
      bActive  : boolean;
      btn1     : TBitBtn;
      i, k, iIcon : integer;
    begin
      lstKey    := TStringList.Create;
      Reg       := TRegIniFile.Create;
      try
        //clear bitbtn
        if ScrollBox2.ControlCount >开发者_运维百科; 0 then begin
           for k := ScrollBox2.ControlCount - 1 downto 0 do begin
              with ScrollBox2.Controls[k] AS TBitBtn do begin
                Free;
              end;
           end;
        end;

sKeyName := Sysmem.RegKeyRoot+'\POSConfig\ItemSetting\';
Reg      := TRegIniFile.Create(sKeyName);
Reg.GetKeyNames(lstKey);   //button1, button2,...
for i := 0 to lstKey.Count - 1 do begin
  Reg.OpenKeyReadOnly(sKeyName);
  bActive := Reg.ReadBool(lstKey.Strings[i], 'IsActive', TRUE);
  if bActive = TRUE then begin
     //create dynamic bitbtn
     btn1 := TBitBtn.Create(self);
     btn1.Parent  := ScrollBox2;
     btn1.Height  := 82;
     btn1.Width   := 82;
     btn1.Left    := ((i mod 5) * btn1.Width);
     btn1.Top     := (Trunc((i div 5)) * btn1.Height);
     btn1.Caption := Reg.ReadString(lstKey.Strings[i], 'Caption', '');

     iIcon := Reg.ReadInteger(lstKey.Strings[i], 'IconImage', 0);
     imglstIcon.GetBitmap(iIcon, btn1.Glyph);
     btn1.Layout  := blGlyphTop;
     btn1.Name    := lstKey.Strings[i]; 
     btn1.OnClick := OnButtonClick;
  end;
  Reg.CloseKey;
end;

finally
    lstKey.Free;
    Reg.Free;
  end;
end;


I suspect you wonder why the space for the second button is still there, instead of the third button filling that area.

It's because you're setting the Left properties for the buttons as though all the buttons were there:

btn1.Left := ((i mod 5) * btn1.Width);

When i = 1, you skip over it because that button is invisible. But when i = 3, you calculate its position the same as you would have if button 2 had been visible. Keep a visible-button counter separate from your list iterator, and use it to position your buttons:

BtnIndex := 0;
Reg.OpenKeyReadOnly(sKeyName);
for i := 0 to lstKey.Count - 1 do begin
  bActive := Reg.ReadBool(lstKey.Strings[i], 'IsActive', TRUE);
  if bActive then begin
    //create dynamic bitbtn
    btn1 := TBitBtn.Create(self);
    btn1.Parent := ScrollBox2;
    btn1.SetBounds(BtnIndex mod 5 * 82, BtnIndex div 5 * 82, 82, 82);
    Inc(BtnIndex);

There are better ways to do what you want. If you have a sufficiently recent version of Delphi, use a TFlowPanel or TGridPanel. They will arrange your buttons next to each other for you automatically. If your Delphi version didn't come with that control, then try a TToolBar instead and fill it with TToolButton controls.

Your question actually had nothing to do with the registry, but you can make better use of the registry anyway. There's no need to keep re-opening the same key every time. The value of sKeyName doesn't change inside the loop, so open the key once before you enter the loop (as shown above) and then just leave it open for as long as you need it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜