unit UCommon;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Buttons, ToolWin, ComCtrls, ImgList,
ActnList, Menus, jpeg;
type
TForm_Common = class(TForm)
Button1: TButton;
CoolBar1: TCoolBar;
ToolBar1: TToolBar;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
SpeedButton4: TSpeedButton;
ActionList1: TActionList;
First: TAction;
prev: TAction;
next: TAction;
Last: TAction;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
RadioGroup1: TRadioGroup;
GroupBox1: TGroupBox;
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
CheckBox3: TCheckBox;
CheckBox4: TCheckBox;
CheckBox5: TCheckBox;
CheckBox6: TCheckBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Edit1: TEdit;
Edit2: TEdit;
ListBox1: TListBox;
ComboBox1: TComboBox;
Memo1: TMemo;
ImageList1: TImageList;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FirstExecute(Sender: TObject);
procedure prevExecute(Sender: TObject);
procedure nextExecute(Sender: TObject);
function SaveContext():Boolean;
Procedure LoadContext();
procedure LastExecute(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TSRecord=Record //存储人员的基本资料的结构
Name:String; //姓名
Age:Integer; //年龄
Career:Integer;//职业
Income:Integer;//收入
Sex:Integer; //性别
Hobby1:Boolean;//爱好1
Hobby2:Boolean;//爱好2
Hobby3:Boolean;//爱好3
Hobby4:Boolean;//爱好4
Hobby5:Boolean;//爱好5
Hobby6:Boolean;//爱好6
Remark:String; //备注
end;
var
Form_Common: TForm_Common;
//十条人员记录,分别存储在Person[0]..Person[9]中
Person:array [0..9] of TSRecord;
//表示当前人员记录编号
CurRecord : Integer;
implementation
{$R *.dfm}
procedure TForm_Common.FormCreate(Sender: TObject);
var
i:integer;
begin
//编辑框中的文本
Edit1.Text :='0';
Edit2.Text :='0';
//设置组合框的风格为下拉列表,不允许用户编辑文本
Combobox1.Style := csDropDownList;
//设置组合框的初始项
Combobox1.ItemIndex :=0;
//设置列表框的初始选项
ListBox1.ItemIndex :=0;
//设置多行编辑组件的滚动条
Memo1.ScrollBars :=ssVertical ;
Memo1.Text :='';
CurRecord :=0;
for i:=0 to 9 do
begin
Person[i].Name := Inttostr(i);
end;
//设置动作First和Prev的初始状态
First.Enabled :=False;
Prev.Enabled :=False;
//设置TRadioGroup组件及其中的单选按钮的属性
RadioGroup1.Columns := 2; //分两列显示单选按钮
RadioGroup1.Items.Add('男'); //向TRadioGroup组件中添加单选按钮
RadioGroup1.Items.Add('女');
RadioGroup1.ItemIndex := 0; //设置初始的被选按钮
end;
procedure TForm_Common.Button1Click(Sender: TObject);
begin //结束程序的运行
Application.Terminate ;
end;
procedure TForm_Common.FirstExecute(Sender: TObject);
begin //漫游到第一个用户的资料
if (SaveContext()=false) then
exit; //如果某些资料的录入有问题,则退出
CurRecord :=0; //设置当前人员记录编号的新值
LoadContext(); //在窗体上显示新用户的资料
//修改各个动作的可用性
First.Enabled :=False;
Prev.Enabled :=False;
Next.Enabled :=True;
Last.Enabled :=True;
end;
procedure TForm_Common.prevExecute(Sender: TObject);
begin //漫游到当前用户的后一个用户的资料
if (SaveContext()=false) then
exit;
CurRecord :=CurRecord-1;
LoadContext();
if (CurRecord=0) then
begin
First.Enabled :=False;
Prev.Enabled :=False;
end ;
if (CurRecord<>9) then
begin
Next.Enabled :=True;
Last.Enabled :=True;
end ;
end;
procedure TForm_Common.nextExecute(Sender: TObject);
begin //漫游到当前用户的前一个用户的资料
if (SaveContext()=false) then
exit;
CurRecord :=CurRecord+1;
LoadContext();
if (CurRecord=9) then
begin
Next.Enabled :=False;
Last.Enabled :=False;
end ;
if (CurRecord<>0) then
begin
First.Enabled :=True;
Prev.Enabled :=True;
end;
end;
procedure TForm_Common.LastExecute(Sender: TObject);
begin //漫游到最后一个用户的资料
if (SaveContext()=false) then
exit;
CurRecord :=9;
LoadContext();
Next.Enabled :=False;
Last.Enabled :=False;
First.Enabled :=True;
Prev.Enabled :=True;
end;
function TForm_Common.SaveContext():boolean;
var //保存人员的资料
Code: Integer;
begin
Person[CurRecord].Name :=Edit1.Text;
Val(Edit2.Text, Person[CurRecord].Age ,code);
if (code<>0) then //监测人员的年龄是否为整数
begin
ShowMessage('人员的年龄必须为整数,请重新输入!');
result:=false;
exit;
end;
//保存人员的其它资料
Person[CurRecord].Career :=Combobox1.ItemIndex;
Person[CurRecord].Income:=ListBox1.ItemIndex ;
Person[CurRecord].Sex:=RadioGroup1.ItemIndex;
Person[CurRecord].Remark:=Memo1.Text ;
//新添加代码
Person[CurRecord].Hobby1:=Checkbox1.Checked;
Person[CurRecord].Hobby2:=Checkbox2.Checked;
Person[CurRecord].Hobby3:=Checkbox3.Checked;
Person[CurRecord].Hobby4:=Checkbox4.Checked;
Person[CurRecord].Hobby5:=Checkbox5.Checked;
Person[CurRecord].Hobby6:=Checkbox6.Checked;
result :=true;
end;
Procedure TForm_Common.LoadContext();
begin //将保存在Person数组中的人员资料在窗体上显示出来
Edit1.Text := Person[CurRecord].Name;
Edit2.Text := IntToStr(Person[CurRecord].Age);
Combobox1.ItemIndex := Person[CurRecord].Career;
ListBox1.ItemIndex := Person[CurRecord].Income;
Memo1.Text :=Person[CurRecord].Remark;
//新添加代码
RadioGroup1.ItemIndex :=Person[CurRecord].Sex;
Checkbox1.Checked := Person[CurRecord].Hobby1;
Checkbox2.Checked := Person[CurRecord].Hobby2;
Checkbox3.Checked := Person[CurRecord].Hobby3;
Checkbox4.Checked := Person[CurRecord].Hobby4;
Checkbox5.Checked := Person[CurRecord].Hobby5;
Checkbox6.Checked := Person[CurRecord].Hobby6;
end;
end.