delphi中DateTimePicker同时修改日期和时间的实现

第一种方法:

 

第一步: 修改属性

    1、kind -> dtktime 也可以是dtkdate 但是在用dtkdate的时候需要将datemode属性修改成dmUpDown

    2、parseinput -> true

第二步:在onUserInput事件中写一句代码

DateTimePicker1.DateTime:=DateAndTime;

 

注:以上方法虽然可以达到目的,但却会引发新的问题。(这时候控件中可以输入任意字符)

第二种方法:

1。修改delphi的包含Tdatatimepicker的单元ComCtrls.pas  

     搜索:TDateTimePicker.CNNotify(var   Message:   TWMNotify);  在这个函数中,代码:

 procedure   TDateTimePicker.CNNotify(var   Message:   TWMNotify);  
  var  
      DT:   TDateTime;  
      AllowChange:   Boolean;  
  begin  
      with   Message,   NMHdr^   do  
      begin  
          Result   :=   0;  
          case   code   of  
              DTN_CLOSEUP:  
                  begin  
                      FDroppedDown   :=   False;  
                      SetDate(SystemTimeToDateTime(FLastChange));  
                      if   Assigned(FOnCloseUp)   then   FOnCloseUp(Self);  
                  end;  
              DTN_DATETIMECHANGE:  
                  begin  
                      with   PNMDateTimeChange(NMHdr)^   do  
                      begin  
                          if   FDroppedDown   and   (dwFlags   =   GDT_VALID)   then  
                          begin  
                              FLastChange   :=   st;  
                              FDateTime   :=   SystemTimeToDateTime(FLastChange);  
                          end  
                          else   begin  
                              if   FShowCheckbox   and   IsBlankSysTime(st)   then  
                                  FChecked   :=   False  
                              else   if   dwFlags   =   GDT_VALID   then  
                              begin  
                                  FLastChange   :=   st;  
                                  DT   :=   SystemTimeToDateTime(st);  
                            //   if   Kind   =   dtkDate   then   SetDate(DT)  
                            //       else   SetTime(DT);  
                             
  //以上原代码注释掉  
  //My   change  
                                  SetDate(DT);  
                                  SetTime(DT);  
   
  //以上2行我更改  
                                  if   FShowCheckbox   then   FChecked   :=   True;  
                              end;  
                          end;  
                          Change;  
                      end;  
                  end;  
              DTN_DROPDOWN:  
                  begin  
                      DateTimeToSystemTime(Date,   FLastChange);  
                      FDroppedDown   :=   True;  
                      if   Assigned(FOnDropDown)   then   FOnDropDown(Self);  
                  end;  
              DTN_USERSTRING:  
                  begin  
                      with   PNMDateTimeString(NMHdr)^   do  
                      begin  
                          DT   :=   StrToDateTime(pszUserString);  
                          if   Assigned(FOnUserInput)   then  
                          begin  
                              AllowChange   :=   True;  
                              FOnUserInput(Self,   pszUserString,   DT,   AllowChange);  
                              dwFlags   :=   Ord(not   AllowChange);  
                          end  
                          else  
                              dwFlags   :=   Ord(False);  
                          DateTimeToSystemTime(DT,   st);  
                      end;  
                  end;  
          else  
              inherited;  
          end;  
      end;  
  end;

2.修改以后要重新编译这个源文件

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值