关于 CxDateedit中可以输入乱码,导致根据日期查询出错的解决方法

本文介绍了如何在Delphi中使用KeyPress事件禁用CxDateedit输入字母字符,只允许输入数字和回车键,并提供了一个函数来验证表单中所有CxDateedit控件的数据是否符合日期格式要求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)) + '''';

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值