2010-02-15 3 views
5

Ich habe eine DB-Komponente, die DataLink.UpdateRecord aufgerufen wird, wenn sie CM_EXIT Nachricht empfängt. Diese Nachricht wird gesendet, wenn sie den Fokus verliert. Wenn ich auf den Post-Button klicke, verliert es nicht den Fokus und der Wert wird nicht in die Datenquelle geschrieben. Wie kann ich erreichen, dass die Komponente den Fokus verliert, ohne auf einen anderen zu wechseln?Wie kann der Fokus von der aktuell fokussierten Komponente entfernt werden?

Antwort

7

könnten Sie verwenden:

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

Ich schaute auf diese Prozedur, versuchte es zu benutzen, und es hat nicht funktioniert. Jetzt habe ich es nochmal gemacht und es funktioniert. Es ist Zeit, schlafen zu gehen :) – LukLed

+9

Wie es auftritt, die Einstellung Self.ActiveControl: = Nil tut den Job auch und ist intuitiver. Offensichtlich nicht für mich .... – LukLed

+0

@LukLed: Toller Tipp, Danke! – Sharken

3

Werfen Sie einen Blick auf TCustomForm.FocusControl. Sie können nicht den Fokus verlieren, ohne den Fokus auf etwas anderes zu setzen, aber Sie können umschalten und dann sofort zurückschalten, was wahrscheinlich funktionieren würde.

+0

Und wie kann ich es mit Focuscontrol? Der Aufruf der aktiven Steuerung löst keine Ereignisse aus. – LukLed

7

Wir dies zu erreichen, indem die Self.ActiveControl Einstellung: = nil. Dadurch werden alle Exit-Ereignisse ausgelöst. In unserem Fall wollten wir uns auch nach dem Speichern wieder auf die Steuerung konzentrieren. Das erforderte ein paar zusätzliche Überprüfungen, um sicherzustellen, dass wir eine gute Kontrolle hatten, die den Fokus akzeptieren konnte.

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

Es gibt eine SetFocus-Funktion in der Windows-Einheit. Versuchen Sie folgendes:

Windows.SetFocus(0);

Verwandte Themen