lesson 3 — menus, mdis, and simple loops

49
Lesson 3 — Menus, MDIs, and Simple Loops Microsoft Visual Basic .NET, Introduction to Programming

Upload: eleanor-hanson

Post on 03-Jan-2016

20 views

Category:

Documents


0 download

DESCRIPTION

Lesson 3 — Menus, MDIs, and Simple Loops. Microsoft Visual Basic .NET, Introduction to Programming. Objectives. Design a menu bar with menus and submenus . Plan and document an application. Use MDI forms. Use For loops to write programs. Vocabulary. Access keys Cascade - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lesson 3 — Menus, MDIs, and Simple Loops

Lesson 3 — Menus, MDIs, and Simple Loops

Microsoft Visual Basic .NET, Introduction to Programming

Page 2: Lesson 3 — Menus, MDIs, and Simple Loops

Objectives

Design a menu bar with menus and submenus.

Plan and document an application. Use MDI forms. Use For loops to write programs.

Page 3: Lesson 3 — Menus, MDIs, and Simple Loops

Vocabulary

Access keys Cascade Definite loop Indefinite loop Loop MainMenu tool Multiple document interface (MDI) Separator bar Shortcut key Tile

Page 4: Lesson 3 — Menus, MDIs, and Simple Loops

Menus

Menus are a common Windows tool that make it easy to access commonly used commands. It is not hard to imagine a Windows application with no menu bar: all of your applications to this point have used buttons. Menus are a more natural interface than buttons because they allow you to group related actions. Therefore, menus are an important part of most Windows applications. The menu bar is found at the top of the application window directly under the title bar.

Visual Basic lets you create menus quickly and easily.

Page 5: Lesson 3 — Menus, MDIs, and Simple Loops

Planning a Menu

To what commands and options do users need access?

In what order should the commands and options appear?

What forms are required? What information needs to be included on each

form? From where will this information come? Do the forms need to transfer information

elsewhere?

Page 6: Lesson 3 — Menus, MDIs, and Simple Loops

Basic Elements of a Menu

Menus are made up of the menu bar, menu titles, menu commands, submenu titles, and submenu commands.

Menu titles appear on the menu bar at the top of the form. When you click a menu title, its menu opens and displays a list of menu commands or submenu titles.

Menu items usually have associated access keys that allow the user to quickly access them through the keyboard.

Often, menu items also have shortcut key combinations that let the user execute the command without using the mouse.

Page 7: Lesson 3 — Menus, MDIs, and Simple Loops

Basic Conventions

Start all menu titles and menu commands with a capital letter.

File and Edit are usually the first menu titles on a menu bar, and Help is usually the last one.

Use short, specific captions, preferably no more than two words each.

Start the names of all menu items with the three‑letter prefix "mnu."

Page 8: Lesson 3 — Menus, MDIs, and Simple Loops

The MainMenu Tool

Page 9: Lesson 3 — Menus, MDIs, and Simple Loops

Did You Know?

In the early days of computers, the industry almost died because of a lack of programmers. In the late 1950s and early 1960s, programmers used assembly language, which is the obscure code-like language one level above machine language, or binary. It quickly became apparent that if something did not happen to make programming easier, the industry was doomed. To fill this need, high‑level languages, more easily understood by humans than computers, were developed. The computers helped themselves interpret these new languages.

Page 10: Lesson 3 — Menus, MDIs, and Simple Loops

Creating a Menu in a Windows Application

Page 11: Lesson 3 — Menus, MDIs, and Simple Loops

Note

There are a number of properties of the menu item shown in the Properties window:

The Checked property, when True, displays a check mark in front of the menu item. This is often used to show that the menu item has already been selected. This property can be set to True or False by program code.

The Enabled property, when False, displays the item grayed out, so the user cannot select it.

The Visible property, when False, makes the menu item invisible to the user.

The Text property reflects the text entered in the Type Here box on the form.

Page 12: Lesson 3 — Menus, MDIs, and Simple Loops

Menu Building in Progress

Page 13: Lesson 3 — Menus, MDIs, and Simple Loops

Tip

To reorder a menu, click and drag the item to a new position. The other items will make room as you drag.

Page 14: Lesson 3 — Menus, MDIs, and Simple Loops

Access Keys

You are probably familiar with access keys and shortcut keys. Access keys allow you to open a menu by pressing the Alt key and then the designated letter. Access keys also allow the user to access a menu command once the menu is open by continuing to hold the Alt key and pressing its designated letter.

Page 15: Lesson 3 — Menus, MDIs, and Simple Loops

Shortcuts

Shortcut keys are different. They run a menu command immediately after the shortcut key sequence is pressed. Shortcut keys with which you may be familiar are Ctrl+C for Copy and Ctrl+V for Paste. You cannot assign a shortcut key to a menu title.

Page 16: Lesson 3 — Menus, MDIs, and Simple Loops

Step-by-Step 3.3

Select the Open entry by clicking it. Click again to edit the entry. Position the cursor with the arrows and key an ampersand in front of the word Open. This makes the letter O the special access key.

Page 17: Lesson 3 — Menus, MDIs, and Simple Loops

