Delphi 如何从当前焦点组件中移除焦点?
我有一个DB组件,当它接收到CM_退出消息时调用DataLink.UpdateRecord。此消息在失去焦点时发送。当我单击post按钮时,它不会失去焦点,并且值不会写入数据源。如何在不切换到其他组件的情况下达到组件失去焦点的效果?请查看Delphi 如何从当前焦点组件中移除焦点?,delphi,focus,delphi-7,Delphi,Focus,Delphi 7,我有一个DB组件,当它接收到CM_退出消息时调用DataLink.UpdateRecord。此消息在失去焦点时发送。当我单击post按钮时,它不会失去焦点,并且值不会写入数据源。如何在不切换到其他组件的情况下达到组件失去焦点的效果?请查看TCustomForm.FocusControl。如果不将焦点切换到其他对象,则无法使其失去焦点,但您可以切换,然后立即切换回来,这可能会起作用。您可以使用: procedure TCustomForm.DefocusControl(Control: TWinC
TCustomForm.FocusControl
。如果不将焦点切换到其他对象,则无法使其失去焦点,但您可以切换,然后立即切换回来,这可能会起作用。您可以使用:
procedure TCustomForm.DefocusControl(Control: TWinControl; Removing: Boolean);
我们通过设置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;
过程tsaleditor.SaveCurrentState();
变量
SavedActiveControl:TWinControl;
备选视频控制:TWinControl;
开始
//强制当前控件退出并保存任何状态。
如果Self.ActiveControl为零,则
开始
SavedActiveControl:=Self.ActiveControl;
//我们可能有一个就地网格编辑器作为当前控件。那样的话,我们
//将无法将其重置为活动控件。这将导致
//滚动框滚动至活动控件,该控件将是最低的选项卡顺序
//控制。我们的“真实”控件有名称,而动态在位编辑器没有
//找到另一个控件来设置焦点,方法是向上遍历父列表,直到
//查找命名控件。
AlternateSavedControl:=SavedActiveControl;
而(AlternateSavedControl.Name='')和(AlternateSavedControl.Parent nil)不
开始
AlternateSavedControl:=AlternateSavedControl.Parent;
结束;
Self.ActiveControl:=nil;
//如果控件是单选按钮,则不要重新设置焦点
//因为如果您取消选择单选按钮,这将自动
//重新选择它
如果(SavedActiveControl.CanFocus=true)和
((SavedActiveControl为TcxRadioButton)=false)然后
开始
Self.ActiveControl:=SavedActiveControl;
结束
否则如果(AlternateSavedControl.CanFocus=true)和
((AlternateSavedControl为TcxRadioButton)=false)然后
开始
Self.ActiveControl:=交替视频控制;
结束;
结束;
结束;
windows单元中有一个设置焦点功能。试试这个:
Windows.SetFocus(0)代码>我如何使用FocusControl实现这一点?在active control上调用它不会触发事件。我查看了这个过程,尝试使用它,但它不起作用。现在我又做了一次,效果很好。现在是睡觉的时候了:)当它发生时,设置Self.ActiveControl:=nil也会起作用,而且更直观。显然不适合我…@LukLed谢谢,这太棒了!太好了,因为我正在寻找一个全球性的水平来实现这一点。不幸的是,我刚刚尝试了表单的OnCreate
和OnShow
,但重点仍然放在了表单中。顺便说一句,以下解决方案似乎都不适用于表单的OnCreate
或OnShow
事件。OnActive
有效,但是,在点击表单之后,一些东西最终得到了关注。