其他分享
首页 > 其他分享> > 用一个 Byte 数表示 8 个复选框的选择状态

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

作者:互联网

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


本例效果图:

o_0941601.gif

代码文件:
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.

窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 127
  ClientWidth = 231
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object CheckListBox1: TCheckListBox
    Left = 8
    Top = 8
    Width = 105
    Height = 113
    ItemHeight = 13
    TabOrder = 0
    OnClick = CheckListBox1Click
  end
  object Button1: TButton
    Left = 128
    Top = 72
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Edit1: TEdit
    Left = 119
    Top = 32
    Width = 98
    Height = 21
    TabOrder = 2
    Text = 'Edit1'
  end
end

posted on 2009-04-16 17:38  万一  阅读(4560)  评论(20)  编辑  收藏

标签:状态,end,Sender,Text,复选框,Edit1,num,CheckListBox1,Byte
来源: https://blog.51cto.com/u_14617575/2747981