程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi中用一個Byte數表示8個復選框的選擇狀態

Delphi中用一個Byte數表示8個復選框的選擇狀態

編輯:Delphi

本例效果圖:

代碼文件:

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved