用好 datetimepicker(180)

  • 主题发起人 主题发起人 lijucheng8
  • 开始时间 开始时间
L

lijucheng8

Unregistered / Unconfirmed
GUEST, unregistred user!
请教:DatetimePicker 显示为 年/月/日, 按Enter键自动跳转,到日后 焦点移动到其他控件 ,为什么要这样:当输入大量数据时,客户觉得按TAB键麻烦,同理:当有二个datetimepicker时,每次得到焦点,希望光标落在YYYY上的。以下是程序, 原理就是当在日上按回车时,日改变,这样就跳到下一目标。调了一天都调不好procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);begin if key=vk_return then begin if (ActiveControl is Tdatetimepicker) then begin IsDayChange:=false;// d:=picker1.Date; //先取得日期 keybd_event(vk_up,0,0,0); keybd_event(vk_up,0,KEYEVENTF_KEYUP,0); keybd_event(vk_down,0,0,0); keybd_event(vk_down,0,KEYEVENTF_KEYUP,0); //上下移动,值不会变,但应该会引发picker1的change 事件,但总是不能捕捉到 if IsDayChange then keybd_event(vk_tab,0,0,0) else keybd_event(vk_right,0,0,0); end else SendMessage(Self.Handle, WM_NEXTDLGCTL, 0, 0); end;end;procedure TForm1.Picker1Change(Sender: TObject);begin if d<>picker1.Date then IsDayChange:=true;end; 原理就是当在日上按回车时,日改变,这样就跳到下一目标,
 
//在DateTimePicker的OnKeyDown事件里进行处理,//所有的DateTimePicker共用该事件procedure TForm1.DateTimePicker1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);var d1,d2:TDateTime;begin if key=13 then //回车 begin d1:=TDateTimePicker(Sender).DateTime; TDateTimePicker(Sender).Perform(WM_KEYDOWN,VK_UP,0); d2:=TDateTimePicker(Sender).DateTime; TDateTimePicker(Sender).DateTime:=d1; if (d2-d1>1) and (d2-d1<=365) then //焦点在年和月上 key:=VK_RIGHT else //焦点在日期上,此处不知道楼主的日改变是怎么个变法 SelectNext(TWinControl(Sender),True,True); end;end;
 
谢谢wangdonghai了,后一半还希望解决:如何实现当datetimepicker每次得到焦点,光标落在YYYY上的
 
procedure TForm1.DateTimePicker1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);var d1,d2:TDateTime;begin if key=13 then //回车 begin d1:=TDateTimePicker(Sender).DateTime; TDateTimePicker(Sender).Perform(WM_KEYDOWN,VK_UP,0); d2:=TDateTimePicker(Sender).DateTime; TDateTimePicker(Sender).DateTime:=d1; if (d2-d1>1) and (d2-d1<=365) then //焦点在年和月上 key:=VK_RIGHT else //焦点在日期上,此处不知道楼主的日改变是怎么个变法 begin //加上这一行代码就行了 TDateTimePicker(Sender).Perform(WM_KEYDOWN,VK_RIGHT,0); SelectNext(TWinControl(Sender),True,True); end; end;end;
 
谢谢了,根据你的提示,改了一点点,才可以实际运行,只是不知道什么原因: // TDateTimePicker(Sender).Perform(WM_KEYDOWN,VK_RIGHT,0);改为 keybd_event(VK_RIGHT,0,0,0);才可以。下面是程序:其实这个问题有实际用途的,而且我查了一下提这个问题的好像都没有得到最终那个结果的.谢谢,祝你好运!procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);var d1,d2:TDatetime; begin if key=vk_return then begin if (ActiveControl is Tdatetimepicker) then begin d1:=(ActiveControl as Tdatetimepicker).Datetime; (ActiveControl as Tdatetimepicker).Perform(WM_KEYDOWN,VK_UP,0); d2:=(ActiveControl as Tdatetimepicker).Datetime; (ActiveControl as Tdatetimepicker).Datetime:=d1; if (d2-d1>1) and (d2-d1<=365) then //焦点在年和月上 key:=VK_RIGHT else //焦点在日期上,此处不知道楼主的日改变是怎么个变法 begin keybd_event(VK_RIGHT,0,0,0); // TDateTimePicker(Sender).Perform(WM_KEYDOWN,VK_RIGHT,0); keybd_event(vk_tab,0,0,0); end; end else SendMessage(Self.Handle, WM_NEXTDLGCTL, 0, 0); end; end;
 
原因就是我们两个的KeyDown事件不一样,我是在DateTimePicker的OnKeyDown事件里进行处理,而你是在窗体的OnKeyDown事件里进行处理,两个事件的Sender参数不一样,所以TDateTimePicker(Sender).Perform(WM_KEYDOWN,VK_RIGHT,0);这段代码有问题,你也可以改成(ActiveControl as Tdatetimepicker).Perform(WM_KEYDOWN,VK_RIGHT,0);
 
后退
顶部