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?
 
MadshiConnect With a Mentor Commented:
So if you're happy with my comment, I'll post an answer...   :-)

Regards, Madshi.
0
 
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
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
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
 
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
All Courses

From novice to tech pro — start learning today.