无法在 vb.net 中的项目/表单之间切换 [英] Trouble switching between projects/forms in vb.net
问题描述
我正在使用 VB.Net 在 Visual Studio Community 2019 中处理一个项目.我正在开发一个用户界面,其中用户从一个表单(一种索引,称之为表单 1)开始,然后从他们想要访问的下一个表单中选择(比如表单 2).这部分工作正常.麻烦的是,在用户完成表格 2 后,他们应该被引导回到表格 1 以从这里继续,而我无法让这种情况发生.我需要以某种方式将主菜单设为公共共享表单,以便其他项目可以访问它,但我不知道该怎么做.
I am working on a project in Visual Studio Community 2019 using VB.Net. I am developing a user interface wherein users start on one form (a sort of index, call it form 1) and select from their the next form they want to go to (say form 2). This part is working fine. The trouble is that after users finish in form 2, they should be directed back to form 1 to continue from here, and I can't get this to happen. I need somehow to make the main menu a public shared form so that the other projects can access it but I can’t figure out how to do that.
以下是表格 1 中的相关行:
Here are the relevant lines from form 1:
Public Class frmMainMenu
Public Shared inst As frmMainMenu
Private Sub frmMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
inst = Me
Timer1.Start()
Me.Show()
End Sub
Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles
btnGeology.Click
Dim formNew As New Geology.frmGeologyMenu
formNew.Show()
Timer1.Stop()
Me.Hide()
End Sub
这里是表单 2 中的相关行.我希望第 23 行和第 24 行将用户返回到主菜单,但它不起作用.
And here are the relevant lines from form 2. I want lines 23 and 24 to return the user to the main menu, but it doesn't work.
Public Class frmGeologyMenu
Public Sub frmGeologyMenu_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lblTime.Text = 10
Me.Show
End Sub
Private Sub BtnMainMenu_Click(sender As Object, e As EventArgs) Handles
btnMainMenu.Click
inst.frmMainMenu.Show()
inst.frmMainMenu.Timer1.Start()
Me.Close()
End Sub
感谢您提供的任何帮助!
I appreciate any help you can offer!
推荐答案
来自我上面的评论:
当您创建名为formNew"的第二个表单的实例时,使用 AddHandler 命令将其 FormClosed 事件连接到一个方法在主要形式.然后在第二种形式中,您可以调用 Me.Close()并且主窗体中的方法将在您可以调用的地方触发Me.Show()..
When you create your instance of your second form called "formNew", use the AddHandler command to wire up its FormClosed event to a method in the main form. Then in the second form you can just call Me.Close() and the method in the main form will fire where you can call Me.Show()..
在主菜单中:
Public Class frmMainMenu
Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles btnGeology.Click
Dim formNew As New Geology.frmGeologyMenu
AddHandler formNew.FormClosed, AddressOf formNew_FormClosed
Timer1.Stop()
Me.Hide()
formNew.Show()
End Sub
Private Sub formNew_FormClosed(sender As Object, e As FormClosedEventArgs)
Me.Show()
Timer1.Start()
End Sub
End Class
在 frmGeologyMenu 中:
In frmGeologyMenu:
Public Class frmGeologyMenu
Public Sub frmGeologyMenu_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lblTime.Text = 10
End Sub
Private Sub BtnMainMenu_Click(sender As Object, e As EventArgs) Handles btnMainMenu.Click
Me.Close()
End Sub
End Class
这篇关于无法在 vb.net 中的项目/表单之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!