2017-08-23 52 views
1

我想知道模块中'Handles Me.FormClosing'的任何替代方案。模块中'Handles Me.FormClosing'的替代方案

我创建的代码在点击'X'按钮后会显示一条确认消息。问题是,我需要将此代码放入一个模块中,以便在可以调用它的多个表单上使用,但是,当我尝试执行此操作时,'Handles Me.FormClosing'将不起作用。

这里是我使用的代码:

Private Sub Close(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing 

    Dim result As DialogResult = MessageBox.Show("Are you sure you want to Exit the application?", "Exit", MessageBoxButtons.YesNo) 

    If result = DialogResult.Yes Then 
     FrmLogin.Close() 
    Else 
     e.Cancel = True 
    End If 

End Sub 
+0

使用[AddHandler](https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/addhandler-statement) – Pikoh

回答

7

只要你创建一个新的形式:

Dim newForm as Form = New YourFormClass() 
AddHandler newForm.FormClosing, AddressOf YourModule.Close 

这将让您透过该子希望所有关闭的事件。然后,只要删除Handles Me.Closing,除非您没有向我们显示相关信息。

+0

对不起,我有点新来编码。我应该在哪里输入? – SwiftWombat

+0

无论你在哪里创建一个表单,你想把它添加到 –

0

如果单击“是”,则显示MessageBox并退出应用程序的模块中可能会创建一个函数,否则返回False

Module YourModule 

    Private dontAskAgain As Boolean 
    Public Function AskFormClosing() As Boolean 
     If dontAskAgain = False Then 
      Dim result As DialogResult = MessageBox.Show("Are you sure you want to Exit the application?", "Exit", MessageBoxButtons.YesNo) 

      If result = DialogResult.Yes Then 
       dontAskAgain = True 
       Application.Exit() 
      End If 
     End If 

     Return dontAskAgain 
    End Function 

End Module 

然后你只需要设置e.Cancel给函数的求反结果。

Private Sub Close(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing 
    e.Cancel = Not YourModule.AskFormClosing 
End Sub 

如果你喜欢使用的AddHandler其他人的建议,你可以用下面的方法,这将导致同样的结果。

Module YourModule 

    Public Sub AskFormClosing(sender As Object, e As FormClosingEventArgs) 
     If dontAskAgain = False Then 
      Dim result As DialogResult = MessageBox.Show("Are you sure you want to Exit the application?", "Exit", MessageBoxButtons.YesNo) 

      If result = DialogResult.Yes Then 
       dontAskAgain = True 
       Application.Exit() 
      Else 
       e.Cancel = True 
      End If 
     End If 
    End Sub 

End Module 

然后添加处理程序是这样的:

Dim newForm as Form = New YourFormClass() 
AddHandler newForm.FormClosing, AddressOf YourModule.AskFormClosing 

但你的主表格,您将需要添加的处理程序中加载事件,像这样:

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    AddHandler Me.FormClosing, AddressOf YourModule.AskFormClosing 
End Sub 
+1

这个代码的确会使它弹出消息框,这很棒,但是当点击yes时程序不会关闭,只有当前表单 – SwiftWombat

+0

你写道:“我需要把这个代码放到一个模块中用于多种形式”,而不是关闭所有其他形式的整个应用程序。为了简单起见,只需在'Return True'前面加上'End',它就可以做你想做的事。但这更多是一种“残酷”的方式。 – MatSnow

+0

谢谢。对不起,没有正确地说出问题。你是一堆帮助 – SwiftWombat

0

尝试这个。我所做的更改工作,只要你想:

Module YourModule 

    Public Function AskFormClosing() As Boolean 
     Dim result As DialogResult = MessageBox.Show("Are you sure you want to Exit the application?", "Exit", MessageBoxButtons.YesNo) 

     If result = DialogResult.Yes Then 
      Return True 
     Else 
      Return False 
     End If 
    End Function 
End Module 

然后

Private Sub Close(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing 
    If YourModule.AskFormClosing Then 
     Application.Exit 
    Else 
     e.Cancel = True 
    End If 
End Sub 
+0

这样,如果按MessageBox上的“否”,它将不会取消关闭。 – MatSnow

+0

我刚刚编辑了答案和方法Close –

+0

这将使MessageBox弹出两次。顺便说一句,它应该是'e.Cancel = True'。 – MatSnow

0

而不是将这个代码在模块重用,考虑创建一个具有您希望的行为的基础Form分享,然后让你从其他形式继承从这个基地Form(而不是从System.Windows.Forms.Form)。

请参阅Microsoft网站上的How to: Inherit Windows Forms