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
 

Close active form in a MDI container using C# and VB in .net 2.0.

Author: Zunnair
Download Source Code : 835_CloseActiveMdi.zip

In this simple article you will learn that how to you can close active form in a MDI container.

 Create new c# project. Drag Menu Strip and add two new forms.

 

 

Now write following code on form load event:

 

C#

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "Close Active Form in MDI";

            this.IsMdiContainer = true;

            this.WindowState = FormWindowState.Maximized;

           

            Form2 frm2 = new Form2();

            frm2.MdiParent = this;

            frm2.Show();

           

            Form3 frm3 = new Form3();

            frm3.MdiParent = this;

            frm3.Show();

        }

 

VB

 

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

        Me.Text = "Close Active Form in MDI"

        Me.IsMdiContainer = True

        Me.WindowState = FormWindowState.Maximized

 

        Dim frm2 As New Form2()

        frm2.MdiParent = Me

        frm2.Show()

 

        Dim frm3 As New Form3()

        frm3.MdiParent = Me

        frm3.Show()

    End Sub

 

Now write code on Menu events

 

C#

 

private void closeActiveFormToolStripMenuItem_Click(object sender, EventArgs e)

        {

            try

            {

                this.ActiveMdiChild.Close();

            }

            catch (Exception ex)

            {

                MessageBox.Show("No Child Form Available...");

            }

        }

 

        private void showNewFormToolStripMenuItem_Click(object sender, EventArgs e)

        {

            Form2 frm2 = new Form2();

            frm2.MdiParent = this;

            frm2.Show();

        }

VB

 

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

        Try

            Me.ActiveMdiChild.Close()

        Catch ex As Exception

            MessageBox.Show("No Child Form Available...")

        End Try

    End Sub

 

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

        Dim frm2 As New Form2()

        frm2.MdiParent = Me

        frm2.Show()

    End Sub

 

 

This is the way to close active form in MDI container.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Conversion of string into character array using C# and VB in .net 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net