1、在CxDateedit的KeyPress事件中加以下代码,禁止输入字母,只能输入数字和退格键
procedure TFrmPurchaseReqHistory.EdtCreationBeginDateKeyPress(
Sender: TObject; var Key: Char);
begin
inherited;
if not (key in ['0'..'9', #8]) then key := #0;
end;
2、要查询之前检查一下CxDateedit中的数据是否符合日期型,
检查FORM中所有CxDateedit中的值是否符合日期类型的要求。
function CheckAllCxDateeditValidate(Aform: TForm): boolean;
var
i, j: integer;
Da: TDateTime;
begin
Result := true;
j := aform.ComponentCount;
for i := 0 to j - 1 do
begin
if Aform.Components[i] is TcxDateEdit then
begin
if (TcxDateEdit(Aform.Components[i]).Text) = '' then Continue;
if not (TryStrToDate(TcxDateEdit(Aform.Components[i]).Text, da)) then
begin
Result := false;
MsgDlg(TcxDateEdit(Aform.Components[i]).Hint + '不合法,请检查!!');
//Aform.Components[i]).Hint 表示日期名字比如订单日期,创建日期等
end;
end;
end;
end;
3、查询代码:
if Trim(EdtCreationBeginDate.Text) <> '' then
sql := sql + ' and CreationDate>=''' + FormatDateTime('yyyy-mm-dd', strtodatetime(EdtCreationBeginDate.Text)) + '''';