首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模块中“Handles Me.FormClosing”的替代方案

模块中“Handles Me.FormClosing”的替代方案
EN

Stack Overflow用户
提问于 2017-08-23 19:01:54
回答 4查看 162关注 0票数 1

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

我已经创建了在单击'X‘按钮时显示确认消息的代码。问题是,我需要将这段代码放到一个模块中,以便在可以调用它的多个窗体上使用,但是,当我尝试这样做时,“Handles Me.FormClosing”将不起作用。

下面是我使用的代码:

代码语言:javascript
复制
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
EN

回答 4

Stack Overflow用户

发布于 2017-08-23 19:05:33

无论何时创建新表单:

代码语言:javascript
复制
Dim newForm as Form = New YourFormClass()
AddHandler newForm.FormClosing, AddressOf YourModule.Close

这将路由所有您想要的关闭事件通过该潜艇。然后删除Handles Me.Closing,除非您没有向我们展示与之相关的内容。

票数 7
EN

Stack Overflow用户

发布于 2017-08-23 19:17:56

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

代码语言:javascript
复制
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设置为函数的反转结果。

代码语言:javascript
复制
Private Sub Close(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    e.Cancel = Not YourModule.AskFormClosing
End Sub

如果你更喜欢按照别人的建议使用AddHandler,你可以使用下面的方法,也会得到同样的结果。

代码语言:javascript
复制
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

然后添加Handler,如下所示:

代码语言:javascript
复制
Dim newForm as Form = New YourFormClass()
AddHandler newForm.FormClosing, AddressOf YourModule.AskFormClosing

但对于main-Form,您需要在加载事件中添加处理程序,如下所示:

代码语言:javascript
复制
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    AddHandler Me.FormClosing, AddressOf YourModule.AskFormClosing
End Sub
票数 0
EN

Stack Overflow用户

发布于 2017-08-23 19:49:40

尝尝这个。我按照你的要求做了一些改动:

代码语言:javascript
复制
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

然后

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45837894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档