简体   繁体   English

如何使用按钮关闭 Visual Basic 中打开的表单?

[英]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.

相关问题 Visual Basic表单应用程序自定义关闭按钮 - Visual Basic Form Application custom close button Visual Basic形式.close()方法 - Visual Basic form .close() method 我需要从另一个未关闭第一个窗体的窗体中关闭一个窗体。 (基本视觉) - I need to close a form from another form, which did not open the first form. (visual basic) 如何通过在Visual Basic中按另一个窗体上的按钮来按窗体上的按钮 - How to press a button on a form by pressing a button on another form in Visual basic 如何重新打开具有相同数据的隐藏窗体 Visual Basic - How to re-open a hidden form with the same data Visual Basic 如何使用 Visual Basic 和 mvc 核心从按钮调用 function - How to call function from button using visual basic and mvc core 在提示后单击“关闭”按钮时终止程序-Visual Basic - Terminate program when Close button is Clicked after Prompt - Visual Basic 使用 Visual Basic 打开并登录到另一个程序 - Using Visual Basic to open and log into another program 在visual basic中双击DataGridView上的特定单元格时如何打开表单? - How to open form when a specific cell on DataGridView double-clicked in visual basic? 在Visual Basic 2010中使用OpenTextFileReader(path)后如何关闭文档 - How do I close a document after using OpenTextFileReader(path) in Visual Basic 2010
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM