Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Getting window form closing reason using C# in .net 2.0.

Author: Zunnair
Download Source Code : 824_FormCloseReasonApp.zip

In this simple article you will learn that get window form closing reason when form close due to any reason.

 Create new c# project. Drag two buttons on the windows form.

 

Now write following code on form load event:

 

C#

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "Form Close Reason Apps";

            button1.Text = "Close Application";

            button2.Text = "Close Form";

 

        }

 

VB

 

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "Form Close Reason Apps"

        button1.Text = "Close Application"

        button2.Text = "Close Form"

 

    End Sub

 

Now write code on buttons events

 

C#

 

private void button1_Click(object sender, EventArgs e)

        {

            Application.Exit();

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            this.Close();

        }

 

VB

 

    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)

        Application.[Exit]()

    End Sub

 

    Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)

        Me.Close()

    End Sub

 

Now Write code on Form Closing Event

 

C#

 

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

        {

            if (e.CloseReason == CloseReason.ApplicationExitCall)

            {

                MessageBox.Show("Closed by Application Exit Call");

            }

            else if (e.CloseReason == CloseReason.FormOwnerClosing )

            {

                MessageBox.Show("Closed by Form Owner closing");

            }

            else if (e.CloseReason == CloseReason.MdiFormClosing )

            {

                MessageBox.Show("MDI form Closing");

            }

            else if (e.CloseReason == CloseReason.TaskManagerClosing)

            {

                MessageBox.Show("Closed by Task Manager");

            }

            else if (e.CloseReason == CloseReason.UserClosing)

            {

                MessageBox.Show("Closed by User");

            }

            else if (e.CloseReason == CloseReason.WindowsShutDown)

            {

                MessageBox.Show("Window Shut down");

            }

 

        }

 

VB

 

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)

        If e.CloseReason = CloseReason.ApplicationExitCall Then

            MessageBox.Show("Closed by Application Exit Call")

        ElseIf e.CloseReason = CloseReason.FormOwnerClosing Then

            MessageBox.Show("Closed by Form Owner closing")

        ElseIf e.CloseReason = CloseReason.MdiFormClosing Then

            MessageBox.Show("MDI form Closing")

        ElseIf e.CloseReason = CloseReason.TaskManagerClosing Then

            MessageBox.Show("Closed by Task Manager")

        ElseIf e.CloseReason = CloseReason.UserClosing Then

            MessageBox.Show("Closed by User")

        ElseIf e.CloseReason = CloseReason.WindowsShutDown Then

            MessageBox.Show("Window Shut down")

        End If

 

    End Sub

 

With this source you can easily get window form closing reason.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Applying system colors on window controls using .net 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net