Menus and Toolbars

Objectives


Discussion

Menus

ToolBar

Process

Back to top


Demonstration

1.  Create a new project.  Since we will use this project in the next lesson we will call it "Unit7 Editor" for future reference.  In the toolbox, drag a MainMenu control onto your form.  Notice that the control shows up in the region below the form and a menu is added to the top of the form.

2.  We will now add menu items to the menu.  In the menu, you should see Type Here.  Type &File in this location.  The "&" before the "F" is optional; it will make an "F" the HotKey for the menu item so that the user can choose the "File" item by typing "Alt-F" just like you can in VS.  Now click enter or the down arrow to enter another item.  Type &Open&Save, &Exit.  Then move to the right to enter &Notepad next to the File item.  When you are done your menu should look as shown below.  Notice the "&" does not show up and the appropriate letter is underlined.

3. You should change the name of each menu item. You can do this by clicking on a menu item and then changing the name in the Properties Window.  Making the name something meaningful such as mnuFile for File makes it easier to code and understand your program.

4.  Now we will make the Exit item work.  Double-click the Exit item so that you end up in the click event for this item in the code window.  Type "End" in the click event. Run and test your program.

5.  Now we will make the Notepad item work.  When the user clicks this item, Notepad should open.  We can open other programs by using Process.Start.  In the click event for the Notepad menu item add the following code:

Process.Start("notepad")

6. Run and test your program.  Hopefully Notepad opens when you click the Notepad item.

7.  We will now add a Toolbar. Drag a ToolBar from the toolbox and place it on the form. 

8.  To add buttons to the Toolbar, click on the Toolbar and select Buttons in the Properties Window.  When the ToolbarButton Collection Editor Window appears, click Add.  A new button is added.  Change the name to "tbExit" and the text to "Exit".  Now you can program the click event for this button.  Type "End" in the click event.  Run and test the program.

9.  If you want to add an image to the button face you need to add an ImageList control to the form.  Add an ImageList to your form.  We need to add images to the ImageList.  Click on the Images collection property for the ImageList in the Properties Window.  Once the Image Collection Editor Window appears, click the Add button to add an image. 

10.  Now we must link the ImageList to the Toolbar.  Click on the Toolbar and choose the ImageList property in the Properties Window.   Select the ImageList (it should be ImageList1 unless you changed the name of the ImageList).

11.  Now we will link the Toolbar button ImageIndex property to the index of the image that you added.  Click on the Buttons property for the Toolbar and set the ImageIndex property for the Exit button in the ToolBarButton Collection Editor Window.   

12. Test your program. There should be an image on the Exit button.

Back to top
Exercises

1.  Modify the program that you created in this demonstration by adding a new item in the Menu, mnuStart with the text property equal to "Start".  Then add several items under mnuStartmnuNotepad, mnuCalculator, and mnuPaint with appropriate text properties.  You can then delete the Notepad item that you had added in the last lesson.  Then make each menu item work by calling Process.Start to open the appropriate application (Notepad, msPaint, and Calc). 

2. Add items to the Toolbar to open the calculator, notepad, and paint as you did in #1. 

3.  Add images to the Toolbar for each button.  If you can't find the appropriate icon that is already associated with the application then create your own image.

4.  When you use Process.Start to open Notepad, you can have Notepad automatically open an existing file (or create the file if it does not exist).  How do you do this?  Modify your program so that it has the capability to let the user enter a filename for Notepad to open.

5.  You can add menus and menu items while a program is running.  How do you do this? (Search in Help).  Try it out.

6. Find in Help where it tells how to add icons to Toolbars in code.

Back to top
Links & Help
Back to top