当再次显示表单时,单选按钮保存上次检查而不是所需的检查 [英] RadioButtons save last checked instead of desired one when form displayed again
问题描述
我在 Form2
上有一些 TRadioButton
,然后调用 Form2
从 Form1
使用以下代码:
I have some TRadioButton
s on Form2
and call Form2
from Form1
with this code:
procedure TForm1.btnCallForm2Click(Sender:TObject)
begin
Form2.RadioButton2.Checked:= true;
Form2.ShowModal;
end;
如果用户单击 btnCallForm2
, Form2
将显示,然后用户单击 RadioButton3
并关闭该窗体,然后通过单击 btnCallForm2重新打开它
。
If user clicked btnCallForm2
, Form2
will be displayed, then user clicks RadioButton3
and closes the form, and then reopen it by clicking btnCallForm2
again.
现在再次显示 Form2
,但改为选中 RadioButton3
RadioButton2
。
Now Form2
displayed again but RadioButton3
is checked instead of RadioButton2
.
问:这是什么行为,是一个错误吗?如何设置选中我想要的 RadioButton
而不是用户上一轮选择的内容?
Q: What is this behavior, is it a bug? How to set checked my desired RadioButton
instead of what is selected by the user in previous turn?
推荐答案
这不是错误。之所以具有这种奇怪的行为,是因为如果Form2没有被销毁,那么下次它变得可见时(例如 ShowModal
),它会记住哪个控件具有焦点。
This is not a bug. The reason that you have this "strange" behavior is that if the Form2 is not destroyed, then next time it becomes visible (e.g. ShowModal
) it remembers which control had the focus.
您所关注的最后一个控件是RadioButton3(因为单击它可以更改已选中状态)。因此,即使将已检查状态更改回RadioButton2,下次激活该窗体时,焦点也会恢复到RadioButton3。为了恢复焦点,向控件发送了 WM_SETFOCUS
。阅读文档用于按钮控件的默认消息处理:
In your case the last control in focus is the RadioButton3 (because you clicked on it to change the "checked" state). So even if you are changing the "checked" state back to RadioButton2, the focus will be restored to RadioButton3 when the form is next activated. To restore the focus, the control is sent a WM_SETFOCUS
. Read the rest from documentation for default message processing for button controls:
WM_SETFOCUS 在按钮上绘制焦点矩形以获取焦点。
对于单选按钮和自动单选按钮,父窗口是
发送的 BN_CLICKED 通知代码。
此 BN_CLICK
通知( WM_COMMAND
消息)将单选按钮的状态设置为选中。
This BN_CLICK
notification (WM_COMMAND
message) sets the state of the radio button to checked.
背后的原理使用键盘导航单选按钮时,可以发现此行为。当您在其中一个单选按钮上按下向上/向下箭头时,选中下一个接收焦点的单选按钮。
The rationale behind this behavior can be found while navigating the radio buttons with the keyboard. When you press up/down arrow while on one of the radio buttons, the next radio button that receives the focus becomes checked.
此行为仅适用于单选按钮,例如,请尝试对另一个控件(例如复选框)执行相同的操作,以确保它具有焦点时其状态不会改变。您将看到一切都按预期运行
This behavior only applies to radio buttons, e.g., try the same with another control (e.g. check box), that its state is not altered when it has focus. You will see that everything is working as expected
如kobik所建议的那样,一种快速简便的解决方案是在显示Form2之前将ActiveControl设置为nil
As kobik was suggesting, a fast and easy solution would be to set the ActiveControl to nil before showing the Form2
Form2.ActiveControl := nil;
或根据文档:
Form2.ActiveControl := Form2.RadioButton2;
或者您可以隐藏并重新创建如下表格:
or you could destoy and recreate the form as following:
从Project-> Options-> Forms中的自动创建的表单中删除Form2,并在ButtonClick事件中手动创建它
Remove the Form2 from the AutoCreated Forms in Project->Options->Forms and create it manually in the ButtonClick event
procedure TForm1.btnCallForm2Click(Sender:TObject)
begin
Form2 := TForm2.Create(nil);
try
Form2.RadioButton2.Checked:= true;
Form2.ShowModal;
finally
FreeAndNil(Form2);
end;
end;
这篇关于当再次显示表单时,单选按钮保存上次检查而不是所需的检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!