给你贴一段我写的代码作为参考:
unit UnitCompensateEdit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, cxLookAndFeelPainters, cxMaskEdit, cxDropDownEdit, cxCalendar,
StdCtrls, cxButtons, cxControls, cxContainer, cxEdit, cxTextEdit,UnitMain,
DB, ADODB,DateUtils, cxCheckBox
;
type
TfrmCompensateEdit = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
edtComputerNo: TcxTextEdit;
edtSignNo: TcxTextEdit;
edtDamanaged: TcxTextEdit;
edtCaution: TcxTextEdit;
Label7: TLabel;
edtMoney: TcxTextEdit;
btnOK: TcxButton;
btnCancel: TcxButton;
cxdDamanaged: TcxDateEdit;
adqT_SUN_HUAI_PEI_CHANG: TADOQuery;
cxcChangComputer: TcxCheckBox;
edtNewComputerID: TcxTextEdit;
Label8: TLabel;
edtTreatment: TcxComboBox;
cxcNeedRepair: TcxCheckBox;
procedure btnOKClick(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
procedure cxcChangComputerClick(Sender: TObject);
procedure edtComputerNoPropertiesChange(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
//CompensateID为-1表示增加新记录大于零表示显示记录
constructor Create(AOwner: TComponent;CompensateID:integer);reintroduce; overload;
end;
var
frmCompensateEdit: TfrmCompensateEdit;
implementation
{$R *.dfm}
constructor TfrmCompensateEdit.Create(AOwner: TComponent;CompensateID:integer);
begin
inherited create(AOwner);
if CompensateID=-1 then
begin
self.edtComputerNo.Clear;
self.edtSignNo.Clear;
self.edtDamanaged.Clear;
self.edtCaution.Clear;
self.edtMoney.Text:='0';
self.cxdDamanaged.Clear;
self.edtTreatment.Clear;
end
else
begin
self.btnCancel.Caption:='确定';
SELF.btnOK.Visible:=FALSE;
self.adqT_SUN_HUAI_PEI_CHANG.SQL.Text:='select * from T_SUN_HUAI_PEI_CHANG where ID='+inttostr(CompensateID);
try
self.adqT_SUN_HUAI_PEI_CHANG.Open;
if self.adqT_SUN_HUAI_PEI_CHANG.Eof then
raise exception.Create('编号不存在!');
self.edtComputerNo.Text:=self.adqT_SUN_HUAI_PEI_CHANG.fieldbyname('DIAN_NAO_ID').AsString;
self.edtSignNo.Text:=self.adqT_SUN_HUAI_PEI_CHANG.fieldbyname('XIE_YI_ID').AsString;
self.edtDamanaged.Text:= self.adqT_SUN_HUAI_PEI_CHANG.fieldbyname('SUN_HUAI_PEI_JIAN').AsString;
self.edtCaution.Text:=self.adqT_SUN_HUAI_PEI_CHANG.fieldbyname('SUN_HUAI_YUAN_YIN').AsString;
self.edtMoney.Text:=self.adqT_SUN_HUAI_PEI_CHANG.fieldbyname('PEI_CHANG_JIN_E').AsString;
SELF.edtTreatment.Text:=self.adqT_SUN_HUAI_PEI_CHANG.fieldbyname('CHU_LI_QING_KUANG').AsString;
SELF.cxdDamanaged.Date:= DATEOF(self.adqT_SUN_HUAI_PEI_CHANG.fieldbyname('SUN_HUAI_RI_QI').AsDateTime);
self.adqT_SUN_HUAI_PEI_CHANG.Close;
except
self.Close;
end;
end;
end;
procedure TfrmCompensateEdit.btnOKClick(Sender: TObject);
begin
if trim(self.edtComputerNo.Text)='' then
raise exception.Create('电脑编号不能为空!');
if trim(self.cxdDamanaged.Text)='' then
raise exception.Create('日期不能为空!');
self.adqT_SUN_HUAI_PEI_CHANG.SQL.Clear;
self.adqT_SUN_HUAI_PEI_CHANG.SQL.Text:='select top 1 * from T_SUN_HUAI_PEI_CHANG';
self.adqT_SUN_HUAI_PEI_CHANG.Open;
self.adqT_SUN_HUAI_PEI_CHANG.Append;
self.adqT_SUN_HUAI_PEI_CHANG.fieldbyname('DIAN_NAO_ID').AsString:=self.edtComputerNo.Text;
self.adqT_SUN_HUAI_PEI_CHANG.fieldbyname('XIE_YI_ID').AsString:=self.edtSignNo.Text;
self.adqT_SUN_HUAI_PEI_CHANG.fieldbyname('SUN_HUAI_PEI_JIAN').AsString:= self.edtDamanaged.Text;
self.adqT_SUN_HUAI_PEI_CHANG.fieldbyname('SUN_HUAI_YUAN_YIN').AsString:= self.edtCaution.Text;
self.adqT_SUN_HUAI_PEI_CHANG.fieldbyname('PEI_CHANG_JIN_E').AsString:= self.edtMoney.Text;
self.adqT_SUN_HUAI_PEI_CHANG.fieldbyname('CHU_LI_QING_KUANG').AsString:= SELF.edtTreatment.Text;
self.adqT_SUN_HUAI_PEI_CHANG.fieldbyname('SUN_HUAI_RI_QI').AsDateTime:= SELF.cxdDamanaged.Date;
if self.cxcChangComputer.Checked then
begin
self.adqT_SUN_HUAI_PEI_CHANG.FieldByName('GENG_HUAN_DIAN_NAO').AsBoolean:=true;
self.adqT_SUN_HUAI_PEI_CHANG.FieldByName('GENG_HUAN_DIAN_NAO_ID').AsString:=self.edtNewComputerID.Text;
end;
if not self.cxcNeedRepair.Checked then
self.adqT_SUN_HUAI_PEI_CHANG.FieldByName('SHI_FOU_WEI_XIU').AsBoolean:=true;
self.adqT_SUN_HUAI_PEI_CHANG.Post;
if self.cxcChangComputer.Checked then
begin
self.adqT_SUN_HUAI_PEI_CHANG.Close;
self.adqT_SUN_HUAI_PEI_CHANG.SQL.Clear;
self.adqT_SUN_HUAI_PEI_CHANG.SQL.Text:='update T_QIAN_YUE_XIN_XI set DIAN_NAO_ID='''+trim(self.edtNewComputerID.Text)+''' where ID='''+trim(self.edtSignNo.Text)+'''';
self.adqT_SUN_HUAI_PEI_CHANG.ExecSQL;
end;
self.ModalResult:=mrOK;
end;
procedure TfrmCompensateEdit.btnCancelClick(Sender: TObject);
begin
self.Close;
end;
procedure TfrmCompensateEdit.cxcChangComputerClick(Sender: TObject);
begin
if cxcChangComputer.Checked then
begin
self.Label8.Visible:=true;
self.edtNewComputerID.Visible:=true;
self.edtNewComputerID.SetFocus;
end
else
begin
self.Label8.Visible:=false;
self.edtNewComputerID.Visible:=false;
end;
end;
procedure TfrmCompensateEdit.edtComputerNoPropertiesChange(
Sender: TObject);
begin
if self.edtComputerNo.Text='000000000000' then
begin
self.edtSignNo.Enabled:=false;
self.cxcChangComputer.Enabled:=false;
end;
end;
end.