VB.net 开闭表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18286432/
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
VB.net opening and closing forms
提问by Marc Intes
I have a VB program that has two forms, i have coded the form load of each forms.
我有一个 VB 程序,它有两种形式,我对每种形式的形式加载进行了编码。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("I AM FORM 1")
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("I AM FORM 2")
End Sub
Here is how i switch through Form1 and Form2, i made use of a button.
这是我如何切换 Form1 和 Form2,我使用了一个按钮。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form2.Show()
End Sub
But everytime i switch forms the form load event will only trigger once. Is there something wrong with my code? I am guesing the Me.Hide() will only hide the previous form and not totally close it. I want to be able to close the previous form so that when i will open it again, the form load event will trigger again.
但是每次我切换表单时,表单加载事件只会触发一次。我的代码有问题吗?我猜 Me.Hide() 只会隐藏以前的表单而不是完全关闭它。我希望能够关闭以前的表单,以便当我再次打开它时,表单加载事件将再次触发。
回答by Cody Gray
But everytime i switch forms the form load event will only trigger once. Is there something wrong with my code? I am guesing the Me.Hide() will only hide the previous form and not totally close it.
但是每次我切换表单时,表单加载事件只会触发一次。我的代码有问题吗?我猜 Me.Hide() 只会隐藏以前的表单而不是完全关闭它。
This is exactly what is happening. The Hide
method just hidesthe form from the user, effectively making it invisible.
这正是正在发生的事情。该Hide
方法只是对用户隐藏表单,有效地使其不可见。
What you're looking for is the Close
method, which actually closes the form. (Since you are displaying the form using the Show
method, you do not need to call Dispose
.)
您要查找的是Close
方法,这实际上关闭表单。(由于您使用Show
方法显示表单,因此不需要调用Dispose
。)
You will, however, not be able to close a form and continue to run code in its methods. So you'll need to reverse the order of the statements in your event handler functions, displaying the other form firstand then closing itself. Make them look like this:
但是,您将无法关闭表单并继续在其方法中运行代码。所以你需要扭转在事件处理函数的语句的顺序,显示其他形式的第一,然后关闭本身。让它们看起来像这样:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.Show()
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
Me.Close()
End Sub
That will do what you want. The Load
event will be triggered each time you call the Show
method, because you're creating and showing a new form.
那会做你想做的。Load
每次调用该Show
方法时都会触发该事件,因为您正在创建并显示一个新表单。
It is worth pointing out, though, that you're relying on an unusual characteristic of VB.NET, one that it retains from the older VB languages for backwards compatibility reasons. Instead of referring to an objectof your form class (like you would have to do with all other class objects), you are referring to it by the typename (the name of the class itself). You really shouldn't do that, it causes all sorts of headaches and will confuse people reading your code. It is better to just instantiate a new form object, like this:
不过,值得指出的是,您依赖于 VB.NET 的一个不寻常的特性,出于向后兼容性的原因,它保留了旧 VB 语言的特性。不是引用表单类的对象(就像您必须处理所有其他类对象一样),而是通过类型名称(类本身的名称)引用它。你真的不应该这样做,它会引起各种各样的头痛,并且会让阅读你的代码的人感到困惑。最好只实例化一个新的表单对象,如下所示:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frm As New Form1 ' create a new Form1 object
frm.Show() ' ... and display it
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frm As New Form1 ' create a new Form2 object
frm.Show() ' ... and display it
Me.Close()
End Sub
When you run this code, you will likely run immediately into another problem: the first time you close Form1
, your entire application will quit. This is because, by default for a new project, Form1
is designated as the "Startup form" in your project's properties ("My Project" in the Solution Explorer). You will either have to:
当您运行此代码时,您可能会立即遇到另一个问题:第一次关闭时Form1
,您的整个应用程序将退出。这是因为,默认情况下,新项目Form1
在项目属性中被指定为“启动表单”(解决方案资源管理器中的“我的项目”)。您要么必须:
- create a thirdform to use as the "main" form, and set the "Startup form" to this third form, or
- change the "Shutdown mode" (also in "My Project") from "When startup form closes" to "When last form closes".
- 创建第三个表单用作“主”表单,并将“启动表单”设置为第三个表单,或
- 将“关闭模式”(也在“我的项目”中)从“启动表单关闭时”更改为“最后一个表单关闭时”。
回答by Hans Passant
I am guesing the Me.Hide() will only hide the previous form and not totally close it
我猜 Me.Hide() 只会隐藏以前的表单而不是完全关闭它
Yes, it does what it says. If you want to close the form then use Me.Close() instead. The Load event will fire again when you create the new instance.
是的,它按照它说的做。如果要关闭表单,请改用 Me.Close()。当您创建新实例时,Load 事件将再次触发。
You'll have to change a setting to ensure that doesn't also close your application. Project + Properties, Application tab, change the Shutdown mode setting to "When last form closes". And put the Me.Close() call afterthe Show() call.
您必须更改设置以确保它不会同时关闭您的应用程序。项目 + 属性,应用程序选项卡,将关闭模式设置更改为“最后一个表单关闭时”。并把Me.Close()调用后显示()调用。
回答by Jade Mendoza
If MessageBox.Show("Are you sure to close this application?", "Close",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
frmIndex.Show() //the main form
Else
e.Cancel = True
Me.Show() // The form open
End If
The form open going closing and going back to the main/index form. hope it help :) just play with the .show
, .hide and e.cancel
表单打开关闭并返回主/索引表单。希望它有帮助:) 只玩.show
, .hide 和 e.cancel
回答by Manny265
I also had a similar question. When u .Hide()
you are just storing it away in memory somewhere such that when it is re-opened it doesnt have to make a new form just recalls the one from memory hence that method is not called again. You have to destroy the form. So what you can do when navigating to another form is go to that form first and then destroy the current form like so Form2.Show()
Me.Close()
. Look at my question and my accepted answer. If that works please dont forget to tick this as your accepted answer.
When my form is hidden and reloaded from another form it is not executing the code in the Load event
我也有类似的问题。当.Hide()
你只是将它存储在内存中的某个地方时,当它重新打开时,它不必创建一个新表单,只需从内存中调用一个,因此不会再次调用该方法。你必须破坏表格。因此,当导航到另一个表单时,您可以先转到该表单,然后像这样销毁当前表单Form2.Show()
Me.Close()
。看看我的问题和我接受的答案。如果可行,请不要忘记将此作为您接受的答案。
当我的表单被隐藏并从另一个表单重新加载时,它不会执行 Load 事件中的代码
回答by John Bush
Yes. What you are doing is closing the form before it could open up form2.
是的。您正在做的是在打开 form2 之前关闭表单。
Instead Of:
代替:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form2.Show()
End Sub
You Need To Put:
你需要把:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.show
Me.hide
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.show
Me.hide
End Sub
If This Helps, Please Reply.
如果这有帮助,请回复。
回答by Lectere
I think you using a silly construction, but you should;
我认为你使用了一个愚蠢的结构,但你应该;
Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Form1.close()
End Sub
Use the Shown event.
使用 Shown 事件。
And use ShowDialog()
并使用 ShowDialog()
Form1.ShowDialog()