VB.net 开闭表格 [英] VB.net opening and closing forms

查看:25
本文介绍了VB.net 开闭表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有两种表单的 VB 程序,我已经对每个表单的表单加载进行了编码.

I have a VB program that has two forms, i have coded the form load of each forms.

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

这里是我如何在 Form1 和 Form2 之间切换,我使用了一个按钮.

Here is how i switch through Form1 and Form2, i made use of a button.

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

但是每次我切换表单时,表单加载事件只会触发一次.我的代码有问题吗?我猜 Me.Hide() 只会隐藏以前的表单而不是完全关闭它.我希望能够关闭之前的表单,这样当我再次打开它时,表单加载事件会再次触发.

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() 只会隐藏以前的表单而不是完全关闭它.

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.

这正是正在发生的事情.Hide 方法只是对用户隐藏表单,有效地使其不可见.

This is exactly what is happening. The Hide method just hides the form from the user, effectively making it invisible.

您正在寻找的是 Close 方法,实际上是关闭表单.(由于您使用 Show 方法显示表单,因此不需要调用 Dispose.)

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.)

但是,您将无法关闭表单并继续在其方法中运行代码.因此,您需要颠倒事件处理程序函数中语句的顺序,首先显示另一个表单,然后关闭自身.让它们看起来像这样:

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 first and 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

这会做你想做的.每次调用 Show 方法时都会触发 Load 事件,因为您正在创建和显示一个新表单.

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.

不过,值得指出的是,您依赖于 VB.NET 的一个不寻常的特性,出于向后兼容性的原因,它保留了旧 VB 语言的特性.不是引用表单类的对象(就像您必须处理所有其他类对象一样),而是通过类型名称(名称类本身).你真的不应该这样做,它会引起各种各样的头痛,并且会让阅读你的代码的人感到困惑.最好只是实例化一个新的表单对象,如下所示:

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 object of your form class (like you would have to do with all other class objects), you are referring to it by the type name (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:

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

当您运行此代码时,您可能会立即遇到另一个问题:第一次关闭 Form1 时,您的整个应用程序将退出.这是因为,默认情况下,对于新项目,Form1 被指定为项目属性中的启动表单"(解决方案资源管理器中的我的项目").您要么必须:

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:

  • 创建一个第三个​​表单作为主"表单,并将启动表单"设置为第三个表单,或者
  • 将关闭模式"(也在我的项目"中)从启动表单关闭时"更改为最后一个表单关闭时".
  • create a third form 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".

这篇关于VB.net 开闭表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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