| < Free Open Study > |
|
The last exercise in the automation code will demonstrate how to place a menu on the form programmatically. Listing 6-13 contains the code to place the menu on the form. Adding a menu to the form programmatically is one of the simpler tasks that you have had to perform in this automation process. If you have not already done so, perhaps you should stop and create a Windows application with one form in it and familiarize yourself with the Menu Designer.
Listing 6-13: Adding a Menu
01 'Create a menu on the form 02 Dim mainMenuComp As IComponent = fdHost.CreateComponent 03 (Type.GetType("System.Windows.Forms.MainMenu, 04 System.Windows.Forms"), "mainMenu1") 05 Dim fileMenuItemComp As IComponent = fdHost.CreateComponent 06 (Type.GetType("System.Windows.Forms.MenuItem, 07 System.Windows.Forms"), "fileMenuItem") 08 Dim menuItem2Comp As IComponent = fdHost.CreateComponent 09 (Type.GetType("System.Windows.Forms.MenuItem, 10 System.Windows.Forms"), "mainItem2") 11 Dim menuItem3Comp As IComponent = fdHost.CreateComponent 12 (Type.GetType("System.Windows.Forms.MenuItem, 13 System.Windows.Forms"), "mainItem3") 14 Dim menuItem4Comp As IComponent = fdHost.CreateComponent 15 (Type.GetType("System.Windows.Forms.MenuItem, 16 System.Windows.Forms"), "mainItem4") 17 18 Dim mainMenu1 As MainMenu = CType(mainMenuComp, MainMenu) 19 Dim fileMenuItem As MenuItem = CType(fileMenuItemComp, MenuItem) 20 Dim menuItem2 As MenuItem = CType(menuItem2Comp, MenuItem) 21 Dim menuItem3 As MenuItem = CType(menuItem3Comp, MenuItem) 22 Dim menuItem4 As MenuItem = CType(menuItem4Comp, MenuItem) 23 24 fileMenuItem.Text = "&File" 25 menuItem2.Text = "&Edit" 26 menuItem3.Text = "&Open" 27 menuItem4.Text = "&Save" 28 29 mainMenu1.MenuItems.Add(fileMenuItem) 30 mainMenu1.MenuItems.Add(menuItem2) 31 32 fileMenuItem.MenuItems.Add(menuItem3) 33 fileMenuItem.MenuItems.Add(menuItem4)
Menus, just like buttons and other controls, are components on the form. In VB 6.0, you could add controls to a form at runtime if you had at least one control of the type that you wanted to add already on the form. However, you could not add a menu from scratch. In .NET, you can add any component (control) to the form at runtime, including a main menu and menu items (submenus).
Line 02 dimensions and creates mainMenuComp as a MainMenu constructor. A MainMenu is an array to which you can add MenuItem objects. Lines 05 through 14 create the MenuItem objects that will be added to the MainMenu array. Note that the menu objects, both the MainMenu array and the individual MenuItem objects, are being named as they are created. If you were manually adding menu items to a menu control in the IDE, you would right-click the item to edit the name. In the automation model, you do that as part of adding the menu item to the array of items. To understand more clearly exactly what is going on with each line of the automation code, you should compare the code in the automation example to the code being generated. The complete code for the whole form is shown in Listing 6-14.
Listing 6-14: Completed Form Code
Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents btnTestButton As System.Windows.Forms.Button Friend WithEvents mainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents fileMenuItem As System.Windows.Forms.MenuItem Friend WithEvents mainItem2 As System.Windows.Forms.MenuItem Friend WithEvents mainItem3 As System.Windows.Forms.MenuItem Friend WithEvents mainItem4 As System.Windows.Forms.MenuItem <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.btnTestButton = New System.Windows.Forms.Button() Me.mainMenu1 = New System.Windows.Forms.MainMenu() Me.fileMenuItem = New System.Windows.Forms.MenuItem() Me.mainItem2 = New System.Windows.Forms.MenuItem() Me.mainItem3 = New System.Windows.Forms.MenuItem() Me.mainItem4 = New System.Windows.Forms.MenuItem() Me.SuspendLayout() ' 'btnTestButton ' Me.btnTestButton.Location = New System.Drawing.Point(30, 30) Me.btnTestButton.Name = "btnTestButton" Me.btnTestButton.Size = New System.Drawing.Size(100, 60) Me.btnTestButton.TabIndex = 0 Me.btnTestButton.Text = "&Test Button" ' 'mainMenu1 ' Me.mainMenu1.MenuItems.AddRange (New System.Windows.Forms.MenuItem(){Me.fileMenuItem, Me.mainItem2}) ' 'fileMenuItem ' Me.fileMenuItem.Index = 0 Me.fileMenuItem.MenuItems.AddRange (New System.Windows.Forms.MenuItem() {Me.mainItem3, Me.mainItem4}) Me.fileMenuItem.Text = "&File" ' 'mainItem2 ' Me.mainItem2.Index = 1 Me.mainItem2.Text = "&Edit" ' 'mainItem3 ' Me.mainItem3.Index = 0 Me.mainItem3.Text = "&Open" ' 'mainItem4 ' Me.mainItem4.Index = 1 Me.mainItem4.Text = "&Save" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnTestButton}) Me.Menu = Me.mainMenu1 Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Private Sub btnTestButton_OnClick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnTestButton.Click End Sub End Class
Line 18 dimensions and casts the MainMenu object to type MainMenu, which is the array that will be the container for the MenuItem objects. Lines 19 through 22 dimension and cast the individual MenuItem objects. These are the individual MenuItems, both top-level menus and submenus.
Lines 24 through 27 set the Text property of the MenuItem objects. Finally, lines 29 and 30 add the top-level MenuItem objects to the array. Lines 32 and 33 add the submenus to the File menu.
Now that you have added the menu to the form, the automation demonstration is complete. The completed form displayed in the Designer is shown in Figure 6-11.
Figure 6-11: Completed form
The complete code for the form as generated by the automation demonstration is shown in Listing 6-14. If you are having trouble understanding any of the processes that you have gone through, it will be most helpful for you to compare the code in the automation code snippets to the generated code in the form.
| < Free Open Study > |
|