vb.net BringToFront 并没有将形式带到前面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17948938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BringToFront isn't bringing form to the front
提问by Sean Long
I'm trying to set up a button that does the following:
我正在尝试设置一个执行以下操作的按钮:
- Checks to see if a form is open (and has lost focus). If so, it brings that form to the front.
- If not, it opens a new instance of the form.
- 检查表单是否已打开(并且已失去焦点)。如果是这样,它会将这种形式带到前面。
- 如果没有,它会打开一个新的表单实例。
However, I've tried a few different methods and it will always either create a new form (if I use frm_About.visible as the check) or simply not do anything (with the following code).
但是,我尝试了几种不同的方法,它总是会创建一个新表单(如果我使用 frm_About.visible 作为检查)或者根本不做任何事情(使用以下代码)。
Private Sub counter_aboutClick(sender As Object, e As EventArgs) Handles counter_About.Click
If Application.OpenForms().OfType(Of frm_About).Any Then
frm_About.BringToFront()
Else
Dim oAbout As frm_About
oAbout = New frm_About()
oAbout.Show()
oAbout = Nothing
End If
End Sub
I've heard that there's a bug with BringToFront in certain scenarios, am I hitting that bug?
我听说在某些情况下BringToFront 存在错误,我是否遇到了该错误?
回答by J...
VB.Net does a terrible thing and creates a default instance of a form (which can be referred to by its class name). This creates endless confusion and headaches - I suggest you read up on default instances (google can find a lot to read about, surely)
VB.Net 做了一件很糟糕的事情并创建了一个表单的默认实例(可以通过它的类名来引用)。这会造成无休止的混乱和头痛 - 我建议您阅读默认实例(谷歌可以找到很多可以阅读的内容,当然)
In this case, you have a class called frm_Aboutas well as a default instance of that form which is also called frm_About. If you've created a new form of type frm_Aboutthen the following code
在这种情况下,您有一个名为 的类frm_About以及该表单的默认实例,该实例也称为frm_About。如果你已经创建了一个新的表单类型,frm_About那么下面的代码
If Application.OpenForms().OfType(Of frm_About).Any Then
frm_About.BringToFront()
will search your open forms to look for a form of type frm_Aboutand, if it finds one, will attempt to bring the default instance of frm_Aboutto the front - note that the open form can be (an in your case is most likely) not the default instance but any instance created with New frm_About().
将搜索您打开的表单以查找一种类型的表单frm_About,如果找到,将尝试将默认实例frm_About放在前面 - 请注意,打开的表单可以(在您的情况下很可能)不是默认的实例,但任何使用New frm_About().
To find the actual instance of the form you would have to do something like :
要找到表单的实际实例,您必须执行以下操作:
For Each openForm In Application.OpenForms()
If TypeOf (openForm) Is frm_About Then _
CType(openForm, frm_About).BringToFront()
Next

