Add MDI Form and Main Menu

In our previous tutorial I discuss on “How to Create Master/Detail Windows Forms” on our Library System Project.

Today we will add MDI Form and create a main menu to open the forms on our project. If you follow closely this tutorial you will have now several Windows Forms on your project.

Please follow the steps below to Add MDI Form and Main Menu on your Library System project.

  1. Click Project >> Add Windows Form.
  2. Select MDI Form in “Add New Item” dialog form.

    Add Main Form

  3. By default Visual Studio creates a Main Menu automatically for you. But since the default one is not related to our project we will remove some of it and add our own menu.

    Default menu:

    MDI Form

    Library System Menu:

    Library System Menu

  4. Next we will add a code to open every form on our project. To do this double click “Borrow Book(s)” menu.

    There are so many unnecessary codes behind MDI Form. Delete it and leave only the following code:

    Imports System.Windows.Forms Public Class mdiMain Private Sub BorrowBooksToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BorrowBooksToolStripMenuItem.Click Dim f As Form = Application.OpenForms.Item("frmBorrow") If f Is Nothing Then Dim Borrow As New frmBorrow With Borrow .getFormState = modGlobal.FormState.adStateAddMode .MdiParent = Me .StartPosition = FormStartPosition.CenterScreen .Show() End With Else f.Show() f.BringToFront() End If End Sub End Class

    The code above will simply open the Borrow form in Add state.

    getFormState is a public property on Borrow Form. Here’s the code in our Borrow Form.

    Public Class frmBorrow Public BorrowID As Integer Dim State As FormState Public WriteOnly Property getFormState() Set(ByVal value) State = value End Set End Property Private Sub BorrowBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BorrowBindingNavigatorSaveItem.Click Me.Validate() Me.BorrowBindingSource.EndEdit() Me.TableAdapterManager.UpdateAll(Me.BorrowDataSet) End Sub Private Sub frmBorrow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.BooksTableAdapter.Fill(Me.BooksDataSet.Books) Me.BorrowDetailsTableAdapter.Fill(Me.BorrowDataSet.BorrowDetails) Me.BorrowTableAdapter.Fill(Me.BorrowDataSet.Borrow) If State = FormState.adStateAddMode Then BindingNavigatorAddNewItem.PerformClick() End If End Sub End Class

We need to add Module on our project to handle the getFormState property.

  1. Create another folder inside your project called “Module”.
  2. The click Project >> Add Module.

    Add Module

  3. Name it as “modGlobal”.

    Global Module

  4. Paste the following code on the declaration section of modGlobal module. Module modGlobal 'Enumerator for form state Public Enum FormState adStateAddMode = 0 adStateEditMode = 1 End Enum End Module

    Global Module Code

After this you need to the startup object. Read “How to Select Startup Form” on our previous tutorial and choose mdiMain.

That’s it. You can now run your project and see if you can open “Borrow” form.

frmBorrow

Back to Visual Basic .NET 2008 Tutorial.

Add new comment