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

  • 2
  • 906 views
  • A+
所属分类:[开发技巧]

操作界面效果:

代码如下:

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);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
var
num: Integer;


procedure TForm1.FormCreate(Sender: TObject);
var
i : Integer ;
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.

代码文件下载:200904170029140353.rar

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
广告也精彩
avatar
广告也精彩

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:2   其中:访客  2   博主  0

    • avatar godyad 5

      呵呵~在网络上看到的,就写下

      • avatar delphi 4

        偶系delphi,
        星,这个是祢写的?
        好好玩哦..