TEdit focus & caret

Rick picture Rick · Sep 5, 2011 · Viewed 10.2k times · Source

I got two TEdit controls. When I tab out of edit1, edit2 receives the focus. On my OnExit event of Edit1 I have the following code:

procedure TForm1.Edit1Exit(Sender: TObject);
begin
  edit2.Enabled := false;
  edit2.Enabled := true;
  edit2.setfocus;
end;

Edit2 has the focus. However, there is no caret in it. I can start typing but it's confusing as I do not know which control has the focus.

I'm more interested on what's with the flipping of the Enabled property that's causing some messages to be not firing properly ? For instance edit2's OnEnter event is not being triggered.

This is on D2006 if it matters at all.

Thanks for the reply.

Answer

Whiler picture Whiler · Sep 5, 2011

I don't understand why you disable and enable edit2, but you do this:

procedure TForm1.Edit1Exit(Sender: TObject);
begin
  edit2.Enabled := false;
  edit2.Enabled := true;
  edit2.setfocus;
  PostMessage(edit2.Handle, WM_SETFOCUS, 0, 0);
end;

BTW, I agree with Andreas Rejbrand.