How to remove focus from the current focused component?

I have a DB component that is called by DataLink.UpdateRecord when it receives a CM_EXIT message. This message is sent when it loses focus. When I press the message button, it does not lose focus, and the value is not written to the data source. How can I achieve the effect of component loss without switching it to another?

+5
source share
4 answers

You can use:

procedure TCustomForm.DefocusControl(Control: TWinControl; Removing: Boolean);
+8
source

, Self.ActiveControl: = nil. . . , , , .

procedure TSaleEditor.SaveCurrentState();
var
  SavedActiveControl: TWinControl;
  AlternateSavedControl: TWinControl;
begin

  // Force the current control to exit and save any state.
  if Self.ActiveControl <> nil then
  begin
    SavedActiveControl := Self.ActiveControl;

    // We may have an inplace grid editor as the current control.  In that case we
    // will not be able to reset it as the active control.  This will cause the
    // Scroll box to scroll to the active control, which will be the lowest tab order
    // control.  Our "real" controls have names, where the dynamic inplace editor do not
    // find an Alternate control to set the focus by walking up the parent list until we
    // find a named control.
    AlternateSavedControl := SavedActiveControl;
    while (AlternateSavedControl.Name = '') and (AlternateSavedControl.Parent <> nil) do
    begin
      AlternateSavedControl := AlternateSavedControl.Parent;
    end;

    Self.ActiveControl := nil;

    // If the control is a radio button then do not re-set focus
    // because if you are un-selecting the radio button this will automatically
    // re-select it again
    if (SavedActiveControl.CanFocus = true) and
      ((SavedActiveControl is TcxRadioButton) = false) then
    begin
      Self.ActiveControl := SavedActiveControl;
    end
    else if (AlternateSavedControl.CanFocus = true) and
      ((AlternateSavedControl is TcxRadioButton) = false) then
    begin
      Self.ActiveControl := AlternateSavedControl;
    end;

  end;

end;
+7

Take a look TCustomForm.FocusControl. You cannot lose focus without switching the focus to something else, but you can switch and then switch back immediately, which will probably work.

+3
source

There is a SetFocus function in the Windows module. Try the following:

Windows.SetFocus(0);

+2
source

All Articles