Step-by-Step 3.3

In the Properties window, click the Shortcut property. Click the list arrow to the right of the entry and select CtrlO from the list. This sets the shortcut for this menu command to Ctrl+O. With the shortcut set, the user can execute this command from the keyboard without using the mouse or the access keys to select and open the menu.

Page 18: Lesson 3 — Menus, MDIs, and Simple Loops

Menu Changes in Progress

Page 19: Lesson 3 — Menus, MDIs, and Simple Loops

Tip

Remember to name your objects as you create them. Use short meaningful names that start with the correct prefix. For menu items, use the mnu prefix.

Page 20: Lesson 3 — Menus, MDIs, and Simple Loops

Adding Code to a Menu

When a user clicks a menu control, a Click event occurs. All menu controls, with the exception of the separator bar, recognize the Click event. Coding for a menu Click event works the same as coding for any other event procedure. You open the Code window, choose the menu control from the Class Name box on the left, and enter the code between the Protected Sub and End Sub statements.

Another way to open a menu item’s Code window is to double-click the menu item in the MainMenu tool.

Page 21: Lesson 3 — Menus, MDIs, and Simple Loops

Step-by-Step 3.4

'Change the color of the text in txtMenuTest to Black Dim NewColor As New Color() txtMenuTest.ForeColor = NewColor.Black() 'Check the Black submenu control mnuEditColorBlack.Checked = True 'Uncheck the Blue submenu control mnuEditColorBlue.Checked = False 'Uncheck the Red submenu control mnuEditColorRed.Checked = False

Page 22: Lesson 3 — Menus, MDIs, and Simple Loops

Step-by-Step 3.4

'Change the color of the text in txtMenuTest to Blue Dim NewColor As New Color() txtMenuTest.ForeColor = NewColor.Blue 'UnCheck the Black submenu control mnuEditColorBlack.Checked = False 'Check the Blue submenu control mnuEditColorBlue.Checked = True 'Uncheck the Red submenu control mnuEditColorRed.Checked = False

Page 23: Lesson 3 — Menus, MDIs, and Simple Loops

Step-by-Step 3.4

'Change the color of the text in txtMenuTest to Red Dim NewColor As New Color() txtMenuTest.ForeColor = NewColor.Red 'Uncheck the Black submenu control mnuEditColorBlack.Checked = False 'Uncheck the Blue submenu control mnuEditColorBlue.Checked = False 'Check the Red submenu control mnuEditColorRed.Checked = True

Page 24: Lesson 3 — Menus, MDIs, and Simple Loops

Running a Program

Run the program by selecting Debug | Start Without Debugging or pressing Ctrl+F5.

Page 25: Lesson 3 — Menus, MDIs, and Simple Loops

Colors

Visual Basic’s handling of colors is very convenient for the programmer. When you click the list arrow in the ForeColor property in the Properties window, you get to choose between three different color management systems: you may select a color from among System Colors, custom colors from a palette of colors, or one of many predefined Web colors.

Page 26: Lesson 3 — Menus, MDIs, and Simple Loops

Multiple Document Interface (MDI)

The multiple document interface (MDI) is another common item found in Windows applications. The MDI allows you to create an application that has many forms that are contained within one main form. Applications like Microsoft Word, where you can open several different documents within the main Word program, are MDIs.

The forms that make up an MDI (em-dee-eye) project are referred to as parent and child forms. The main application is referred to as the parent, and all of the forms contained within the application are called the children.

Page 27: Lesson 3 — Menus, MDIs, and Simple Loops

Parents and Children

There can be only one MDI parent form per application. This cuts down on the possible confusion that could result from having more than one form in charge of the rest. You create an MDI parent form by setting the appropriate property of the form in the Properties window. When you create an MDI form, the Properties window refers to it specifically as an MDI form rather than just a form. To have the application recognize that the other forms are children of the MDI form, you set their MDIChild property to True.

Page 28: Lesson 3 — Menus, MDIs, and Simple Loops

Programming Skills

A major problem that programmers run into is that they tend to design for their own use. It may seem obvious to you or another programmer how a program should process data and how a user accesses and enters that data. However, the end‑user typically does not have the same perspective. Therefore, when designing a program, it is essential that you plan for alternate approaches. Murphy's law here is, “If there is one thing that would crash a program, users will find it."

Page 29: Lesson 3 — Menus, MDIs, and Simple Loops

Creating an MDI Application

In order to create an MDI application, you must create an MDI form. Creating an MDI form is similar to creating any other new form. You populate the form with controls from the Toolbox and build a menu. Then change the form’s IsMDIContainer property to True.

After the MDI container or parent form is created, you create a template from which child forms are created. You add a second form to the project and populate it with controls from the Toolbox and provide a menu. This second form is the template used to create child forms while the application is running.

Page 30: Lesson 3 — Menus, MDIs, and Simple Loops

Me

While the application is running, the parent form can be referred to by the name Me. This is a special variable in Visual Basic. Me holds the name of the form that is currently active. Thus, the Me takes different values depending on the active form.

Page 31: Lesson 3 — Menus, MDIs, and Simple Loops

