如何从VB.net中的子窗体获得父窗体?

  • 本文关键字:窗体 VB net vb.net forms parent
  • 更新时间 :
  • 英文 :


我有一个VB的形式,可以由一些形式调用。退出此子表单后,我想恢复调用表单,但我似乎不知道如何确定哪个表单调用它。如有任何帮助,我将不胜感激。

您可以为父表单创建一个标签(可能是一个全局数组),您将在每次打开子表单时更新该值。你只需要记住你要做的标签上的形式。

也许你可以试试这个-
模块与表单标签:


Module Module1
Public parentForms(2) As String
'parentForms(0) - parent form of Form 1
'parentForms(1) - parent form of Form 2
'parentForms(2) - parent form of Form 3
Public Sub openParentForm(ByVal ParentTag As String)
If ParentTag {use the notequal operator} "" Then
Select Case ParentTag
Case Form1.Tag
Form1.Show()
Case Form2.Tag
Form2.Show()
Case Form3.Tag
Form3.Show()
End Select
End If
End Sub
End Module

Form1:

Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Tag = "Form1"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
parentForms(1) = Me.Tag
Form2.Show()
'Dont use .Close as the Parent Form will be disposed and the whole application will exit
Me.Visible = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
parentForms(2) = Me.Tag
Form3.Show()
Me.Visible = False
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Call openParentForm(parentForms(0))
End Sub

结束课

Form2


Public Class Form2
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Tag = "Form2"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
parentForms(0) = Me.Tag
Form1.Show()
Me.Visible = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
parentForms(2) = Me.Tag
Form3.Show()
Me.Visible = False
End Sub
Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Call openParentForm(parentForms(1))
End Sub
End Class

Form3


Public Class Form3
Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Tag = "Form3"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
parentForms(0) = Me.Tag
Form1.Show()
Me.Visible = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
parentForms(1) = Me.Tag
Form2.Show()
Me.Visible = False
End Sub
Private Sub Form3_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Call openParentForm(parentForms(2))
End Sub
End Class

最新更新