打开和关闭vcl窗体
现在我有两种形式。在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
执行此操作的正常方法是
,如果
TForm2
包含“确定”按钮,则应在设计时将ModalResult
属性设置为mrOK
-时间。使用对象检查器来设置它。您可能还想将Default
设置为True
。现在您可以通过按键盘上的 Enter 键来“单击”确定按钮!此外,如果对话框中有“取消”按钮,则应将
ModalResult
设置为mrCancel
并将Cancel
设置为True< /代码>。现在您可以通过按键盘上的 Escape 键来“单击”取消按钮!
具有
ModalResult
值的按钮将自动关闭模式对话框。The normal way to do this is to do
and, if
TForm2
contains a OK button, this should have theModalResult
property set tomrOK
at design-time. Use the object inspector to set this. You probably also want to setDefault
toTrue
. 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 tomrCancel
andCancel
set toTrue
. 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.由于表单以模态方式显示,正确的解决方案是在按钮单击处理程序中设置
ModalResult := mrCancel
。快捷方式是将按钮的ModalResult
属性设置为mrCancel
,然后您甚至不需要事件处理程序。请注意,您的表单创建不正确。您将未分配的变量
Form2
作为Owner
参数传递给构造函数。我预计这是访问冲突的原因。例如,您应该传递另一个表单,
Application
或nil
。事实上,在这种情况下,您也可以传递 nil,以便代码应为:如果您传递所有者,则当所有者被销毁时,表单将被销毁。由于您自己销毁它,因此不需要传递所有者。
也就是说,有时设置所有者很有用,例如,如果您使用
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 theModalResult
property of the button tomrCancel
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 theOwner
parameter to the constructor. I expect this is the cause of the access violation.You should pass another form,
Application
ornil
, for example. In fact in this case you may as well pass nil so that the code should read: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 passingSelf
in this instance which is aTForm1
object reference.另外两个答案讨论了解决您问题的其他方法。
我将指出问题的原因。
您有两个名为 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 withself.
so replaceform2.close;
withself.close;
在第二个代码中,您已经处于表单 2 中,因此您只需要执行“
如果在第一个代码中创建了一个变量,则您没有使用作为表单中的公共变量创建的通用 Form2 变量,则它不会分配给您所调用的表格。所以会报错。如果在单元顶部,您删除了“var Form2: TForm2;”行,你会看到它抱怨该行缺少变量,将其更改为关闭,它就会消失,你的错误也会消失。
In your second code, you're already in form 2, so you would only need to do
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.
我不知道您是否已经解决了该问题或创建了新代码来避免该问题,但我认为正在发生的情况(并且您没有显示所有代码来确认)是您在 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.