Handling Menu Events Menus

221 myMenu = New MainMenu mnuFile = New MenuItem mnuFileExit = New MenuItem mnuEdit = New MenuItem Set the properties of the menu items. mnuFile.Text = File mnuFileExit.Text = Exit mnuEdit.Text = Edit Connect the menu items to each other and to the main menu. mnuFile.MenuItems.AddmnuFileExit myMenu.MenuItems.AddmnuFile myMenu.MenuItems.AddmnuEdit Connect the main menu to the form. Me.Menu = myMenu End Sub End Class

5.5.3 Handling Menu Events

User interaction with a menu causes menu events to be fired. The most common menu event is the Click event of the MenuItem class, which fires when a user clicks a menu item. Here is an example of a Click event handler this code could be added to the Form1 class of Ex am ple 5- 2 : Private Sub mnuFileExit_Click _ ByVal sender As Object, _ ByVal e As EventArgs _ Handles mnuFileExit.Click Me.Close End Sub The events of the MenuItem class are: Click Fired when the menu item is chosen either by clicking it with the mouse or by pressing a shortcut key combination defined for the menu item. The syntax of the Click event is: Public Event Click As System.EventHandler This is equivalent to: Public Event ClickByVal sender As Object, ByVal e As System.EventArgs Disposed Fired when the MenuItem objects Dispose method is called. The syntax of the Disposed event is: Public Event Disposed As System.EventHandler This is equivalent to: Public Event DisposedByVal 222 sender As Object, ByVal e As System.EventArgs The event is inherited from the Component class. DrawItem Fired when the menu item needs to be drawn, when the MenuItem objects OwnerDraw property is True . The syntax of the DrawItem event is: Public Event DrawItem As System.Windows.Forms.DrawItemEventHandler This is equivalent to: Public Event DrawItem _ ByVal sender As Object, _ ByVal e As System.Windows.Forms.DrawItemEventArgs _ The e parameter, of type DrawItemEventArgs, provides additional information that is needed for drawing the menu item. The properties of the DrawItemEventArgs class are: BackColor The background color that should be used when drawing the item. The type is Color defined in the System.Drawing namespace. Bounds The bounding rectangle of the menu item. The type is Rectangle defined in the System.Drawing namespace. Font The font that should be used when drawing the item. The type is Font defined in the System.Drawing namespace. ForeColor The foreground color that should be used when drawing the item. The type is Color defined in the System.Drawing namespace. Graphics The graphics surface on which to draw the item. The type is Graphics defined in the System.Drawing namespace. Index The index of the menu item within its parent menu. The type is Integer. State The state of the menu item. The type is DrawItemState defined in the System.Windows.Forms namespace. DrawItemState is an enumeration that defines the 223 values None , Selected , Grayed , Disabled , Checked , Focus , Default , HotLight , Inactive , NoAccelerator , NoFocusRect , and ComboBoxEdit . MeasureItem Fired prior to firing the DrawItem event when the MenuItem objects OwnerDraw property is True . The MeasureItem event allows the client to specify the size of the item to be drawn. The syntax of the MeasureItem event is: Public Event MeasureItem As System.Windows.Forms.MeasureItemEventHandler This is equivalent to: Public Event MeasureItem _ ByVal sender As Object, _ ByVal e As System.Windows.Forms.MeasureItemEventArgs _ The e parameter, of type MeasureItemEventArgs, provides additional information needed by the event handler and provides fields that the event handler can set to communicate the item size to the MenuItem object. The properties of the MeasureItemEventArgs are: Graphics The graphics device upon which the menu item will be drawn. This is needed so the client can determine the scale of the device upon which the menu item will be rendered. The type is Graphics defined in the System.Drawing namespace. Index The index of the menu item within its parent menu. The type is Integer. ItemHeight The height of the menu item. The type is Integer. ItemWidth The width of the menu item. The type is Integer. Popup Fired when the submenu is about to be displayed, when a menu item has subitems associated with it. This provides the client with an opportunity to set the menu states checked, enabled, etc. of the submenu items to match the current program state. The syntax of the Popup event is: Public Event Popup As System.EventHandler This is equivalent to: Public Event PopupByVal sender As Object, ByVal e As System.EventArgs Select 224 Fired when the user places the mouse over the menu item or when the user highlights the menu item by navigating to it with the keyboard arrow keys. The syntax of the Select event is: Public Event Select As System.EventHandler This is equivalent to: Public Event SelectByVal sender As Object, ByVal e As System.EventArgs The ContextMenu class also exposes a Popup event, which is fired just before the context menu is displayed. The syntax of the Popup event is: Public Event Popup As System.EventHandler This is equivalent to: Public Event PopupByVal sender As Object, ByVal e As System.EventArgs

5.5.4 Cloning Menus