📄 unit4.~pas
字号:
unit Unit4;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Mask, StdCtrls, Buttons;
type
Tfmlxraddormodify = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Edit2: TEdit;
Edit6: TEdit;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
ComboBox2: TComboBox;
Edit1: TEdit;
Edit3: TEdit;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Edit4: TEdit;
procedure Edit1Exit(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure Edit3KeyPress(Sender: TObject; var Key: Char);
procedure Edit3Exit(Sender: TObject);
procedure Edit4KeyPress(Sender: TObject; var Key: Char);
procedure Edit4Exit(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
fmlxraddormodify: Tfmlxraddormodify;
implementation
uses unit1,unit2,unit3;
{$R *.dfm}
procedure insert;
begin
if fmlxr.ADOTable1.Locate('contactid',fmlxraddormodify.edit3.Text,[]) then //联系人ID不能已经存在
begin
showmessage('此联系人ID已经存在!');
exit;
end;
if not fmlxrz.ADOTable1.Locate('groupid',fmlxraddormodify.edit4.text,[]) then //联系人组ID必须已经存在
begin
showmessage('此联系人组ID不存在!');
exit;
end;
fmlxr.ADOTable1.Append;
fmlxr.ADOTable1.FieldByName('contactid').AsString:=fmlxraddormodify.edit3.Text;
fmlxr.ADOTable1.FieldByName('contactname').AsString:=fmlxraddormodify.edit2.Text;
fmlxr.ADOTable1.FieldByName('groupid').AsString:=fmlxraddormodify.edit4.Text;
if fmlxraddormodify.edit1.Text<>'' then
fmlxr.ADOTable1.FieldByName('birthday').AsString:=fmlxraddormodify.edit1.Text;
if fmlxraddormodify.combobox2.Text<>'' then
fmlxr.ADOTable1.FieldByName('sex').AsString:=fmlxraddormodify.combobox2.Text;
if fmlxraddormodify.edit6.Text<>'' then
fmlxr.ADOTable1.FieldByName('homeaddress').AsString:=fmlxraddormodify.edit6.Text;
fmlxr.ADOTable1.Post;
fmlxr.ADOTable1.Refresh;
//新建联系人时对联系人表的shouldcontactdate字段进行填充
fmlxrz.ADOTable1.Locate('groupid',fmlxraddormodify.Edit4.Text,[]);
fmlxr.ADOTable1.Locate('contactid',fmlxraddormodify.Edit3.Text,[]);
fmlxr.ADOTable1.Edit;
fmlxr.ADOTable1.FieldByName('shouldcontactdate').AsDateTime:=date+fmlxrz.ADOTable1.fieldbyname('interval').AsInteger;
fmlxr.ADOTable1.Post;
fmlxr.ADOTable1.Refresh;
showmessage('插入成功!');
fmlxraddormodify.Close;
end;
procedure modify;
var
bm:string;
begin
bm:=fmlxr.ADOTable1.Bookmark;
if (fmlxr.ADOTable1.Locate('contactid',fmlxraddormodify.edit3.Text,[])) and (fmlxr.ADOTable1.Bookmark<>bm) then //联系人ID不能已经存在 ID可以不修改
begin
showmessage('此联系人ID已经存在!');
fmlxr.ADOTable1.Bookmark:=bm;
exit;
end;
if not fmlxrz.ADOTable1.Locate('groupid',fmlxraddormodify.edit4.text,[]) then //联系人组ID必须已经存在
begin
showmessage('此联系人组ID不存在!');
exit;
end;
fmlxr.ADOTable1.edit;
fmlxr.ADOTable1.FieldByName('contactid').AsString:=fmlxraddormodify.edit3.Text;
fmlxr.ADOTable1.FieldByName('contactname').AsString:=fmlxraddormodify.edit2.Text;
fmlxr.ADOTable1.FieldByName('groupid').AsString:=fmlxraddormodify.edit4.Text;
//if fmlxraddormodify.edit1.Text<>'' then
fmlxr.ADOTable1.FieldByName('birthday').AsString:=fmlxraddormodify.edit1.Text;
//if fmlxraddormodify.combobox2.Text<>'' then
fmlxr.ADOTable1.FieldByName('sex').AsString:=fmlxraddormodify.combobox2.Text;
//if fmlxraddormodify.edit6.Text<>'' then
fmlxr.ADOTable1.FieldByName('homeaddress').AsString:=fmlxraddormodify.edit6.Text;
fmlxr.ADOTable1.Post;
fmlxr.ADOTable1.Refresh;
showmessage('修改成功!');
fmlxraddormodify.Close;
end;
procedure Tfmlxraddormodify.Edit1Exit(Sender: TObject);
var
str,stryear,strmonth,strday:string;
isdate:boolean;
begin
if edit1.Text='' then //文本框为空可以退出
exit;
isdate:=true;
str:=fmlxraddormodify.edit1.Text;
if (length(str)<>10) or (copy(str,1,1)='-') or (copy(str,2,1)='-') or (copy(str,3,1)='-') or (copy(str,4,1)='-') or (copy(str,5,1)<>'-') or (copy(str,6,1)='-') or (copy(str,7,1)='-') or (copy(str,8,1)<>'-') or (copy(str,9,1)='-') or (copy(str,10,1)='-') then
begin
showmessage('格式出错!');
edit1.SetFocus;
exit;
end; //此行代码以后确保日期格式类似2000-01-01
strmonth:=copy(str,6,2); //测试月份是否合法
if (strtoint(strmonth)>12) or (strtoint(strmonth)<1) then
begin
isdate:=false;
end;
stryear:=copy(str,1,4);
strday:=copy(str,9,2); //测试日是否合法
case strtoint(strmonth) of
1,3,5,7,8,10,12:
if (strtoint(strday)>31) or (strtoint(strday)<1) then
isdate:=false;
4,6,9,11:
if (strtoint(strday)>30) or (strtoint(strday)<1) then
isdate:=false;
2:
if (strtoint(stryear) mod 400=0) or ((strtoint(stryear) mod 4=0) and (strtoint(stryear) mod 100<>0))then
if (strtoint(strday)>29) or (strtoint(strday)<1) then
isdate:=false;
else
if (strtoint(strday)>28) or (strtoint(strday)<1) then
isdate:=false;
end;
if isdate then //是否大于今日
begin
if encodedate(strtoint(stryear),strtoint(strmonth),strtoint(strday))>date then
isdate:=false;
end;
if not isdate then
begin
showmessage('日期不合法,正确格式为月日年!');
edit1.SetFocus;
end;
end;
procedure Tfmlxraddormodify.Edit1KeyPress(Sender: TObject; var Key: Char); //至允许输入数字,短杠及退格键
begin
if not ( ((ord(key)>=48) and (ord(key)<=57)) or (ord(key)=45) or (ord(key)=8)) then
begin
key:=#16;
exit;
end;
end;
procedure Tfmlxraddormodify.Edit3KeyPress(Sender: TObject; var Key: Char); //至允许输入数字及退格键
begin
if not ( ((ord(key)>=48) and (ord(key)<=57)) or (ord(key)=8)) then
begin
key:=#16;
exit;
end;
end;
procedure Tfmlxraddormodify.Edit3Exit(Sender: TObject);
begin
if edit3.text='' then //文本框为空可以退出
exit;
if length(edit3.Text)<>6 then
begin
showmessage('联系人ID为六位数字!');
edit3.SetFocus;
exit;
end;
end;
procedure Tfmlxraddormodify.Edit4KeyPress(Sender: TObject; var Key: Char);
begin
if not ( ((ord(key)>=48) and (ord(key)<=57)) or (ord(key)=8)) then
begin
key:=#16;
exit;
end;
end;
procedure Tfmlxraddormodify.Edit4Exit(Sender: TObject);
begin
if edit4.Text='' then //文本框为空可以退出
exit;
if length(edit4.Text)<>2 then
begin
showmessage('联系人组ID为两位数字!');
edit4.SetFocus;
exit;
end;
end;
procedure Tfmlxraddormodify.BitBtn1Click(Sender: TObject);
begin
if edit3.Text=''then //联系人ID不能为空
begin
showmessage('联系人ID不能为空!');
exit;
end;
if edit2.Text=''then //联系人姓名不能为空
begin
showmessage('联系人姓名不能为空!');
exit;
end;
if edit4.Text=''then //联系人组ID不能为空
begin
showmessage('联系人组ID不能为空!');
exit;
end;
if isadd then
begin
insert;
end
else
begin
modify;
end;
end;
procedure Tfmlxraddormodify.BitBtn2Click(Sender: TObject);
begin
fmlxraddormodify.close;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -