vb.net代码关闭另一个窗体(如果打开)

您所在的位置:网站首页 vb关闭窗口的代码 vb.net代码关闭另一个窗体(如果打开)

vb.net代码关闭另一个窗体(如果打开)

2023-08-04 02:48| 来源: 网络整理| 查看: 265

这很简单,但是我不知道。

如果form1仍处于打开状态,我想以编程方式在form2关闭时关闭form1。 Form2是通过form1上的命令按钮打开的。

在Form1上打开Form2的代码是:

12Dim frm As New form2 frm.Show()

关闭Form2来关闭所有打开的Form1的打开副本的最佳方法是什么?

相关讨论 只需为FormClosed事件添加一个事件处理程序,然后调用Me.Close() 我正在尝试向form2添加一个事件处理程序,因此当它关闭时,它会关闭form1(如果form1是打开的)。 Me.Close()将关闭form2,不是吗?我如何关闭form1? 不,该代码属于Form1类。很难理解为什么这是一个问题,也许您还不知道AddHandler语句。将AddHandler frm.FormClosed,AddressOf(yadayada)作为您发布的代码段中的第二条语句。并添加yadayada方法,它所要做的就是Me.Close()

添加此引用以使其起作用。

12345Imports System.Linq If Application.OpenForms().OfType(Of Form1).Any Then    MsgBox("Form1 is open") End If

如果要独立处理两种形式,则需要从第三种形式或类中进行监视。因此,我的建议是在第三类中创建它们两者,并将第二个表单的引用传递给第一个表单,以便随后将其打开。这样:

12345678910111213141516171819202122232425262728293031323334353637383940414243Public Class MyHelper     Public Sub CreateForms()         Dim form2 as New Form2()         AddHandler form2.Closed, AddressOf Form2_OnClosed         ‘ Create as many copies as you need         Dim form1 as New Form1(form2)         form1.Show()     End Sub     Protected Sub Form2_OnClosed(sender as object, e as EventArgs)         ‘ Same code for each form1 that has been created and opened.         If (form1.IsOpen) Then form1.Close()     End Sub End Class Public Class Form1     Private _form2 as Form2     Public Property IsOpen as Boolean = false     Public Sub New(form2 as Form2)         _form2 = form2     End Sub     Protected Sub MyButton_Click(sender as object, e as EventArgs) handles MyButton.Click         ‘ You open your form here or wherever you want (even on the constructor)         _form2.Show()     End Sub     Protected Sub Me_OnClosed(sender as object, e as EventArgs) handles Me.Closed         Me.IsOpen = false     End Sub     Protected Sub Me_OnShown(sender as object, e as EventArgs) handles Me.Shown         Me.IsOpen = true     End Sub End Class 相关讨论 那么,如何确定表单是否打开?如果用户在打开form2之后可能已经关闭了该怎么办?

假设您有3个表单,并且要关闭其他两个表单,请点击

123456789Private Sub EMPLEADOToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EMPLEADOToolStripMenuItem.Click     If Application.OpenForms().OfType(Of BUSCAR_INDEX).Any Then         BUSCAR_INDEX.Close()     ElseIf Application.OpenForms().OfType(Of MIEMBROS_INDEX).Any Then         MIEMBROS_INDEX.Close()     End If     EMP_INDEX.Show()     EMP_INDEX.EmpIDTextBox.Text = EmpIDTextBox.Text End Sub


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3