Click here to Skip to main content
15,904,156 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi
I want to navigate like how the windows explorer behaves,
Assume There is 1 Mdi and 10 Child Forms,

So i want to navigate forward,back,up from one form to other..

Let me know if anybody have an idea..
Thanks in Advance..


Regards,
Pawan.
Posted

The answer in C#

Create a project, add an MDI form and create two buttons named cmdBack and cmdForward.

C#
private void cmdBack_Click(object sender, EventArgs e)
        {
            try
            {
                Form PrevForm = null;
                foreach (Form ChildForm in this.MdiChildren)
                {
                    if (ChildForm.Equals(this.ActiveMdiChild))
                    {
                        if ((PrevForm != null))
                        {
                            this.ActivateMdiChild(PrevForm);
                            PrevForm.Focus();
                            break;
                        }
                    }
                    PrevForm = ChildForm;
                }
                PrevForm = null;

            }
            catch (Exception ex)
            {
            }

        }



C#
private void cmdForward_Click(object sender, EventArgs e)
        {
            try
            {
                Form NextForm = null;
                foreach (Form ChildForm in this.MdiChildren.Reverse())
                {
                    if (ChildForm.Equals(this.ActiveMdiChild))
                    {
                        if ((NextForm != null))
                        {
                            this.ActivateMdiChild(NextForm);
                            NextForm.Focus();
                            break;
                        }
                    }
                    NextForm = ChildForm;
                }
                NextForm = null;

            }
            catch (Exception ex)
            {
            }

        }
 
Share this answer
 
Comments
Pawan Kiran 24-Jul-10 2:53am    
Reason for my vote of 5
Thanks to you for posting answers in both VB,C#.
I had the same problem when I found your question, so I gave up searching. The code I wrote resolved the problem for me.

In your project, create two buttons named cmdBack and cmdForward and try the following:

VB
Private Sub cmdBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdBack.Click
        Try
            Dim PrevForm As Form = Nothing
            For Each ChildForm As Form In Me.MdiChildren
                If ChildForm.Equals(Me.ActiveMdiChild) Then
                    If Not IsNothing(PrevForm) Then
                        Me.ActivateMdiChild(PrevForm)
                        PrevForm.Focus()
                        Exit For
                    End If
                End If
                PrevForm = ChildForm
            Next
            PrevForm = Nothing
        Catch ex As Exception

        End Try
    End Sub


VB
Private Sub cmdForward_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdForward.Click
    Try
        Dim NextForm As Form = Nothing
        For Each ChildForm As Form In Me.MdiChildren.Reverse
            If ChildForm.Equals(Me.ActiveMdiChild) Then
                If Not IsNothing(NextForm) Then
                    Me.ActivateMdiChild(NextForm)
                    NextForm.Focus()
                    Exit For
                End If
            End If
            NextForm = ChildForm
        Next
        NextForm = Nothing
    Catch ex As Exception

    End Try
End Sub
 
Share this answer
 
v2
Comments
Pawan Kiran 23-Jul-10 2:52am    
Reason for my vote of 5
Many many thanks to you. just know tested it in sample application and it works fine.
Toli Cuturicu 23-Jul-10 4:50am    
Reason for my vote of 1
no c# here (see tags)
Pawan Kiran 24-Jul-10 2:55am    
Hi Toli cuturicu, answer posted by Wazza9 is true and it is according the question asked by me.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900