Mega Code Archive

 
Categories / Delphi / Examples
 

Textboxinput

procedure TMakeOrEditBForm.NameBoxKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); {we use 'virtual key codes' below -these are useful for trapping keys} {like up-arrow, down-arrow, backspace, return, etc BUT if for example} {you want to validate user input it's better to do that by looking at} {each character in InputBox.Text in turn AFTER the whole line} {of input is put in by the user rather than by translating from virtual} {codes to characters as they are typed in: this is because the} {mapping of 'virtual' to ASCII characters can get complicated and it's} {better to let Delphi itself do that since by default text gets put} {into InputBox.Text when the focus shifts away from the input box...} var Redundant: Integer; purpose : String; begin purpose := Copy(MakeOrEditBForm.Caption, 1, 4); {there IS such a thing as a masked edit box (useful for input validation)} {but this way we've got total control...} case Key of VK_UP, VK_LEFT : NameBox.SetFocus; VK_DOWN, VK_RIGHT : if purpose = 'Seat' then begin MakeOrEditBForm.BookedBox.SetFocus end else begin MakeOrEditBForm.SeatNoBox.SetFocus end; VK_RETURN : NameBoxProcess(Redundant); VK_BACK : NameBox.Clear; {without next line the shift character on it's own will be considered} {non-alphabetic so generating an error message...} VK_SHIFT : {do nothing}; end; end;