Menus can be very easily created in Visual Basic 6.0 Forms using the Menu Editor. They can also be modified at runtime and they can be displayed as main menus as well as context menus . Visual Basic .NET provides two separate controls, MainMenu and ContextMenu . Both are available in the Visual Studio .NET control toolbox at design time. These controls can be added to the Visual Basic .NET Form at design time, or they can be created dynamically during runtime. Let us look at some code examples to evaluate the issue of migrating menu controls to Visual Basic .NET. CODE EXAMPLES Example 13 In Visual Basic, create two main menu controls called File and Exit. When user clicks on the Exit menu, the program ends. When the user clicks on the File menu, a submenu called List appears. When the user clicks on the List submenu, a Drive list box, a directory list box, and a file list box are displayed to the user. The user can get the list of the files in any drive and directory. Note that access keys have been set for all the menu items. The file menu can be accessed from the keyboard with the ALT + F key combination. Note that a shortcut key (CTRL + L) has been assigned to invoke the List functionality from the keyboard without having to navigate through the menu. Figure 4-6 shows the screen in design mode in Visual Basic 6.0. The following code is in the Menu2-VB folder for this chapter: Figure 4-6. File Menu in Design mode. Private Sub Dir1_Change() File1.Path = Dir1.Path End Sub Private Sub Drive1_Change() Dir1.Path = Drive1.Drive File1.Path = Dir1.Path End Sub Private Sub Form_Load() Drive1.Visible = False Dir1.Visible = False File1.Visible = False End Sub Private Sub mnuExit_Click() End End Sub Private Sub mnuList_Click() Drive1.Visible = True Dir1.Visible = True File1.Visible = True End Sub The following equivalent Visual Basic .NET code is in the Menu2-VB.NET folder for this chapter: Public WithEvents mnuList As System.Windows.Forms. _ MenuItem Public WithEvents MnuFile As System.Windows.Forms. _ MenuItem Public WithEvents mnuExit As System.Windows.Forms. _ MenuItem Public MainMenu1 As System.Windows.Forms.MainMenu Me.MainMenu1 = New System.Windows.Forms.MainMenu Me.MnuFile = New System.Windows.Forms.MenuItem Me.mnuList = New System.Windows.Forms.MenuItem Me.mnuExit = New System.Windows.Forms.MenuItem MainMenu1.MenuItems.AddRange(New System.Windows.Forms. _ MenuItem(){Me.MnuFile, Me.mnuExit}) Me.mnuList.Index = 0 MnuFile.MenuItems.AddRange(New System.Windows.Forms. _ MenuItem(){Me.mnuList}) Me.Menu = MainMenu1 Private Sub Dir1_Change(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles Dir1.Change File1.Path = Dir1.Path End Sub Private Sub Drive1_SelectedIndexChanged(ByVal _ eventSender As System.Object, ByVal eventArgs As _ System.EventArgs) Handles Drive1.SelectedIndexChanged Dir1.Path = Drive1.Drive File1.Path = Dir1.Path End Sub Private Sub Form1_Load(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles MyBase.Load Drive1.Visible = False Dir1.Visible = False File1.Visible = False 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 mnuList_Popup(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles mnuList.Popup mnuList_Click(eventSender, eventArgs) End Sub Public Sub mnuList_Click(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles mnuList.Click Drive1.Visible = True Dir1.Visible = True File1.Visible = True End Sub Note how the menu objects are created. These are declared to be of type MenuItem that reside in the namespace System.Windows.Form . The main menu for the form is declared to be of type MainMenu , and the other menu items are added to the main menu. When the project is upgraded to Visual Basic .NET, there are no issues, and the code behaves in the same manner and gives the same functionality as in Visual Basic 6.0. The functionality of the access keys is maintained in Visual Basic .NET. The File menu can be accessed from the keyboard with the ALT + F key combination. Note that even the shortcut key assigned to the List menu (CTRL + L) works in Visual Basic .NET. In this example, another set of controls is also upgraded. The DriveListBox , DirListBox, and the FileListBox are no longer available in Visual Basic .NET. The OpenFileDialog and the SaveFileDialog controls in Visual Basic .NET provide the same functionality as these controls, and the upgrade wizard automatically supports functionality for these controls through the Microsoft. VisualBasic.Compatibility namespace. Example 14 This example demonstrates the use of context menu and the issues faced in the upgrade as well as what the developer has to do in Visual Basic .NET to get the code working. In this example a normal form with two menu items is created. The first menu item is called Edit and it has two submenus, cut and copy. The second menu item is called Exit, which is used to terminate the application. The form has two labels. When the user right-clicks on the first label, the context menu (with the options cut and copy) appears. When the user clicks on copy, contents of label1 are copied to label2. If the user clicks on cut, the contents of label1 are copied to label2, and the contents of label1 are emptied. The following code snippet shows the logic for implementing the context menu in Visual Basic 6.0 code. The logic for the context menu is implemented in the mouse down event of Label1. The PopupMenu command is used to invoke the specified menu as the context menu. The following code is in the ContextMenu-VB folder for this chapter: Private Sub Label1_MouseUp(Button As Integer, Shift As _ Integer, X As Single, Y As Single) If Button = vbRightButton Then PopupMenu mnuEdit End If End Sub Private Sub mnuCopy1_Click() Label2.Caption = Label1.Caption End Sub Private Sub mnuCut_Click() Label2.Caption = Label1.Caption Label1.Caption = "" End Sub Private Sub mnuExit_Click() End End Sub Upgrading and compiling this code results in compilation errors. Visual Basic .NET does not have an equivalent for the popup menu. Visual Basic .NET has a different mechanism for implementing context menus. To make the preceding code work in Visual Basic .NET, first, select a context menu from the toolbox and drop it into the form. Edit the menu and add the submenu items (cut and copy) as in the example. Put the following code snippets in the click events of the cut and copy menu items. The following equivalent Visual Basic .NET code is in the ContextMenu-VB.NET-Modified folder for this chapter: Public Sub mnuCopy1_Popup(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles mnuCopy1.Popup mnuCopy1_Click(eventSender, eventArgs) End Sub Public Sub mnuCopy1_Click(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles mnuCopy1.Click Label2.Text = Label1.Text End Sub Public Sub mnuCut_Popup(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles mnuCut.Popup mnuCut_Click(eventSender, eventArgs) End Sub Public Sub mnuCut_Click(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) _ Handles mnuCut.Click Label2.Text = Label1.Text Label1.Text = "" 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 Private Sub MenuItem1_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) Handles _ MenuItem1.Click Label2.Text = Label1.Text Label1.Text = "" End Sub Private Sub MenuItem2_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) Handles _ MenuItem2.Click Label2.Text = Label1.Text End Sub As shown, in Visual Basic any menu can be made into a context menu. However, Visual Basic .NET offers two different objects, MainMenu for normal menus displayed on forms and ContextMenu for popup menus. All controls on the form have a property called ContextMenu where the developer can specify which context menu to pop up. |