2010-02-15 22 views
5

Tengo un componente DB al que se llama DataLink.UpdateRecord cuando recibe el mensaje CM_EXIT. Este mensaje se envía cuando pierde el foco. Cuando hago clic en el botón Publicar, no pierde el foco y el valor no se escribe en el origen de datos. ¿Cómo puedo alcanzar un efecto de pérdida de foco del componente sin cambiarlo a otro?¿Cómo eliminar el foco del componente actualmente enfocado?

Respuesta

7

que puede usar:

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

Miré este procedimiento, intenté usarlo y no funcionó. Ahora lo hice de nuevo y funciona. Es hora de ir a dormir :) – LukLed

+9

Como ocurre, establecer Self.ActiveControl: = nil también hace el trabajo y es más intuitivo. Obviamente no para mí ... – LukLed

+0

@LukLed: ¡Un gran consejo, gracias! – Sharken

3

Echa un vistazo a TCustomForm.FocusControl. No puede hacer que pierda el foco sin cambiar el foco a otra cosa, pero puede cambiar y luego volver inmediatamente, lo que probablemente funcione.

+0

Y qué puedo hacer yo con FocusControl? Llamarlo a control activo no desencadena eventos. – LukLed

7

Logramos esto mediante el establecimiento de la Self.ActiveControl: = nil. Eso causa que todos los eventos de salida se disparen. En nuestro caso, también queríamos volver a enfocarnos en el control una vez que se realizó el guardado. Eso requirió algunos controles adicionales para garantizar que teníamos un buen control que pudiera aceptar el enfoque.

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; 
2

Hay una función SetFocus en la unidad de Windows. Prueba esto:

Windows.SetFocus(0);

Cuestiones relacionadas