Click here to Skip to main content
15,886,833 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Back and Forward button for MDI Parent form to traverse through opened MDI child forms

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
27 Feb 2014CPOL 12.7K   3   1

Introduction 

This code snippet defines methods for Back and Forward buttons for MDI parent forms to show all opened MDI child forms one by one.

Using the code

One can implement the below code in a button click or in any event as applicable.

In this article, the code is implemented within the click event of menu items.

VB.NET
 Private Sub BACKToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BACKToolStripMenuItem.Click
        Try
            Dim t As New Form
            For Each f As Form In Application.OpenForms
                If f IsNot Me.ActiveMdiChild Then
                    t = f
                Else
                    t.Activate()
                    Return
                End If
            Next
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.Message, "Error...!!!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub FORWARDToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FORWARDToolStripMenuItem.Click
        Try
            Dim t As New Form
            For i As Integer = Application.OpenForms.Count - 1 To 0 Step -1
                Dim f As Form = Application.OpenForms(i)
                If f IsNot Me.ActiveMdiChild Then
                    t = f
                Else
                    t.Activate()
                    Return
                End If
            Next
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.Message, "Error...!!!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub 

Points of Interest 

This method is actually 'old school' and can be refined in numerous ways.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Arab Emirates United Arab Emirates
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionBack and Forward button for MDI Parent Pin
Rashad Hameed31-May-16 1:20
Rashad Hameed31-May-16 1:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.