打开和关闭vcl窗体

发布于 2024-11-05 20:26:53 字数 385 浏览 0 评论 0原文

现在我有两种形式。在 Form1 上,我像这样打开 Form2:

procedure TForm1.Action1Execute(Sender: TObject);
var
  Form2: TForm2;
begin
  Form2 := TForm2.Create(Form2);
  Form2.ShowModal;
  Form2.Free;
end;

现在我想用按钮关闭 Form2。所以我尝试了

procedure TForm2.cancelBtnClick(Sender: TObject);
begin
  Form2.Close;
end;`

但是当我单击该按钮时这只会给我访问冲突错误。我做错了什么?

Right now i have 2 forms. On Form1 i open up Form2 like this:

procedure TForm1.Action1Execute(Sender: TObject);
var
  Form2: TForm2;
begin
  Form2 := TForm2.Create(Form2);
  Form2.ShowModal;
  Form2.Free;
end;

Now i want to close the Form2 with a button on it. So i tried

procedure TForm2.cancelBtnClick(Sender: TObject);
begin
  Form2.Close;
end;`

But that only gives me Access violation error when i click that button. What i am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

没有你我更好 2024-11-12 20:26:53

执行此操作的正常方法是

procedure TForm1.Action1Execute(Sender: TObject);
begin
  with TForm2.Create(nil) do
    try
      ShowModal;
    finally
      Free;
    end;
end;

,如果 TForm2 包含“确定”按钮,则应在设计时将 ModalResult 属性设置为 mrOK -时间。使用对象检查器来设置它。您可能还想将 Default 设置为 True。现在您可以通过按键盘上的 Enter 键来“单击”确定按钮!

此外,如果对话框中有“取消”按钮,则应将 ModalResult 设置为 mrCancel 并将 Cancel 设置为 True< /代码>。现在您可以通过按键盘上的 Escape 键来“单击”取消按钮!

具有 ModalResult 值的按钮将自动关闭模式对话框。

The normal way to do this is to do

procedure TForm1.Action1Execute(Sender: TObject);
begin
  with TForm2.Create(nil) do
    try
      ShowModal;
    finally
      Free;
    end;
end;

and, if TForm2 contains a OK button, this should have the ModalResult property set to mrOK at design-time. Use the object inspector to set this. You probably also want to set Default to True. Now you can 'click' the OK button by pressing the Enter key on your keyboard!

In addition, if there is a Cancel button in the dialog, this should have ModalResult set to mrCancel and Cancel set to True. Now you can 'click' the Cancel button by pressing the Escape key on your keyboard!

A button with a ModalResult value will automatically close a modal dialog box.

污味仙女 2024-11-12 20:26:53

由于表单以模态方式显示,正确的解决方案是在按钮单击处理程序中设置 ModalResult := mrCancel 。快捷方式是将按钮的 ModalResult 属性设置为 mrCancel,然后您甚至不需要事件处理程序。


请注意,您的表单创建不正确。您将未分配的变量 Form2 作为 Owner 参数传递给构造函数。我预计这是访问冲突的原因。

例如,您应该传递另一个表单,Applicationnil。事实上,在这种情况下,您也可以传递 nil,以便代码应为:

Form2 := TForm2.Create(nil);
try
  Form2.ShowModal;
finally
  Form2.Free;
end;

如果您传递所有者,则当所有者被销毁时,表单将被销毁。由于您自己销毁它,因此不需要传递所有者。

也就是说,有时设置所有者很有用,例如,如果您使用 Position 属性值之一根据所有者的位置设置表单的位置。如果是这样,那么我建议在此实例中传递 Self ,它是一个 TForm1 对象引用。

Since the form is showing modally the correct solution is to set ModalResult := mrCancel in your button click handler. The shortcut is to set the ModalResult property of the button to mrCancel and then you don't even need the event handler.


Note that your form is being created incorrectly. You are passing the unassigned variable Form2 as the Owner parameter to the constructor. I expect this is the cause of the access violation.

You should pass another form, Application or nil, for example. In fact in this case you may as well pass nil so that the code should read:

Form2 := TForm2.Create(nil);
try
  Form2.ShowModal;
finally
  Form2.Free;
end;

If you pass an owner then the form will be destroyed when the owner is destroyed. Since you are destroying it yourself, you don't need to pass an owner.

That said, it is sometimes useful to set the owner, for example if you are using one of the Position property values that sets the form's position based on the owner's position. If so then I recommend passing Self in this instance which is a TForm1 object reference.

野却迷人 2024-11-12 20:26:53

另外两个答案讨论了解决您问题的其他方法。

我将指出问题的原因。

您有两个名为 Form2 的变量。一个包含表单,另一个未初始化,很可能是 Nil。 A/V 的原因是您访问了 Nil 变量。

在类中使用时,您应该避免使用类的变量名称,而是可以直接引用它的成员,例如调用 Close; 而不引用变量。为了保持清晰,您还可以在其前面添加 self. 前缀,因此将 form2.close; 替换为 self.close;

The other two answers discuss other ways to solve your problem.

I am going to point out the cause of the problem.

You have two variables named Form2. One contains the form and the other is uninitialized most likely Nil. The cause of the A/V is because your accessing the Nil variable.

When working with in a class you should avoid using the variable name of the class instead you can reference it's members directly such as calling Close; without referencing the variable. To keep things clear you can also prefix it with self. so replace form2.close; with self.close;

初相遇 2024-11-12 20:26:53

在第二个代码中,您已经处于表单 2 中,因此您只需要执行“

procedure TForm2.cancelBtnClick(Sender: TObject);
begin
  Close;
end;

如果在第一个代码中创建了一个变量,则您没有使用作为表单中的公共变量创建的通用 Form2 变量,则它不会分配给您所调用的表格。所以会报错。如果在单元顶部,您删除了“var Form2: TForm2;”行,你会看到它抱怨该行缺少变量,将其更改为关闭,它就会消失,你的错误也会消失。

In your second code, you're already in form 2, so you would only need to do

procedure TForm2.cancelBtnClick(Sender: TObject);
begin
  Close;
end;

If in your first code you created a variable you didnt use the generic Form2 variable made as a public variable in the form, its not then allocated to the form you called. So it will give errors. If at the top of the unit, you removed the "var Form2: TForm2;" line, you'd see it complain about the lack of variable for that line, change it to close, it will go away and so will your error.

寒冷纷飞旳雪 2024-11-12 20:26:53

我不知道您是否已经解决了该问题或创建了新代码来避免该问题,但我认为正在发生的情况(并且您没有显示所有代码来确认)是您在 OnActivate main 上创建了错误的代码形成事件。

当您退出第二个窗体时,您将返回主窗体并且它再次“激活”。
可能您操纵了一个不再存在的对象。

I don't know if you already resolved that question or created a new code to avoid the problem, but what I think is happening (and you didn't showed all your code to confirm) is that you created bad code on the OnActivate main form event.

When you exit the second form, you return to your mainform and it "actvates" again.
Probably you manipulated an object that do not exists anymore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文