Saturday, May 16, 2020

Make The Enter Key Work Like Tab in Delphi Applications

We know that, generally, pressing the Tab key moves the input focus to next control and Shift-Tab to previous in the tab order of the form. When working with Windows applications, some users intuitively expect the Enter key to behave like a Tab key. There is a lot of third-party code for implementing better data entry processing in Delphi. Here are a few of the best methods out there (with some modifications). Examples below are written with the assumption that there is no default button on the form. When your form contains a button whose Default property is set to True, pressing Enter at runtime executes any code contained in the buttons OnClick event handler. Enter as Tab The next code causes Enter to behave like Tab, and ShiftEnter like ShiftTab: ~~~~~~~~~~~~~~~~~~~~~~~~~procedure TForm1.Edit1KeyPress (Sender: TObject; var Key: Char) ;begin  Ã‚   If Key #13 Then Begin  Ã‚  Ã‚   If HiWord(GetKeyState(VK_SHIFT)) 0 then  Ã‚  Ã‚  Ã‚   SelectNext(Sender as TWinControl,False,True)  Ã‚  Ã‚   else  Ã‚  Ã‚  Ã‚   SelectNext(Sender as TWinControl,True,True) ;  Ã‚  Ã‚  Ã‚   Key : #0  Ã‚   end;end;~~~~~~~~~~~~~~~~~~~~~~~~~ in DBGrid If you want to have similar Enter (ShiftEnter) processing in DBGrid: ~~~~~~~~~~~~~~~~~~~~~~~~~procedure TForm1.DBGrid1KeyPress (Sender: TObject; var Key: Char) ;begin  Ã‚   If Key #13 Then Begin  Ã‚  Ã‚   If HiWord(GetKeyState(VK_SHIFT)) 0 then begin  Ã‚  Ã‚  Ã‚   with (Sender as TDBGrid) do  Ã‚  Ã‚  Ã‚   if selectedindex 0 then  Ã‚  Ã‚  Ã‚  Ã‚   selectedindex : selectedindex - 1  Ã‚  Ã‚  Ã‚   else begin  Ã‚  Ã‚  Ã‚  Ã‚   DataSource.DataSet.Prior;  Ã‚  Ã‚  Ã‚  Ã‚   selectedindex : fieldcount - 1;  Ã‚  Ã‚  Ã‚   end;  Ã‚  Ã‚   end else begin  Ã‚  Ã‚  Ã‚   with (Sender as TDBGrid) do  Ã‚  Ã‚  Ã‚   if selectedindex (fieldcount - 1) then  Ã‚  Ã‚  Ã‚  Ã‚   selectedindex : selectedindex 1  Ã‚  Ã‚  Ã‚   else begin  Ã‚  Ã‚  Ã‚  Ã‚   DataSource.DataSet.Next;  Ã‚  Ã‚  Ã‚  Ã‚   selectedindex : 0;  Ã‚  Ã‚  Ã‚   end;  Ã‚   end;  Ã‚   Key : #0  Ã‚   end;end;~~~~~~~~~~~~~~~~~~~~~~~~~ More Info on Delphi Applications Keyboard Symphony  Get familiar with the OnKeyDown, OnKeyUp, and onKeyPress event procedures to respond to various key actions or handle and process ASCII characters along with other special purpose keys. What Does #13#10 Stand for, in Delphi Code?  If you are wondering what those characters stand for, heres the answer.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.