保存上次检查而不是所需的

保存上次检查而不是所需的

本文介绍了再次显示表单时,RadioButtons 保存上次检查而不是所需的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Form2 上有一些 TRAdioButton,并使用以下代码从 Form1 调用 Form2:

I have some TRadioButtons 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中的AutoCreated 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;

这篇关于再次显示表单时,RadioButtons 保存上次检查而不是所需的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 08:07