【问题标题】:"Cannot change Visible in OnShow or OnHide" error“无法在 OnShow 或 OnHide 中更改可见”错误
【发布时间】: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 delphi-xe7


【解决方案1】:

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


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

【讨论】:

  • 链接的问题不再存在(因审核而被删除)。
  • 这个问题被否决了,所以我不得不删除它。如果有人支持这个问题(所以它的余额不会是负数)我会恢复它。
  • 不能为不存在的问题投票!请在此处添加完整答案。
  • @RobinP-是的。这是 StackOverflow 中的设计(管理/审核)缺陷。
【解决方案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;

它工作正常,没有任何评论。 Delphi 10.1 柏林。

【讨论】:

  • 那又怎样?现在 Form2 仅在选中 RadioButton2 时可见。您还想要什么?
  • 也许是,也许不是。我无法下载您的示例并进行检查。可能是不同的错别字或产生相同效果的东西。
  • @Alaun:不要再坚持让人们去某个地方下载文件了。 .dfm 是文本,可以直接复制并粘贴到您的问题中。任何人都不应该离开这个网站才能获得信息。如果您不能将它包含在您的问题中,则它不存在。请参阅help center 指南,尤其是How to Askminimal reproducible example(之前已多次提及)。
  • @SanderstheSoftwarer-嗨。你的代码和我的不一样。也许这就是您的代码有效的原因。
  • @Alaun:你假设我投票了。你知道关于ass-u-m-ing的说法,对吧?而revenge就意味着还你,我没有什么可报仇的。在没有任何事实的情况下进行疯狂的指责对你来说不会有好处。我对这篇关于 MCVE 的帖子的评论是为了回应您留下的关于我们需要下载您的文件以查看问题的(现已删除的)表扬。既然你已经被点名了,就不要撒谎和装无辜。
猜你喜欢
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
  • 2020-10-25
  • 1970-01-01
  • 2019-08-28
  • 2017-12-23
  • 1970-01-01
  • 2015-03-20
相关资源
最近更新 更多