delphi events OnkeyDown and OnExit

0

I have the following code in the events:

OnExit:

procedure TFEqt_Pallet.EdNr_PalletExit(Sender: TObject);
var
 MeuKey: word;
begin
 MeuKey := 13;
 EdNr_PalletKeyDown(EdNr_Pallet,MeuKey,[]); 
end;

OnKeyDown:

procedure TFEqt_Pallet.EdNr_PalletKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Ord(key) = 13 then
begin
...
 proximocampo.setfocus;
end;

When I give an enter in the component it is calling the OnExit method twice. How can I resolve this?

    
asked by anonymous 09.01.2017 / 17:42

2 answers

1

When you press the Enter key in Edit, your KeyDown procedure performs a series of things and at the end "arrow" the focus on another component, when this occurs, the OnExit procedure is called and in turn it calls again the KeyDown procedure, so this repetition.

I believe you are doing this because you want to perform a series of procedures when the user press enter and / or exit Edit.

You should put these procedures in the onExit and onKeyDown only the setfocus for the next component.

Example:

OnExit:

procedure TFEqt_Pallet.EdNr_PalletExit(Sender: TObject);
var
 MeuKey: word;
begin
 O aque eu quero que faça.
end;

OnKeyDown:

procedure TFEqt_Pallet.EdNr_PalletKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Ord(key) = 13 then
begin
 proximocampo.setfocus;
end;
    
17.05.2017 / 20:01
0

Ideally, you should not have focus-shifting control inside components, leave it to the form. Follow the steps below and improve your font a bit

  • Set up the TabOrder's correctly for the components of your form.
  • Activate the KeyPreview of form.
  • In the KeyDown event of form put the code:
  • if Key = VK_RETURN then Perform(WM_NEXTDLGCTL, 0, 0);

    Tip: Do not use focus control in the OnExit component. If you are in one component and decide to go back in another by clicking with the mouse, the focus will not go where it should. It annoys.

        
    09.01.2017 / 18:32