Lesson 3 - PowerPoint PPT Presentation

1 / 49
About This Presentation
Title:

Lesson 3

Description:

Microsoft Visual Basic .NET, Introduction to Programming ... Murphy's law here is, 'If there is one thing that would crash a program, users will find it. ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 50
Provided by: michaelw
Learn more at: http://www.lschs.org
Category:
Tags: crash | lesson | net

less

Transcript and Presenter's Notes

Title: Lesson 3


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

2
Objectives
  • Design a menu bar with menus and submenus.
  • Plan and document an application.
  • Use MDI forms.
  • Use For loops to write programs.

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

4
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.

5
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?

6
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.

7
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."

8
The MainMenu Tool
9
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.

10
Creating a Menu in a Windows Application
11
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.

12
Menu Building in Progress
13
Tip
  • To reorder a menu, click and drag the item to a
    new position. The other items will make room as
    you drag.

14
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.

15
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 CtrlC for Copy and CtrlV
    for Paste. You cannot assign a shortcut key to a
    menu title.

16
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.

17
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
    CtrlO. 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.

18
Menu Changes in Progress
19
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.

20
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 items Code window is
    to double-click the menu item in the MainMenu
    tool.

21
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

22
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

23
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

24
Running a Program
  • Run the program by selecting Debug Start
    Without Debugging or pressing CtrlF5.

25
Colors
  • Visual Basics 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.

26
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.

27
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.

28
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."

29
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 forms
    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.

30
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.

31
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()

32
Cascaded Child Forms
33
Child Forms Tiled Horizontally
34
Child Forms Tiled Vertically
35
Child Form Icons Arranged on the Parent Form
36
Step-by-Step 3.6
  • Form1.LayoutMDI(MDILayout.Cascade)
  • Form1.LayoutMDI(MDILayout.TileHorizontal)
  • Form1.LayoutMDI(MDILayout.TileVertical)
  • Form1.LayoutMDI(MDILayout.ArrangeIcons)

37
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().

38
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 forms 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.

39
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.

40
For loop
  • The syntax of the For loop is
  • For control variable starting value To upper
    limit Step increment
  • body of the loop
  • Next
  • An example is
  • For x 1 To 100 Step 2
  • 'The statements that make up the body of the
    loop go here.
  • Next x

41
Examples
  • For x LowerLimit To 5 LowerLimit
  • Next x
  • For Item 1 to 25
  • Next Item

42
Examples
  • For x -5 To 5 Step 0.01
  • Next x
  • For Pounds 8 to 24
  • Time Pounds 17
  • Next Pounds

43
Backward
  • Loops can count forward or backward.
  • For x 10 To 1 Step -1
  • Next x

44
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.

45
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

46
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

47
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.

48
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.

49
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.
Write a Comment
User Comments (0)
About PowerShow.com