[英]How to close an open form in Visual Basic using a button?
I am working on a game, and it has a simple menu system.我正在开发一款游戏,它有一个简单的菜单系统。 One of the buttons, simply known as 'Button1', should close the menu form and load up the game window.
其中一个按钮,简称为“Button1”,应关闭菜单窗体并加载游戏 window。 While it opens the game window, it does not close the menu window.
当它打开游戏 window 时,它不会关闭菜单 window。 Is there a way to make it so that it closes one form and opens another.
有没有办法让它关闭一个表格并打开另一个表格。
Game window name = GameBoard游戏 window 名称 = GameBoard
Menu window name = Form2菜单 window 名称 = Form2
Code:代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Form2 As New GameBoard
Form2.ShowDialog()
Me.Close()
End Sub
The reason that the form is not closed is because Form2
is opened with ShowDialog()
:表单没有关闭的原因是因为
Form2
是用ShowDialog()
打开的:
Form2.ShowDialog()
' This will not execute until Form2 is closed
Me.Close()
This will pause execution of the code in Button1_Click
until after Form2
is closed, as per the docs for ShowDialog()
:根据
ShowDialog()
的文档,这将暂停Button1_Click
中代码的执行,直到Form2
关闭后:
Shows the form as a modal dialog box.
将表单显示为模式对话框。
You could, alternatively, just do Form2.Show()
, which would not open it as a dialog and allow the remainder of your code to run:或者,您可以只执行
Form2.Show()
,它不会将其作为对话框打开并允许其余代码运行:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Form2 As New GameBoard
Form2.Show()
Me.Close()
End Sub
Alternatively, close the current form before opening Form2
:或者,在打开
Form2
之前关闭当前表单:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Form2 As New GameBoard
Me.Close()
Form2.ShowDialog()
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.