Step-by-Step 3.5

'Create a new instance (copy) of Form2

Dim NewMDIChild As New Form2()

'Designate the Child form's parent

NewMDIChild.MDIParent = Me

'Display the new form.

NewMDIChild.Show()

Page 32: Lesson 3 — Menus, MDIs, and Simple Loops

Cascaded Child Forms

Page 33: Lesson 3 — Menus, MDIs, and Simple Loops

Child Forms Tiled Horizontally

Page 34: Lesson 3 — Menus, MDIs, and Simple Loops

Child Forms Tiled Vertically

Page 35: Lesson 3 — Menus, MDIs, and Simple Loops

Child Form Icons Arranged on the Parent Form

Page 36: Lesson 3 — Menus, MDIs, and Simple Loops

Step-by-Step 3.6

Form1.LayoutMDI(MDILayout.Cascade)

Form1.LayoutMDI(MDILayout.TileHorizontal)

Form1.LayoutMDI(MDILayout.TileVertical)

Form1.LayoutMDI(MDILayout.ArrangeIcons)

Page 37: Lesson 3 — Menus, MDIs, and Simple Loops

Adding a Form

A unique form is created in the design phase of the project. Even though you work with the form in the design phase, the form must be called into existence by program code. If the name of the added form is About.vb, the code to create a new instance of the form is

Dim NewAbout As New About().

Page 38: Lesson 3 — Menus, MDIs, and Simple Loops

The New Form

Once a new form is added, the Show method of the form is called to display it. The Hide method hides the form without unloading it from memory. If necessary, the form’s Unload method is used to unload the form from memory. Unlike the child forms that appear wholly contained within the parent form, the new form is displayed independently.

Page 39: Lesson 3 — Menus, MDIs, and Simple Loops

Simple Loops

Executing the same statements over and over at almost inconceivable speeds is what makes computers so powerful. Visual Basic has a number of statements designed to control this repetition.

The first is the For loop. A loop is any program statement that causes the repetition of a group of statements. This is called a definite loop because its starting value, the upper limit, and the number of repetitions is generally known before the loop begins.

Page 40: Lesson 3 — Menus, MDIs, and Simple Loops

For loop

The syntax of the For loop is:

For control variable = starting value To upper limit Step increment body of the loopNext An example is:

For x = 1 To 100 Step 2'The statements that make up the body of the loop go here.

Next x

Page 41: Lesson 3 — Menus, MDIs, and Simple Loops

Examples

For x = LowerLimit To 5 * LowerLimit

Next x

For Item = 1 to 25

Next Item

Page 42: Lesson 3 — Menus, MDIs, and Simple Loops

Examples

For x = ‑5 To 5 Step 0.01

Next x

For Pounds = 8 to 24

Time = Pounds * 17

Next Pounds

Page 43: Lesson 3 — Menus, MDIs, and Simple Loops

Backward

Loops can count forward or backward.

For x = 10 To 1 Step ‑1

Next x

Page 44: Lesson 3 — Menus, MDIs, and Simple Loops

Important

The value of the control variable should not be changed in the body of a For loop. The value is controlled by the For loop itself.

Page 45: Lesson 3 — Menus, MDIs, and Simple Loops

Step-by-Step 3.8

'Declare the necessary variables.

Dim StartingValue, UpperLimit, Increment As Integer

Dim ControlVariable As Integer

Dim DirectCost, FixedCost, TotalCost As Decimal

'Collect information from the TextBoxes.

DirectCost = txtDirectCost.Text.ToDecimal

FixedCost = txtFixedCost.Text.ToDecimal

Page 46: Lesson 3 — Menus, MDIs, and Simple Loops

Step-by-Step 3.8

'Collect information from the user.StartingValue = CInt(InputBox("Enter the starting value:"))UpperLimit = CInt(InputBox("Enter the upper limit:"))Increment = CInt(InputBox("Enter the increment:"))

For ControlVariable = StartingValue To UpperLimit Step Increment TotalCost = DirectCost * ControlVariable + FixedCost MessageBox.Show("The cost at " & ControlVariable.ToString _ & " items is:" & CrLf & TotalCost.ToString)Next ControlVariable

Page 47: Lesson 3 — Menus, MDIs, and Simple Loops

Communication Skills

In creating and editing programs, you will often forget why you did things a certain way or why a particular "fix" was used. That is why it is essential that you document your programs with explanations and dates for later reference. You may have already noticed that you have been using internal comments. This helps later when revisions are made and when the final documentation is done.

Page 48: Lesson 3 — Menus, MDIs, and Simple Loops

Summary

Menus make it easier for users to execute commands in their programs.

The MainMenu tool makes creating professional‑looking menus easy.

Shortcut keys and access keys help speed up access to commonly used menu commands.

The multiple document interface is made up of one parent form that contains many child forms.

Page 49: Lesson 3 — Menus, MDIs, and Simple Loops

Summary

Previously created forms can be added from other projects.

The MDILayout method provides flexibility to the user to display child forms in different ways.

The For statement is used to build definite loops that repeat statements a fixed number of times.