VB.net Framework 3.5 OnClose()或OnDestroy()

时间:2014-01-03 20:21:21

标签: vb.net .net-3.5

我道歉,因为我相信这个问题非常基本。我希望在关闭时对表单进行一些清理。其他语言有OnClose()OnDestroy()方法。 VB.net使用.NET 3.5框架的等价物是什么?

1 个答案:

答案 0 :(得分:1)

System.Windows.Forms.Form 实现了IDisposable接口。像任何实现IDisposable的对象一样,清理代码最好放在Dispose事件或例程中。

请参阅以下示例,其中显示了以下事件处理程序:

  • 事件Form.Disposed
  • 事件Form.FormClosed
  • 事件Form.FormClosing

    Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
        REM     we are loaded... probably  =)
    
    End Sub
    
    Private Sub Form1_Disposed(sender As Object, e As System.EventArgs) Handles Me.Disposed
    
        REM     Put cleanup code here...
    
        REM     when we are done here the form will be completly disposed of
    
    End Sub
    
    Private Sub Form1_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    
        REM     Here the form is CLOSED but not disposed YET!
    
    End Sub
    
    
    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    
        REM     Here the form is CLosing, but has not closed yet, and the closing behavior itself can actually be aborted:
    
        REM     To cancel the closing of a form you can:
    
        ' e.Cancel = True 
    
    End Sub
    
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Me.Dispose()
    End Sub
    

    结束班

结果你也可以通过使用声明处理: 使用关键字可用于实现IDisposable接口的所有对象。

 using frm as new form() ' A new form is created here
     frm.ShowDialog()  ' form is displayed as a Modal form (  dialog ) 
 end using             ' form is Disposed() of here.