"无法在OnShow或OnHide上更改Visible"错误

时间:2017-11-23 10:11:02

标签: delphi delphi-xe7

更新:
这个问题升级为一个新的/相关的问题,幸运的是@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 with   消息&#39;无法在OnShow或OnHide&#39;

中更改Visible

在RadioButton2Click中设置一个断点我发现在Form1.Visible:= TRUE期间(在TCustomForm.SetVisible期间更精确地),RadioButton1被神奇地重新检查。

为什么RadioButton2&#39;神奇地&#39;在SetVisible期间检查?

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.

2 个答案:

答案 0 :(得分:1)

解决方法(而不是最终修复)是在对GUI(form1)进行更改后对其进行更改!

更新!
该错误与TabOrder属性有关! Details

答案 1 :(得分:-2)

从一个表单到另一个表单的字段的直接引用是糟糕的设计,我强烈建议您更改它。但无论如何,我做了两种形式:首先是两个单选按钮,第二个是按钮。处理程序:

{ For both radiobuttons }
procedure TForm1.RadioButtonClick(Sender: TObject);
begin
  Form1.Visible := RadioButton1.Checked;
  Form2.Visible := RadioButton2.Checked;
end;

{ For button }
procedure TForm2.Button1Click(Sender: TObject);
begin
  Form1.RadioButton1.Checked := true;
end;

工作正常,没有任何评论。德尔福10.1柏林。