FormClosing事件不能正常工作 [英] FormClosing event doesn't work as I want

查看:173
本文介绍了FormClosing事件不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们!

首先,我读过其他类似的帖子,但我还没有看到完全匹配。



它是我的VB.NET应用程序。我希望用户在用户通过单击右上角的X关闭表单/应用程序时注销(以防止用户显示在线而不是)。



现在,我为LoggOff创建了一个子程序,将它放在一个模块中,并在需要时随时调用它。所以我去了属性并双击Form Closing事件来编写FormClosing的代码。在FormClose内部我称之为上面解释的LogOff子,当然,当用户点击顶部的大关闭'X'时,他/她被注销。但是有一个问题;



第一个要加载的表单是登录表单,之后会打开另一个表单,说欢迎将被打开,登录将被关闭(通过welcome.open,me.close),现在问题是的,每当执行me.close时,FormClosing事件中的所有代码也会运行,这意味着终止应用程序。



每当我只想关闭一个表单时并打开另一个(我不想只隐藏表单)代码将关闭用户并关闭应用程序。我怎样才能区分用户点击大X和关闭程序员与me.close ???



非常感谢任何输入。 />
Frank!

Hi friends!
First, I have read other similar threads but I haven't seen a complete match.

It is my VB.NET app. I want the user to be logged out when a user closes the form/application by clicking the X at the right-top (to prevent the user from appearing to be online while not).

Now, I created a sub for LoggOff, put it in a module and call it whenever I want it. So I went to properties and double-clicked"Form Closing" event to write the code for FormClosing. Inside FormClose I call the "LogOff" sub explained above, ofcourse, now when the user clicks the big close 'X' at the top, he/she is logged off.But there is a problem;

The first form to load is the login form after which another form, say "Welcome" will be opened and "login" will be closed (by welcome.open, me.close), now the problem is, whenever me.close is executed, all the code in the FormClosing event is also run, which means terminating the application.

Whenever I just want to close one form and open another one (I don't want to just hide forms) the code will log the user off and close the application. How can I differentiate the closing of a user hitting the big X and the closing of a programmer with me.close???

Many thanks for any input.
Frank!

推荐答案

假设你使用的是Windows Forms:

看看 CloseReason property [ ^ ] FormClosingEventArgs [ ^ ]传递给您的事件处理程序。它将返回 <$ c $之一c> CloseReason 值 [ ^ ],您可以使用它来确定表单被关闭的原因。
Assuming you're using Windows Forms:
Look at the CloseReason property[^] of the FormClosingEventArgs class[^] passed to your event handler. It will return one of the CloseReason values[^], which you can use to determine why the form is being closed.


或者看看在 ApplicationExit [ ^ ]事件


我想你需要查看Project Properties。在应用程序选项卡上(对于大多数Visual Studio版本),您应该看到一些选项,如程序集名称,根命名空间,应用程序类型,图标等。还有一个关闭模式的属性。如果它设置为当启动表单关闭时,那么每次关闭启动表单(也是此页面上设置的属性)时,整个应用程序将结束。您可能希望将其更改为最后一个表单关闭时。这种方式只要你打开一个表单(虽然仍然可以隐藏)并且没有调用Application.Exit(),你的应用程序将继续运行。



您还询问了使用按钮关闭表单或使用x之间的区别。我通常通过具有类级别布尔值来实现这一点。在Load事件中将其设置为False。如果我有一个关闭表单的按钮,我确保在关闭之前将变量设置为True ...就像这样:

I'm thinking you need to look at the Project Properties. On the Application tab (for most versions of Visual Studio) you should see some options like Assembly Name, Root namespace, Application Type, Icon, etc. There is also a property for Shutdown mode. If it's set to "When Startup form closes" then any time you close the Startup form (which is also a property set on this page) the entire application will end. You may want to change that to "When last form closes." This way as long as you have a form open (could still be hidden, though) and haven't called Application.Exit(), your app will continue to run.

You also asked about telling the difference between closing a form with a button or using the x. I typically do that by having a class level boolean. It's set to False in the Load event. If I have a button that closes the form I make sure to set the variable to True before closing...like this:
Private Sub btnHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHome.Click
    boolCloseByButton = True
    Home.Show()
    Me.Close()
End Sub





然后在我的表单结束时检查该变量。如果它设置为True,我知道它因为我编码的按钮而关闭。如果设置为false,我知道用户必须点击X才能关闭表单。如果我真的不希望表单关闭,我可以在FormClosing事件中使用e.Cancel。例如,这里有一些代码用于提示用户,只有当他们使用X关闭时,才能确保他们想要关闭表单:



Then in my Form Closing I check that variable. If it's set to True I know it's closing because of a button I coded. If it's set to false I know that the user must have hit the X to close the form. If I don't really want the form to close I can use the e.Cancel inside the FormClosing event. For example, here is some code that I use to prompt users, only when they close by using the X, to make sure they want to close the form:

Private Sub Form1(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If Not boolCloseByButton Then
        If MessageBox.Show("Do you want to exit?", gblstrFormText, MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            e.Cancel = True
        End If
    End If
End Sub



如果用户选择否, e.Cancel = True 将停止表格关闭。



希望这会有所帮助。


If the user selects no, the e.Cancel = True will stop the form from closing.

Hope this helps.


这篇关于FormClosing事件不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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