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
petershaw9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
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
0
MadshiCommented:
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.
0
men xinCEOCommented:
peter:

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

Perhaps,the problem is in yourform.keydown.

menxin
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

petershaw9Author Commented:
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;
0
kretzschmarCommented:
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
0
petershaw9Author Commented:
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
0
MadshiCommented:
So if you're happy with my comment, I'll post an answer...   :-)

Regards, Madshi.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.