Monday, April 23, 2007

Winforms Tabbed UI using multiple forms

The problem at hand was how to create a Windows Forms application that uses a tabbed UI, but where you want to put the logic into separate forms, i.e. each tab page is a separate WinForm (this is mainly to not have all the code and controls in the main page).

I found 3 main approaches to this (there are probably more, but these accomplish the stated goal without excessive coding):

1. the MDIWindowManager project (free under BSD license)

2. the DockPanel Suite project (free, from SourceForge)

3. do-it-yourself (VB.Net code)

Private Sub AddFormToTab(ByVal objForm As Form, ByRef objTabPage As TabPage)
objForm.TopLevel = False
objForm.Text = ""
objForm.MaximizeBox = False
objForm.MinimizeBox = False
objForm.ControlBox = False
objTabPage.Controls.Add(objForm)
objForm.Dock = DockStyle.Fill ' Expands the form to fill the tabpage
objForm.Show()
End Sub


Use this method in the main form to add a form to a tab page.
The do-it-yourself method is simple and works, the other 2 links with the accompanying source code provide more functionality.

No comments: