Multiple-document interface (MDI) applications can be very easily created in Visual Basic 6.0 simply by adding an MDI form to the project. Forms with the MDIChild property set are considered to be child forms of this MDI form. In Visual Basic .NET, any form can be made an MDI parent. For this a property called IsMdiContainer has to be set to True. In Visual Basic 6.0, an MDI application that also contained a form that was not an MDI child would not end until that form was closed, even if the MDI parent was closed. In Visual Basic .NET, the application will end when the start-up form is closed, regardless of any non-MDI forms in the application. Application developers have to note that the properties and methods that applied to Visual Basic 6.0 MDI forms have changed in Visual Basic .NET. However, the upgrade wizard takes care of the details while upgrading, and no major issues are faced during the upgrade process. CODE EXAMPLES Example 15 In Visual Basic, in a new project add an MDI form from the Project Add MDI Form menu. Once an MDI form has been added to the project, this option is disabled. This is because Visual Basic applications cannot contain more than one MDI parent form at a time in a single project. Note that the AutoShowChildren property of the MDI form is set to True by default. Add a second form to the project and set its MDIChild property to True. This will make the second form the child of the MDI container. Add menus called Open and Exit to the MDI form. On clicking Open , add logic for opening the child form. The following code is in the MDIForms1-VB folder of for chapter: Private Sub MDIForm_Load() Form2.Show End Sub Private Sub mnuExit_Click() End End Sub Private Sub mnuOpen_Click() Form1.Show End Sub Now make the second form the start-up for the project. Once the project is executed, because the second form is the start-up form, the MDI form is also invoked because the second form cannot stand by itself. It is contained in the MDI form. Now make the MDI form the start-up form. In this case, when the project is executed, only the MDI form is shown. Once the user clicks on the Open menu, the child form is activated and shown. When this project is migrated using the upgrade wizard, there are no issues, and the application behaves the same in Visual Basic .NET. The upgrade wizard generates the following code, which is kept in the MDIForms1-VB.NET folder for this chapter: Public Sub mnuExit_Popup(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles mnuExit.Popup mnuExit_Click(eventSender, eventArgs) End Sub Public Sub mnuExit_Click(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles mnuExit.Click End End Sub Public Sub mnuOpen_Popup(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles mnuOpen.Popup mnuOpen_Click(eventSender, eventArgs) End Sub Public Sub mnuOpen_Click(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles mnuOpen.Click Form1.DefInstance.Show() End Sub In Visual Basic .NET, any form can be an MDI form when it's property IsMdiContainer is set to True. The upgrade wizard sets this property during the upgrade process. Example 16 Continuing with the preceding example, set the backcolor property of the MDI form in Visual Basic 6.0 to yellow. Also add another new form in the project. However, in the new form do not set the MDIChild property to True. It is False by default. Add the following code to the project. This time the MDI form is made the start-up form for the project. The following code is in the MDIForms2-VB folder for this chapter: Private Sub MDIForm_Load() Form2.Show End Sub Private Sub mnuExit_Click() End End Sub Private Sub mnuOpen_Click() Form1.Show End Sub On executing this project, the MDI form gets loaded first because it has been defined as the start-up object for the project. The background color of the MDI form is yellow as set in the backcolor property of the MDI form at design time. The second form (which is not an MDI child) is also loaded because code to load that form has been written. Now when the MDI form is closed, all the child forms would be closed automatically. However, the second form remains loaded, and the application is not closed. Therefore, to terminate the application, both the MDI form and the non-MDI child forms must be closed. When this project is migrated using the upgrade wizard, there are no errors or warnings. However, the application behaves differently in Visual Basic .NET. First of all the back color of the MDI form is lost in the upgrade process. Also when the MDI form is closed, the application is terminated . All other forms (including MDI child and nonchild forms) are closed. The upgrade wizard generates the following code, which is stored in the MDIForms2-VB.NET folder for this chapter: Private Sub MDIForm1_Load(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles MyBase.Load Form2.DefInstance.Show() End Sub Public Sub mnuExit_Popup(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles mnuExit.Popup mnuExit_Click(eventSender, eventArgs) End Sub Public Sub mnuExit_Click(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles mnuExit.Click End End Sub Public Sub mnuOpen_Popup(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles mnuOpen.Popup mnuOpen_Click(eventSender, eventArgs) End Sub Public Sub mnuOpen_Click(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles mnuOpen.Click Form1.DefInstance.Show() End Sub There is an alternative way to achieve the same functionality of back color in MDI forms in Visual Basic .NET. Create a blank image with the desired background color and save the image. Now for the MDI form, set the BackgroundImage property to the newly created image. This will ensure that the MDI form retains the same back color in the upgraded Visual Basic .NET application. |