Results 1 to 9 of 9

Thread: Form_Closing

  1. #1

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Thumbs down Form_Closing

    Hi,

    I need to find out if the form is closed by control box or by code or by other means. Like in UnloadMode in VB 6.

    In VB.NET there is no such thing as UnloadMode and MSDN tells me this.

    If it is necessary to determine why the form is closing, you will need to create an overloaded version of the Closing event with custom code to determine the caller.
    Cheers.

  2. #2

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    OK I found this so far and if someone can help me find other constants which are for CloseButton, CloseBYCode and rest then it will be cool.

    Thanks.

    VB Code:
    1. Const WM_ENDSESSION As Integer = &H16
    2. Dim osexit As Boolean = False
    3.  
    4. Protected Overrides Sub WndProc(ByRef e As Message)
    5.         If (e.Msg = WM_ENDSESSION) Then
    6.             osexit = True
    7.             Application.Exit()
    8.         End If
    9.         MyBaseProc(e)
    10. End Sub
    11.  
    12. Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    13.         If osexit Then
    14.             'Not doing anything will let the application close
    15.         Else
    16.             e.Cancel = True 'This will cause the application to ignore the close & continue running
    17.             Me.WindowState = FormWindowState.Minimized 'Minimize the Window
    18.         End If
    19. End Sub

  3. #3

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    * BUMP *

  4. #4
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    i cant understand why you need to override ( even if it does say on msdn ) because if you click the X in the form's controlbox , or if you click a button telling a form to close, it will get handled by the Form_Closing sub, are you getting particular problems?
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  5. #5

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    So you mean to say that the Closing event is only going to fire up when then X button is clicked but not when the form is closed by code.

    Just a little confused. What I need to do is when the X button the clicked I need to hide the form in system tray rather than unloading it.

    Cheers.

  6. #6
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    this is how i use the Form_Closing event ( when the X gets clicked ) , but will also work on say a command button.
    VB Code:
    1. [color=blue]Private Sub[/color] Form1_Closing([color=blue]ByVal[/color] sender [color=blue]As Object[/color], [color=blue]ByVal[/color] e [color=blue]As[/color] System.ComponentModel.CancelEventArgs) [color=blue]Handles MyBase[/color].Closing
    2.         [color=blue]If[/color] MessageBox.Show("do you want to save your changes before quiting", [color=blue]MyBase[/color][color=black].Name[/color], MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes [color=blue]Then[/color]
    3.             e.Cancel = [color=blue]True[/color]
    4.         [color=blue]Else[/color]
    5.             e.Cancel = [color=blue]False[/color]
    6.         [color=blue]End If[/color]
    7.     [color=blue]End Sub[/color]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  7. #7

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    I don't need any message box. It should straight go to the taskbar.

    Cheers.

  8. #8
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Try this, it will allow the user to really close the app if they want, otherwise you can lower it to the toolbar.
    VB Code:
    1. Dim fromButton As Boolean = False
    2.  
    3.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    4.         fromButton = True
    5.         Me.Close()
    6.     End Sub
    7.  
    8.  
    9.     Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    10.         If fromButton Then
    11.             'If a person pushes a button on your form to really close it, then
    12.             'you would really close the form.
    13.             MessageBox.Show("The form is being closed by a button on the form.")
    14.             e.Cancel = False
    15.         Else
    16.             'If a person is closing by the X up top right, then just lower the form
    17.             'to the task bar.
    18.             MessageBox.Show("The form is being closed another way.")
    19.             e.Cancel = True
    20.             'Do your code to lower the form to the task bar.
    21.         End If
    22.     End Sub

  9. #9

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    Thanks Guyz.

    I just used this code and it works nice.

    VB Code:
    1. Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    2.     e.Cancel = True
    3.     Show_In_Tray
    4. End Sub

    Cheers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width