Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove focus from currently focused component?

I have a DB component which DataLink.UpdateRecord is called when it receives CM_EXIT message. This message is sent when it loses focus. When I click post button, it doesn't lose focus and value is not written to datasource. How can I reach an effect of component losing focus without switching it to other one?

like image 312
LukLed Avatar asked Feb 15 '10 06:02

LukLed


People also ask

How do you remove focus from current element?

The blur() method removes focus from an element.

How do you remove window focus?

The blur() method removes focus from a window.

How do you Unfocus a button?

If you want to remove the focus around a button, you can use the CSS outline property. You need to set the “none” value of the outline property.

How Stop focus in jQuery?

In jQuery by using blur() property we can remove focus from input textbox field.


2 Answers

You could use:

procedure TCustomForm.DefocusControl(Control: TWinControl; Removing: Boolean);
like image 129
André Avatar answered Oct 26 '22 03:10

André


We accomplish this by setting the Self.ActiveControl := nil. That causes all of the exit events to fire. In our case we wanted to also re-focus back to the control after the save took place. That required a few extra checks to ensure we had a good control that could accept focus.

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;
like image 26
Mark Elder Avatar answered Oct 26 '22 05:10

Mark Elder