Menus are one of the most common and characteristic elements of the windows user interface. Even in the old days of character based displays, menus were used to display methodically organized choices and guide the user through an application. Despite the visually rich interfaces window application and the many alternatives, menus are still the most popular menus of organizing a large number of options. Manu applications duplicate some or all of their menus in the form of icons on a toolbar, but the menu is a standard fixture of a form. You cane turn the toolbar on and off, but not the menus.
Menu Editor
Visual Basic provides menu editor, which helps to create new menus, add new commands to existing menus, and change and delete existing menus. Menus can be attached only to forms, and you design them with the menu editor. To see how the menu editor works, start a new standard EXE project, and when form 1 appears in the design window, choose Tools> menu editor to open the menu editor. Alternatively you can click them menu editor button on the toolbar
- Form the Tools menu chooses Menu Editor.
Click the Menu Editor icon on the tool bar
This will open the menu Editor as in figure.
Figure: The Menu Editor.
While most menu control properties can be set using the menu editor, all menu properties are available in the properties window. The two most important properties for menu controls are.
- Name – This is the name used to refer the menu control form code
- Caption – This is the text that appears on the control
- Index – This is to create an array of menu commands
- Checked – Checked to indicate that they are on or unchecked to
Indicate that they are off.
- Enabled – This is to set the initial status of a command by checking
Or clearing the enable box.
- Visible -This is tom make a command invisible.
- Window -This is used to maintain a list of all open windows.
Menu control arrays
In the menu editor window add a menu option and set its index property to 0. You can then add commands with the same name and consecutive index values. At design time, you don’t have to add more than one option. One command with its index property set to 0 is adequate to create the menu control array. You can use this array’s name and an index value to add new options at runtime.
Figure show the RT menu application, which demonstrates how to add to and remove items from a menu at runtime.
Figure: The RT Menu Application
Initially, the form’s menu contains the following items:
File
Open
Save
Exit
Edit
Copy
Cut
Paste
Run time menu
Commands grouped in submenus are intended form the left. The last command’s name is Run time options and its index is 0. Once a command option is specified as a control array, you can easily add commands to the runtime options array with the load method. After all, menu commands are very similar to controls.
The two buttons at the bottom of the form add commands to and remove commands from the runtime menu. Each menu command is appended at the end of the menu, and the commands are removed from the bottom of the menu (the most recently added commands)
Pop up menu
A pop up menu is a floating menu that is displayed over a form. Independent of the menu bar. The items displayed on the pop-up menu depend on where the pointer is located when the right mouse button is pressed; therefore, pop-up menus are also called context menus. Pop-up menus provide an efficient method for accessing common, contextual commands. For example, if the right mouse button is clicked in a text box, a contextual menu would appear, as in figure. 16.4
Figure: 16.4 Popup menu.
Any menu that has at least one menu item can be displayed at run time as a pop-up menu. Popup menu method is used to displayed pop-up menus, and its syntax is as follows:
Popup menu menuname [flags [x[y[bold command]]]
Only the first argument is required, ad it is the menu’s name, as specified in the menu editor window. The other arguments are optional. The x and y arguments are the coordinates of a point on the form (or control) where the menu will be displayed. The flags argument defines the location behavior of a pop-up menu and can have one of the values. Shown below.
Location constants | Description |
Vb popup menu left align | Default. The specified x location defines the left edge of the |
pop-up menuVb popup menu center alignThe pop up menu is centered around the specified x locationVb popup menu right alignThe specified x location defines the right edge of the popup
menu
The following code displays a popup menu named menu File with its top border centered on the form when the user clicks the form with the right mouse button.
Private Sub Form_Mouse Up (bottom as integer, shift As_
integer, X as single, Y as single)
If button = 2 then ‘check if right mouse button was clicked
popup menu mnuEdit, vbpopup menu center align x, y
End if
End sub
|
The bold command argument
The bold command argument is used to specify the name of a menu control in the displayed pop-up menu, to appear in bold to emphasize the most frequently used menu item. For example, assume that there is a menu named as mnu menu and one of its items is explore name as item explore. The following popup menu statement.
Popupmenu mnu menu,,,, itm explore
Will display the popup menu as in Figure.
Figure: a popup menu with a bold item
Dynamic Data Exchange And Object Linking And Embedding
An important feature of Microsoft windows operating system is its ability for applications to share information. OLE is a means of communication, which gives application the power to directly use and manipulate other windows applications. OLE is an important windows topic available in Visual Basic. Any object that supports OLE can be linked. OLE specification permits the user to link and embed objects and also edit the object with in the container application.
Programmers are not historians. They don’t like to read lengthy histories about a specific technology; they just want to learn how to use it. But when talking about a specific technology as all encompassing as Active X, knowing a little bout its previous history is a must. Before you can understand where Active X is going, you first need to know where it came form.
Active X can trace it roots all the way back to Dynamic Data Exchange (DDE)a way of passing data and commands between applications. Thought DDE is still supported by Visual Basic to maintain backward compatibility, it is seldom used.
Out of DDE evolved OLE1.0 object linking embedding. True to its name OLE brought the ability to link objects to one another or to embed one object inside another. The classic example of using OLE is the work processor. Spreadsheet combination. A reference to a reference, the spreadsheet program that is used to edit the spreadsheet would be automatically loaded and changes could be made. In this way, the spreadsheet is linked to the word processing document. With OLE, the spreadsheet can also be embedded into the word processing document. A window that shows the contents of the spreadsheet would be contained within the document . change to the spreadsheet can then be made inside that window. The spreadsheet program is still used to make the changes, but this is transparent to the user. He or she never has to leave the world processor to make changes to the spreadsheet.
DDE and OLE 1.00 are technologies that were designed to change the way we use computers. They bridged the gab between application-centric use and data centric use. In application centric computing, to work on certain kind of file (such as word Processing document, a graphic image or a spreadsheet), you would call up whichever program was used to edit that kind of file. To work on a word processing document, you need only click on the document itself. The operating system then determines which application is associated with that file type and loads it, along with the document.
Although the concept of data centric computing sounds like it does nothing more than makes tings easier for the user, it goes far beyond that. It also facilitates the embedding of different object types within other objects. A web browser, for example, can display an HTML document that contains many different object types: images, sounds, videos, animation, and others. If the web browser does not natively support the object type, the external program (typically, a “plug-in” for Netscape browsers or an ActiveX control in internet explorer) that is associated with the object type can be used to display the object within the page. Again, this is transparent to the user.
Object Embedding
With this technique, you can insert an object from one application (the server application) into another application (the container application). The inserted object is a copy of the original and can be manipulated and stored separately and apart from the original object. For example, you can embed a range of cells from an excel worksheet in a worked document. To edit the cells you switch to excel by double clicking the embedded excel object if the container application supports in place editing, you will see the menus of the server application right in the container application.
Inserting a OLE object
An OLE object is an item that is exposed, or made available, by an OLE server application. OLE server applications expose different types (or classes) or objects. OLE objects are used in container applications. As figure illustrates, excel (the OLE server) can expose a worksheet (the OLE object) that can be inserted as it in a word document (the container application).
Figure: in word the Excel sheet is inserted
OLE Automation
The method allows you to programmatically manipulate objects exposed by another application form within your Visual Basic applications. It’s also a standard that defines how code is shard between applications and how applications can be controlled form within other is applications. For example, when you copy a range of cells from an excel spreadsheet onto a word document, you embed the range of cells. With OLE automation, you application can request that excel perform some calculations and return the result to word. For example, you can pass a table to excel and request that excel manipulate the numeric data in ways that are not possible with work’s tools and then return the processed data to word.