Board index » delphi » TCheckListBox component, Checked in runtime

TCheckListBox component, Checked in runtime

Hi All,

I wondering if anybody knows how to show an item for a TCheckListBox
component as being checked on runtime. I need to show an item checked
when the application starts but I have not been able to find out how to
do this. I am using delphi 4.03 Client Server.

Thanks in advance.

Nick

 

Re:TCheckListBox component, Checked in runtime


On Sat, 04 Mar 2000 12:11:19 -0400, Nick Tentomas

Quote
<nick.tento...@dal.ca> wrote:
> I wondering if anybody knows how to show an item for a
> TCheckListBox component as being checked on runtime.

There are two parts to the puzzle:

1) How to check an item in a TCheckListBox component at run-time, and

2) Where to store your "check" state information on the disk.

Here's how to do (1):

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  I := 3;
  CheckListBox1.Checked[I] := True;
end;

Where you store your check state information (2) depends entirely on
your application. You could store it in the registry, an ini file, a
database, a custom text file, or an XML file.

Here's a small example which uses an .ini file (the example assumes
you can only have one item checked at a time):

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    CheckListBox1: TCheckListBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  public
    function FindCheckedIndex: Integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses
  IniFiles;

resourcestring
  sIniFile = 'project1.ini';
  sSettings = 'Settings';
  sIndex = 'Index';

procedure TForm1.FormCreate(Sender: TObject);
var
  Path: string;
  IniFile: TIniFile;
  Index: Integer;
begin
  Path := ExtractFilePath(Application.ExeName);
  IniFile := TIniFile.Create(Path + sIniFile);
  try
    Index := IniFile.ReadInteger(sSettings, sIndex, -1);
    if Index <> -1 then CheckListBox1.Checked[Index] := True;
  finally
    IniFile.Free;
  end;
end;

function TForm1.FindCheckedIndex: Integer;
var
  I: Integer;
begin
  for I := 0 to CheckListBox1.Items.Count - 1 do
    if CheckListBox1.Checked[I] then
    begin
      Result := I;
      Exit;
    end;
  Result := -1;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  Path: string;
  IniFile: TIniFile;
  Index: Integer;
begin
  Path := ExtractFilePath(Application.ExeName);
  IniFile := TIniFile.Create(Path + sIniFile);
  try
    Index := FindCheckedIndex;
    IniFile.WriteInteger(sSettings, sIndex, Index);
  finally
    IniFile.Free;
  end;
end;

end.
--
Rick Rogers (TeamB)
www.fenestra.com and www.componentfactory.com

Re:TCheckListBox component, Checked in runtime


If you place a component CheckListBox1:TCheckListBox on the form
with, say, 3 items, then you could type the following:

procedure TForm1.FormCreate(Sender: TObject);
begin
  CheckListBox1.Checked[0] := True;
  CheckListBox1.Checked[1] := False;
  CheckListBox1.Checked[2] := True;
end;

and when you run the application, the first and third items are checked,
but the second item is not.

----------------------------------------------------------------------------
-----------------------

Nick Tentomas <nick.tento...@dal.ca> schreef in artikel
<38C135A7.40D09...@dal.ca>...

Quote
> Hi All,

> I wondering if anybody knows how to show an item for a TCheckListBox
> component as being checked on runtime. I need to show an item checked
> when the application starts but I have not been able to find out how to
> do this. I am using delphi 4.03 Client Server.

> Thanks in advance.

> Nick

Re:TCheckListBox component, Checked in runtime


To show an item for a TCheckListBox component as being checked on runtime,
use the Checked property of the TCheckListBox component.

For each member of the Items array, checked is true if a check mark appears
in the item's check box.

So to check at runtime, say the first item, do like this:

MyCheckListBox.Checked[0] := True;

Lina Scarpellini
==============
Genial Software Inc.  www.genial.qc.ca

Other Threads