Handling Menu Events Menus
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.EventArgs5.5.4 Cloning Menus
Parts
» VB.NET - (O'Reilly) Programming Visual Basic NET
» What Is the Microsoft .NET Framework?
» hello, world An Example Visual Basic .NET Program
» Hello, Windows An Example Visual Basic .NET Program
» Hello, Browser An Example Visual Basic .NET Program
» Source Files Identifiers The Visual Basic .NET Language
» Numeric Literals String Literals Character Literals
» Date Literals Boolean Literals Nothing Summary of Literal Formats
» Custom Types Collections Types
» The Namespace Statement The Imports Statement
» Symbolic Constants Scope The Visual Basic .NET Language
» Access Modifiers Assignment The Visual Basic .NET Language
» Unary Operators Arithmetic Operators
» Relational Operators Operators and Expressions
» String-Concatenation Operators Bitwise Operators
» Logical Operators Operator Precedence
» Call Exit Branching Statements
» Goto If RaiseEvent Branching Statements
» Return Select Case Branching Statements
» For Each Iteration Statements
» Object Instantiation and New Constructors
» Handling Events Inheritance Classes
» Passing arrays as parameters
» Variable-length parameter lists Main method
» Implementing interface methods Overriding inherited methods
» Overloading Overloading inherited methods
» The MyBase Keyword Nested Classes Destructors
» Interfaces The Visual Basic .NET Language
» Enumerations The Visual Basic .NET Language
» Exceptions The Visual Basic .NET Language
» Delegates The Visual Basic .NET Language
» Using Events and Delegates Together
» Creating Custom Attributes Attributes
» Standard Modules Conditional Compilation
» Summary The Visual Basic .NET Language
» Common Language Infrastructure CLI and Common Language Runtime CLR
» Global Assembly Cache GAC Comparison of Assemblies, Modules, and Namespaces
» Application Domains Common Language Specification CLS
» Intermediate Language IL and Just-In-Time JIT Compilation Metadata
» Finalize Memory Management and Garbage Collection
» Dispose Memory Management and Garbage Collection
» A Brief Tour of the .NET Framework Namespaces
» Configuration File Format Configuration
» Configuration Section Groups The appSettings Section
» Adding event handlers Creating a Form in Code
» Handling Form Events Windows Forms I: Developing Desktop Applications
» Relationships Between Forms Windows Forms I: Developing Desktop Applications
» Merging Menus MDI Applications
» Detecting MDI Child Window Activation
» Component Attributes Windows Forms I: Developing Desktop Applications
» The Graphics Class 2-D Graphics Programming with GDI+
» The Pen Class 2-D Graphics Programming with GDI+
» The Brush Class 2-D Graphics Programming with GDI+
» System colors The Color Structure
» Alpha Blending 2-D Graphics Programming with GDI+
» Antialiasing 2-D Graphics Programming with GDI+
» The PrintPageEventArgs Class Printing
» The OnBeginPrint and OnEndPrint Methods Choosing a Printer
» The PageSettings Class Printing
» The PrinterSettings Class Printing
» Page Setup Dialog Box Print Preview
» Summary Windows Forms I: Developing Desktop Applications
» The Button Class The CheckBox Class The ComboBox Class
» The DateTimePicker Class The GroupBox Class The ImageList Class
» The Label Class The LinkLabel Class
» The ListBox Class Common Controls and Components
» The ListBox.ObjectCollection Class
» The ListView Class Common Controls and Components
» The MonthCalendar Class Common Controls and Components
» The Panel Class The PictureBox Class
» The RadioButton Class Common Controls and Components
» The TextBox Class The Timer Class
» Other Controls and Components
» Control Events Windows Forms II: Controls, Common Dialog Boxes, and Menus
» The Anchor Property Form and Control Layout
» Controlling dock order The Dock Property
» The Splitter control The Dock Property
» ColorDialog FontDialog OpenFileDialog Common Dialog Boxes
» PageSetupDialog PrintDialog PrintPreviewDialog SaveFileDialog
» Adding Menus in the Visual Studio .NET Windows Forms Designer
» Programmatically Creating Menus Menus
» Building Controls from Other Controls
» Building Controls That Draw Themselves Building Nonrectangular Controls
» Summary Windows Forms II: Controls, Common Dialog Boxes, and Menus
» Setting control properties using attributes Adding event handlers
» AutoEventWireup Handling Page Events
» The Button control Web Controls
» The CheckBox control The DropDownList control
» The Image control The Label control
» The ListBox control Web Controls
» The RadioButton control Web Controls
» The Table control Web Controls
» The TextBox control Web Controls
» Other web controls Web Controls
» HTML Controls Handling Control Events
» Programmatically Instantiating Controls More About Server Controls
» More About Validation-Control Tag Attributes
» Using Validation-Control Properties Providing a Summary View of Validation Failures
» Performing Custom Validation Adding Validation
» Using Directives to Modify Web Page Compilation
» The Server Object ASP.NET Objects: Interacting with the Framework
» The Application Object The Session Object The Cache Object The Request Object
» The Response Object ASP.NET Objects: Interacting with the Framework
» Discovering Browser Capabilities ASP.NET and Web Forms: Developing Browser-Based Applications
» The Session Object Maintaining State
» The Application Object Maintaining State
» Session and Application Startup and Shutdown global.asax Compiles to a Class
» Adding Global Objects Application-Level Code and global.asax
» ASP.NET authorization Authorization
» Windows NTFS authorization Code-access authorization
» IUSR_ComputerName Impersonation Accessing Network Resources
» User Controls Designing Custom Controls
» Creating a custom server control using Visual Studio .NET
» Creating a custom server control in code Using a custom server control in Visual Studio .NET
» Summary ASP.NET and Web Forms: Developing Browser-Based Applications
» The WebService Attribute The WebMethod Attribute
» Testing a Web Service with a Browser
» Consuming a Web Service in Visual Studio .NET
» Consuming a Web Service in Notepad
» Synchronous Versus Asynchronous Calls
» Web-Service Descriptions Web-Service Discovery
» Limitations of Web Services Summary
» A Brief History of Universal Data Access Managed Providers
» Connecting to a SQL Server Database
» Connecting to an OLE DB Data Source
» The DataSet Class Finding Tables
» Finding Column Values Finding Column Definitions Changing, Adding, and Deleting Rows
» Writing Updates Back to the Data Source
» Relations Between DataTables in a DataSet
» The DataSets XML Capabilities
» Binding a DataSet to a Windows Forms DataGrid
» Binding a DataSet to a Web Forms DataGrid
» Typed DataSets ADO.NET: Developing Database Applications
» Reading Data Using a DataReader
Show more