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;
ImageList1: TImageList;
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Edit2: TEdit;
ComboBox1: TComboBox;
Label3: TLabel;
Label4: TLabel;
ListBox1: TListBox;
Memo1: TMemo;
Label5: TLabel;
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;//收入
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;
//10条人员记录,分别存储在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; //设置TForm_Common窗体中的初始人员资料的位置
for i:=0 to 9 do //初始化人员资料数组
begin
Person[i].Name := Inttostr(i);
end;
//设置动作First和Prev的初始状态
First.Enabled :=False;
Prev.Enabled :=False;
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].Remark:=Memo1.Text ;
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;
end;
end.