- 论坛徽章:
- 0
|
delphi 中焦点的使用
delphi 中焦点的使用
在编程过程当中,有时我们需要人为地传递焦点到下一个 Control目的方便我们或者是用户更好的使用,在大量的数据单元输入或有大量的使用 TEDIT( TDBEDIT ) 控件时更是如此。笔者就日常使用的一些方法总结出来,以荀读者。 从简单的开始,使用消息传递机制: 1、把焦点传递到下一 Control: SendMessage(Form1.Handle,WM_NEXTDLGCTL,0,0); 2、把焦点传递到上一 Control: SendMessage(Handle,WM_NEXTDLGCTL,1,0); 这种方法的好处是无需考虑 Control 的数量、位置及 TabOrder 的顺序。 使用 Delphi 的内部函数 A: 1、把焦点传递到下一 Control: SelectNext((Sender AS TWinControl),true,false); 2、把焦点传递到上一 Control: SelectNext((Sender AS TWinControl),false,false); 这种方法的缺点是需考虑 TabOrder 的顺序。 使用 Delphi 的内部函数 B: 1、把焦点传递到下一 Control: FocusControl(FindNextControl((Sender AS TWinControl),True,False,false)); 2、把焦点传递到上一 Control: FocusControl(FindNextControl((Sender AS TWinControl),False,False,false)); 这种方法的缺点是需考虑 TabOrder 的顺序。 使用 Delphi 的内部函数 C: 1、把焦点传递到下一 Control: (FindNextControl((Sender AS TWinControl),True,False,false) As TWinControl).SetFocus; 2、把焦点传递到上一 Control: (FindNextControl((Sender AS TWinControl),False,False,false) As TWinControl).SetFocus; 这种方法的缺点是需考虑 TabOrder 的需要用回车键代替TAB键下移一个控件时,把KeyPress设为True,加入下列代码拦截击键: Procedure TForm1.FormKeyPress(Sender:Tobject;Var Key:Char); Begin if key=#13 then { 判断是按执行键} if not (ActiveControl is TDbgrid) Then Begin { 不是在TDbgrid控件内} key:=#0; perform(WM_NEXTDLGCTL,0,0);{移动到下一个控件} end else if (ActiveControl is TDbgrid) Then{是在 TDbgrid 控件内} begin With TDbgrid(ActiveControl) Do if Selectedindex<(FieldCount-1) then Selectedindex:=Selectedindex+1{ 移动到下一字段} else Selectedindex:=0; end; End;
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/1507/showart_47321.html |
|