TEdit focus & caret

I have two TEdit controls. When I exit edit1, edit2 gets focus. In my OnExit Edit1 event, I have the following code:

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

Edit2 has focus. However, it does not have a carriage. I can start typing, but this is confusing because I don’t know whose control has the focus.

What interests me more is that with the flip, the Enabled Property, which triggers some messages, is firing properly? For example, the edit2 OnEnter event does not fire.

This is on the D2006, if that matters at all.

Thanks for the answer.

+5
source share
4 answers

I don’t understand why you turn it off and on edit2, but you will do it:

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

, .

+8

, - , , , . , , .

+8

edit2.

OnExit , . , . , - Application.ProcessMessages.

Screen.Cursor crHourGlass, , .

0

, OnActive MainForm .

TMainForm.OnActivate;
begin
ChildForm.ShowModal;
end;

The management focus is set, but does not work. The work around that I discovered sent PostMessage (Handle, WM_SETFOCUS, 0, 0); to the form handle.

procedure TChildForm.FocusControl(AWinControl: TWinControl);
begin
  try
    // http://stackoverflow.com/questions/7305296/tedit-focus-caret
    PostMessage(Handle, WM_SETFOCUS, 0, 0);
    PostMessage(AWinControl.Handle, WM_SETFOCUS, 0, 0); 
    if AWinControl.CanFocus then
       AWinControl.SetFocus;
  except
    on E: Exception do
    begin
      Error(Self, E);
    end;
  end;
end;
0
source

All Articles