Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OnExit event: Query regarding effects of keystrokes.

Status
Not open for further replies.

CHeighlund

Programmer
Jun 11, 2007
163
US
I've been asked to look into a project with a TcxMaskEdit component that's throwing some problems. The main one is this:

The user is expected to hit tab to move between fields. The TcxMaskEdit in question is the last field, and it is suspected that the user will hit enter at this point to enter the record. It is possible for this field to be left blank, so in the OnExit event, if the field is empty a popup dialogue is given, asking whether or not this was intended. (Might have been done accidentally.) If the dialogue is answered with 'no', then the focus is supposed to be set back to the TcxMaskEdit field. Otherwise, focus is set to the submit button.

On leaving the TcxMaskEdit field with a Tab, the field remains enabled, and the SetFocus call works nicely. On leaving the field with an Enter, the field becomes disabled, causing the SetFocus call to throw an error. (Can't set focus to an invisible or disabled object.) Anyone have any clue why this difference occurs, or what needs to be done to stop it? I've cludged in an automatic component.enabled event right before the SetFocus call, but I'd prefer to find some way of not having to use that if possible.

Also, when the user leaves the TcxMaskEdit component by pressing tab, all information remains in the other fields on the screen. When the user presses Enter, all of the fields on screen are emptied. This is done the instant the key is pressed; if the user accidentally pressed the key, then as things stand now they would have to go back and fill in the necessary fields again. It would seem to me that this is most decidedly not an acceptable behavior. I would appreciate hearing if anyone know of any documentation which explains this behavior, or offers ways around it.

Any suggestions?
 
I've found that the more custom events you put on a DB input dialog form, the more headaches you have maintaining it.

If you must, I would put a common 'onkeypress' event on all of the input boxes and negate 'enter':
Code:
procedure TForm1.DBEditAllKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then key:= #0
end;
Then put your validation (MessageDlg - check for blank fields) in your SaveBtnClick event, sending it back to the appropriate edit box if the blank fiend was unintentional.
Code:
procedure TForm1.SaveBtnClick(Sender: TObject);
begin
  if (DBEdit1.Text = '') or
     (DBEdit2.Text = '') or
     (DBEdit3.Text = '') then //and so on...
    if MessageDlg('Field is blank, save anyway?', mtConfirmation, [mbYes, mbNo], 0, mbYes) = mrYes then
      Post
    else
      ActiveControl:= DBEdit1
end;

This disables saving by pressing enter, and requires a Save and Cancel button. There are other ways, but is my suggestion.

Roo
Delphi Rules!
 
The onKeyPress event doesn't take place until after the onExit code has already run; the errors are still getting thrown, and tagging a breakpoint on the onKeyPress command caused it to stop just after the error hit.

There's another event, onKeyDown, right above the onKeyPress. It requires that the key be passed in as a Word datatype, rather than as a Char datatype. Anyone know if that might do what I'm looking for?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top