490 likes | 599 Views
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 Definite loop
E N D
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 • Definite loop • Indefinite loop • Loop • MainMenu tool • Multiple document interface (MDI) • Separator bar • Shortcut key • Tile
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.
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?
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 keysthat allow the user to quickly access them through the keyboard. • Often, menu items also have shortcut keycombinations that let the user execute the command without using the mouse.
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."
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.
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.
To reorder a menu, click and drag the item to a new position. The other items will make room as you drag. Tip
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. Access Keys
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.
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.
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.
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.
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.
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
'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 Step-by-Step 3.4
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
Running a Program Run the program by selecting Debug | Start Without Debugging or pressing Ctrl+F5.
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.
Multiple Document Interface (MDI) The multiple document interface (MDI)isanother 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.
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.
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."
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.
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.
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()
Step-by-Step 3.6 Form1.LayoutMDI(MDILayout.Cascade) Form1.LayoutMDI(MDILayout.TileHorizontal) Form1.LayoutMDI(MDILayout.TileVertical) Form1.LayoutMDI(MDILayout.ArrangeIcons)
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().
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.
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.
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
Examples For x = LowerLimit To 5 * LowerLimit … Next x For Item = 1 to 25 … Next Item
Examples For x = ‑5 To 5 Step 0.01 … Next x For Pounds = 8 to 24 Time = Pounds * 17 … Next Pounds
Backward Loops can count forward or backward. For x = 10 To 1 Step ‑1 … Next x
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.
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
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
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.
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.
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.