如何在不从.ShowDialog返回的情况下隐藏模式对话框? [英] How to hide a modal dialog without returning from .ShowDialog?

查看:104
本文介绍了如何在不从.ShowDialog返回的情况下隐藏模式对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在vb.net中有一个以子功能开头的应用程序,它会做一些事情并决定它是否显示自身.当它显示自己时,可以通过调用dialog.ShowDialog()来实现.

I have an application in vb.net that starts with a sub function do some things and decide if it shows itself or not. When it shows itself it does so by invoking dialog.ShowDialog().

dialog.ShowDialog()返回时,应用程序将进行一些清理并结束.

When dialog.ShowDialog() returns, the application does some cleaning and ends.

我想找到一种方法来临时隐藏对话框(将其发送到系统托盘),而无需从ShowDialog()函数返回.但是,一旦我在窗体的代码中执行me.Hide(),窗体就会被有效地隐藏,但是ShowDialog()函数会返回,并且进程将关闭.

I'd like to find a way to temporarily hide the dialog (send it to the system tray) without returning from the ShowDialog() function. However, as soon as I do a me.Hide() in the form's code, the form is effectively hidden, but the ShowDialog() function returns and the process is closed.

我了解这是预期的行为.所以我的问题是如何获得这种效果?那就是启动一个对话框,该对话框可以隐藏,并阻止直到用户真正想要退出该应用程序为止.

I understand this is the expected behavior. So my question is how can I get this effect? That is launch a dialog, that can be hidden, and block until the user really wants to quit the application.

推荐答案

您无法进行这项工作,隐藏窗体时,ShowDialog()将始终返回.诀窍是使用常规形式和对Application.Run()的常规调用,但要防止立即显示它.将此代码粘贴到您的表单类中:

You cannot make this work, ShowDialog() will always return when the form is hidden. The trick is to use a regular form and a normal call to Application.Run() but to prevent it from becoming visible immediately. Paste this code into your form class:

Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
    If Not IsHandleCreated Then
        CreateHandle()
        value = false
    End If
    MyBase.SetVisibleCore(value)
End Sub

请注意,Load事件处理程序要等到窗体真正可见后才能运行,因此请确保在Sub New构造函数中进行任何初始化.

Beware that your Load event handler won't run until the form actually becomes visible so be sure to do any initialization in the Sub New constructor.

这篇关于如何在不从.ShowDialog返回的情况下隐藏模式对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