【发布时间】:2018-05-07 06:18:32
【问题描述】:
更新:
这个问题升级为一个新的/相关的问题,幸运的是@RemyLebeau here 解决了这个问题。
所以,你应该直接去Major flaw - Radio buttons are not correctly set while the form is invisible而不是阅读下面的内容
谢谢雷米
我有两种形式。当我点击一个 radiobtn 时,我想隐藏一个表单并显示第二个。
隐藏 Form1 并显示 Form2:
procedure TForm1.RadioButton2Click(Sender: TObject);
begin
Form2.Visible:= TRUE;
Form1.Visible:= FALSE;
end;
在 Form2 中,我按下一个按钮“返回”到 Form1:
procedure TForm2.Button1Click(Sender: TObject);
begin
Form1.RadioButton1.Checked:= TRUE;
Form1.Visible:= TRUE; <--- this will 'magically' put the RadioButton1 back to false
end;
但是,当我尝试使 Form1 可见时出现此错误:
Project Tester.exe 引发异常类 EInvalidOperation 与 message 'Cannot change Visible in OnShow or OnHide'
在 RadioButton2Click 中设置断点我发现 RadioButton1 在 Form1.Visible:= TRUE 期间被神奇地重新检查(更准确地说是在 TCustomForm.SetVisible 期间)。
为什么在 SetVisible 期间会“神奇地”检查 RadioButton2?
unit Unit1;
INTERFACE
USES
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, System.Actions, Vcl.ActnList, Vcl.StdCtrls;
TYPE
TForm1 = class(TForm)
GroupBox1: TGroupBox;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
procedure RadioButton2Click(Sender: TObject);
procedure RadioButton1Click(Sender: TObject);
private
public
end;
VAR
Form1: TForm1;
IMPLEMENTATION {$R *.dfm}
USES Unit2;
procedure TForm1.RadioButton1Click(Sender: TObject);
begin
Caption:= '1';
end;
procedure TForm1.RadioButton2Click(Sender: TObject);
begin
Caption:= '2';
Form2.Visible:= TRUE;
Form1.Visible:= FALSE;
end;
end.
-
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
public
end;
VAR
Form2: TForm2;
IMPLEMENTATION {$R *.dfm}
USES Unit1;
procedure TForm2.Button1Click(Sender: TObject);
begin
Form1.RadioButton1.Checked:= TRUE;
Form1.Visible:= TRUE;
end;
end.
【问题讨论】:
-
我在 Delphi IDE 中看到其他报告此错误:stackoverflow.com/questions/161187/…
-
可以给我们一个minimal reproducible example吗?
-
站外链接不好。我们在现场需要minimal reproducible example。
-
@DavidHeffernan - 代码粘贴在
-
FWIW 我无法在 XE7 中尝试,但在 10.1 Berlin 中它按预期工作,甚至在 Form2 上单击 button1 时隐藏 Form2(您没有显示)工作正常。
标签: delphi delphi-xe7