【问题标题】:How to automatically close the dialog box?如何自动关闭对话框?
【发布时间】:2016-04-27 13:23:36
【问题描述】:

我有一个 WinForm 应用程序,它会在一段时间后自动注销,用户没有执行任何活动,该应用程序有一些对话框,如

SearchCustomer.ShowDialog()

如果用户打开该对话框并且时间到期,则用户会自动注销,但该对话框仍保持打开状态,因此即使用户已注销,任何人都可以使用该对话框。

有什么方法可以从主窗体关闭这些对话框?

编辑: 以不同的方式打开了另一个对话框

AddCustomer.Show()
AddCustomer.BringToFront()

编辑 2 基于 jmcilhinney 答案的解决方案

For Each openForm In openForms

    Dim H1 As Integer = openForm.GetHashCode()
    Dim H2 As Integer = Me.GetHashCode()
    If H1 <> H2 Then 'No igual
       openForm.Close()
    End If
Next

【问题讨论】:

  • 只是这个对话框还是其他对话框有类似情况?
  • SearchCustomer.Close 将关闭该表单
  • @Steve 是的,还有另一个以不同的方式打开,我将代码添加到主要问题中,请重新阅读,谢谢。
  • @MattWilko 我可以从主表单的任何部分执行该指令吗?就我现在而言,可能我错了,主窗体的代码执行在 ShowDialog() 处停止,直到对话框关闭;我错了吗?

标签: vb.net winforms dialog


【解决方案1】:

我还没有测试过,但我认为您应该能够按照以下方式做一些事情:

Dim openForms = My.Application.OpenForms.Cast(Of Form)().ToArray()

For Each openForm In openForms
    openForm.Close()
Next

【讨论】:

  • 它会关闭对话框,同时也会关闭主窗体。
  • 所以,在循环内部,测试openForm是否是主窗体,如果不是则关闭它。
  • 是的,我正试图弄清楚如何。我正在寻找一些有助于识别主表单的属性,我可以制作类似 If Not openForm = Me 的东西吗?
  • 我想我明白了!我只是使用 hashcode 属性。
  • 我认为您可以只使用该类型,除非您在某个时间打开了该类型表单的多个实例。例如。 If Not TypeOf openForm Is Form1 Then.
【解决方案2】:

这是一个选项。

使用所有权属性打开您的表单。

    AddCustomer.Show(Me)
    'This open the form and gives the referring form ownership.
    'It Also gives focus to the child form, keeping it on top of the referring form

当引用表单关闭时,子表单也会关闭。

【讨论】:

  • 有趣但不是我需要的。我需要主表单保持打开状态,并在用户注销时关闭所有子表单。无论如何,我已经解决了这个问题。无论如何,谢谢。
猜你喜欢
  • 2013-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
  • 1970-01-01
  • 1970-01-01
  • 2012-11-14
相关资源
最近更新 更多