我有一个数据库组件,当它接收到CM_EXIT消息时会调用DataLink.UpdateRecord函数。这个消息是在失去焦点时发送的。当我点击post按钮时,它不会失去焦点,值也不会被写入数据源。那么,如何使组件失去焦点而不需要切换到其他组件呢?
你可以使用以下代码:
procedure TCustomForm.DefocusControl(Control: TWinControl; Removing: Boolean);
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;
请查看TCustomForm.FocusControl
。你不能让它失去焦点而不切换到其他东西,但你可以先切换,然后立即切换回来,这可能会起作用。
Windows.SetFocus(0);
OnCreate
和 OnShow
,但焦点仍然在任何情况下都会进入。 - Jerry Dodge