用一个 Byte 数表示 8 个复选框的选择状态

unit Unit1;


interface

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

type
  TForm1 
= class(TForm)
    CheckListBox1: TCheckListBox;
    Button1: TButton;
    Edit1: TEdit;
    
procedure FormCreate(Sender: TObject);
    
procedure Button1Click(Sender: TObject);
    
procedure CheckListBox1Click(Sender: TObject);
  
end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  num: Integer;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit1.Text :
= '输入一个 Byte 数';
  CheckListBox1.Align :
= alLeft;
  CheckListBox1.Items.CommaText :
= 'A,B,C,D,E,F,G,H';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  num :
= Lo(StrToIntDef(Edit1.Text, 0));
  Edit1.Text :
= IntToStr(num);
  
for i := 0 to 7 do CheckListBox1.Checked[i] := 1 shl i or num = num;
  Text :
= IntToStr(num);
end;

procedure TForm1.CheckListBox1Click(Sender: TObject);
var
  i: Integer;
begin
  num :
= 0;
  
for i := 0 to 7 do if CheckListBox1.Checked[i] then num := num or 1 shl i;
  Text :
= IntToStr(num);
  Edit1.Text :
= Text;
end;

end.
原文地址:https://www.cnblogs.com/jxgxy/p/2087019.html