Link to home
Start Free TrialLog in
Avatar of petershaw9
petershaw9

asked on

How to avoid OnKeyDown fires OnExit procedure

I write codes in the Edit box OnExit event and OnKeyDown event. the OnKeyDown procedure likes
begin
  if Key = VK_F4 then
    F4AutoClick(CmbDeptFrom, edtDeptFrom);
end

Problem is when I press F4, it fires OnExit procedure first, but I don't want OnExit fired. How can I solve the problem? Thanks.

Peter Shaw9
Avatar of kretzschmar
kretzschmar
Flag of Germany image

hi peter,

what does this code?
>F4AutoClick(CmbDeptFrom, edtDeptFrom);

if i press F4 the onexit-event does not appear. i guess, that the F4AutoClick-procedure moves the focus to another control (a button?), which then causes the exit-event of your edit-box

meikl
Avatar of Madshi
Madshi

If all else fails, you could set a boolean variable to true in the onKeyDown handler like this:

  if Key = VK_F4 then begin
    IgnoreNextOnExit := true;
    F4AutoClick(...);
  end;

Then in the OnExit handler you could do this:

  if IgnoreNextOnExit then begin
    IgnoreNextOnExit := false;
    exit;
  end;
  ...  // rest of the code of OnExit

Regards, Madshi.
peter:

  if (Yourform.keypreview=true)and(yourformonkeydown.ishandled) then begin
   yourform.keydown;
   youredit.keydown;
end;
....

Perhaps,the problem is in yourform.keydown.

menxin
Avatar of petershaw9

ASKER

The F4Autoclick is:
procedure TfrmSupport.F4AutoClick(CmBox : TComboBox; dbEdt : TdbEdit);
var i, Ix : integer;
begin
  With CmBox do begin
    Ix := -1 ;
    With Items do
      for i := 0 to Count -1 do
        if copy(Items[i], 1, 3) = copy(dbEdt.Text, 1, 3) then
           Ix := i ;
    ItemIndex := Ix;
    SetFocus;
  end;
  KeyBd_Event(Vk_F4,0,0,0);
  KeyBd_Event(Vk_F4,0,KEYEVENTF_KeyUp,0);
end;
hi peter,

try this little change


                      procedure TfrmSupport.F4AutoClick(CmBox : TComboBox; dbEdt :
                      TdbEdit);
                      var i, Ix : integer;
                      begin
                        With CmBox do begin
                          Ix := -1 ;
                          With Items do
                            for i := 0 to Count -1 do
                              if copy(Items[i], 1, 3) = copy(dbEdt.Text, 1, 3) then
                                 Ix := i ;
                          ItemIndex := Ix;
                        end;
                        KeyBd_Event(Vk_F4,0,0,0);
                        KeyBd_Event(Vk_F4,0,KEYEVENTF_KeyUp,0);
                        CmBox.SetFocus;  //this line moved, or delete it if not work, as recommended
                      end;

meikl
I try Kretzschmar way, it works, but the combobox behavior becomes very strange. Now I am using Madshi suggestion. It works properly.
Many thanks everyboby

PeterShaw9
ASKER CERTIFIED SOLUTION
Avatar of Madshi
Madshi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial