Your SlideShare is downloading. ×
0
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Chapter 05
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Chapter 05

529

Published on

Published in: Sports, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
529
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
27
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Brief description on how to navigate within this presentation (ppt) The first time a Key Term from the chapter is used in the ppt it will display in blue Gold colored text boxes display coding examples Slides will be numbered (# of #) when multiple slides on same topic (Slide title) Speaker notes are included where appropriate for slides (*)Denotes either a comment for page reference to textbook or slide reference in ppt
  • Menus are used quite extensively while working with the computer
  • To create the menus simply type where the words " Type Here " appear at the top of the form Each time the text for a new menu is entered a ToolStripMenuItem is automatically added to the MenuStrip’s Items collection
  • When typing the words for a menu or menu item the Text property is being entered for the item
  • Another big improvement to VB 2005 menus is that the Menu Designer is now smart enough to give good names to the menu items
  • As new menus are created using the Menu Designer, each menu is added to the Items collection that belongs to the MenuStrip
  • The drop-down list of items below a menu name is called a menu When an item on the menu has another list of items that pops up, the new list is calla submenu
  • By default, all new menu items have their Enabled property set to True An enabled menu item appears in black test and is available for selection whereas the grayed out or disabled (key term) items are not available (False) A menu item may contain a check mark beside it (indicating that the item is checked—by default the Checked property is set to False Many computer users prefer to use keyboard shortcuts for selecting menu items
  • When applications are written to run under Windows, programs should follow the Windows standards Plan menus so that they look like other Windows programs
  • You have probably noticed that when you display a Windows dialog box it remains on top until you respond to it But in many applications you can display additional windows and switch back and forth between windows
  • You can also use the Font Dialog Box in much the same way and assign Font properties to the Front property of other objects on a form
  • When a common dialog box for colors or fonts appears, what color or font do you want to display? It’s best to assign initial values before showing the dialog box
  • Context menus can be added to applications Creating a context menu is similar to creating a menu
  • Often programming situations exist in which multiple procedures perform the same operation—this conidtion can occur when the user can select either a button or a menu item to do the same thing Rather than retyping the code reusable code in a general procedure can be written and call it from both event procedures
  • At time it may be necessary to use the value of a variable in one procedure and then again in a second procedure that is call from the first
  • VB.NET has choices other than Private for the access, such as Public, Friend, and Protected—Private can be used for all general procedures (key term)
  • When passing a value to a procedure it may be passed ByVal or ByRed
  • Programmers may need to calculate a value that will be needed in several different procedures or programs Functions also can be declared as Public, Protected, or Friend (introduced in later chapter)
  • The main difference between coding a function procedure and coding a sub procedure is that in a function procedure a return value must be set; the return value is palced in a variable that VB names with the same name as the function name
  • Transcript

    • 1. aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf
    • 2. Menus, Common Dialog Boxes, Sub Procedures and Function Procedures Chapter 5 McGraw-Hill © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
    • 3. Objectives <ul><li>Create menus and submenus for program control </li></ul><ul><li>Display and use the Windows common dialog boxes </li></ul><ul><li>Create context menus for controls and the form </li></ul><ul><li>Write reusable code in sub procedures and function procedures and call the procedures from other locations </li></ul>
    • 4. Menus <ul><li>Menu Bar </li></ul><ul><ul><li>Contains menus which drop down to display list of menu items </li></ul></ul><ul><ul><ul><li>Can be used in place of or in addition to buttons to execute a procedure </li></ul></ul></ul><ul><ul><ul><li>Menu items are controls with properties and events </li></ul></ul></ul><ul><li>Easy to create menus for a Windows form using the Visual Studio environment’s Menu Designer </li></ul><ul><li>Menus will look and behave like standard Windows menus </li></ul>
    • 5. Defining Menus (1 of 2) <ul><li>MenuStrip component is added to a form </li></ul><ul><li>MenuStrip is a container to which ToolStripMenu Items, ToolStripComboBoxes, ToolStripSeparators, and ToolStripTextBoxes can be added </li></ul>
    • 6. Defining Menus (2 of 2) The MenuStrip component appears in the component tray below the form and the Menu Designer allows you to begin typing the text for the menu items.
    • 7. The Text Property <ul><li>Holds the words that appear on the screen – like the Text property of a button or label </li></ul><ul><li>To conform Windows standards the first menu’s Text property should be File, with a keyboard access key </li></ul><ul><ul><li>Use the ampersand (&amp;) in the text to specify the key to use for keyboard access </li></ul></ul><ul><li>Enter and change the Text property for each menu and menu item using the Menu Designer or make the changes in the Text property using the Properties window </li></ul>
    • 8. The Name Property <ul><li>The File menu item that is added is automatically named FileToolStripMenuItem </li></ul><ul><li>The items are named so well that there won’t be a need to change the Name property of any menu component </li></ul><ul><ul><li>If the Text property is changed for any menu item, the item is not automatically renamed; it will need to be renamed </li></ul></ul>
    • 9. The MenuStrip Items Collection <ul><li>ToolStripMenu Items in the collection can be displayed, reordered, added, deleted using the Items Collection Editor </li></ul>
    • 10. Submenus <ul><li>Filled triangle to the right of the menu item indicates to the user the existence of a submenu </li></ul><ul><li>Create submenus by moving to the right of a menu item and typing the next item&apos;s text </li></ul>
    • 11. Separator Bars <ul><li>Used for grouping menu items according to their purpose </li></ul><ul><li>Visually represented as a bar across the menu </li></ul><ul><li>To create a separator bar , add a new menu item and click on its drop-down arrow </li></ul>
    • 12. Menu Properties <ul><li>Enabled property , True/False-can be set at design or run time </li></ul><ul><li>Checked property , False/True-can be set at design or run time </li></ul><ul><ul><li>Used to indicate that an option is selected </li></ul></ul><ul><li>Setting keyboard shortcuts </li></ul><ul><ul><li>Select the menu item and In Properties window for menu item, select the ShortcuKeyst property </li></ul></ul><ul><ul><li>Make choice from drop-down list </li></ul></ul>
    • 13. Standards for Windows Menus <ul><li>Follow Windows standards for applications </li></ul><ul><li>Include keyboard access keys </li></ul><ul><li>Use standards for shortcut keys, if used </li></ul><ul><li>Place the File menu at left end of menu bar and end File menu with the Exit command </li></ul><ul><li>Help, if included, is placed at right end of menu bar </li></ul>F ile E dit V iew Forma t H elp
    • 14. Common Dialog Boxes <ul><li>Predefined standard dialog boxes for: </li></ul><ul><ul><li>Specifying colors and fonts </li></ul></ul><ul><ul><li>Printing, opening, and saving </li></ul></ul><ul><li>Add appropriate Common Dialog components to display the dialog boxes that are provided as part of the Windows environment </li></ul><ul><ul><li>To use a common dialog component, add the component to the form, placing it in the component tray </li></ul></ul>
    • 15. Common Dialog Tools <ul><li>Pointer </li></ul><ul><li>ColorDialog </li></ul><ul><li>FontBrowserDialog </li></ul><ul><li>FontDialog </li></ul><ul><li>OpenFileDialog </li></ul><ul><li>SaveFileDialog </li></ul>
    • 16. Displaying a Windows Common Dialog Box <ul><li>Use ShowDialog method to display the common dialog box at run time </li></ul><ul><li>ShowDialog only displays the dialog </li></ul>ColorDialog1.ShowDialog( ) FontDialog1.ShowDialog( )
    • 17. Modal versus Modeless Windows <ul><li>A dialog box is said to be modal - means that it stays on top of the application and must be responded to </li></ul><ul><ul><li>Use the ShowDialog method to display a dialog box-it is a window displayed modally </li></ul></ul><ul><li>Modeless windows do not demand that there is a response </li></ul><ul><ul><li>Use the Show method to display a modeless window </li></ul></ul>
    • 18. Using the Information from the Dialog Box <ul><li>Code must be written to retrieve and use the choice made by the user in the common dialog box </li></ul><ul><li>Example </li></ul><ul><ul><li>Color Dialog displayed </li></ul></ul><ul><ul><li>User selects color and clicks OK-the selected color is stored in a property that can be accessed </li></ul></ul><ul><ul><li>Color that is selected is stored in the Color property and can be assigned to another object such as a control </li></ul></ul>titleLabel.BackColor = ColorDialog1.Color
    • 19. Setting Initial Values <ul><li>Before executing the ShowDialog method, assign the existing values of the object&apos;s properties that will be altered </li></ul><ul><li>When the dialog box appears, the current values will be selected </li></ul><ul><li>If the user presses Cancel, property settings for the objects will remain unchanged </li></ul>FontDialog1.Font = .subTotalLabel.Font or ColorDialog1.Color = .BackColor
    • 20. Creating Context Menus <ul><li>Shortcut menus that pop up when you right-click </li></ul><ul><li>Items are specific to the component to which user is pointing, reflecting options available for that component or situation </li></ul><ul><li>A ContextMenuStrip component is added and appears in the component tray below the form </li></ul><ul><li>A context menu does not have a top-level menu, only menu items </li></ul><ul><li>Application can have multiple context menus </li></ul>
    • 21. Writing General Procedures <ul><li>A general procedure is reusable code which can be called from multiple procedures </li></ul><ul><li>Useful for breaking down large sections of code into smaller units </li></ul><ul><li>Two types </li></ul><ul><ul><li>Sub Procedure performs actions </li></ul></ul><ul><ul><li>Function performs actions AND returns a value (the return value ) </li></ul></ul>
    • 22. Passing Arguments to Procedures <ul><li>Declare variable as local and pass to any called procedures – (can be module level but it makes the variable visible to all other procedures) </li></ul><ul><li>If a sub procedure names an argument, any call to the procedure must supply the argument </li></ul><ul><li>Name of the argument does not have to be the same in both locations </li></ul><ul><li>Number of arguments, sequence and data type must match </li></ul>
    • 23. Creating a New Sub Procedure <ul><li>In the Editor window enclose the lines of code with a set of Sub and End Sub statements </li></ul><ul><li>To use the Sub Procedure, call it from another procedure </li></ul><ul><li>Code in a Sub Procedure cannot be executed unless called from another procedure </li></ul>Private Sub ProcedureName ( ) &apos; Statements in the procedure. End Sub
    • 24. Sub Procedure Example Private Sub SelectColor(incomingColor As Color) With ColorDialog1 .Color = incomingColor .ShowDialog( ) End With End Sub Private Sub changeTitleButtonColor_Click( ) Dim originalColor As Color originalColor = titleLabel.ForeColor SelectColor(originalColor) titleLabel.ForeColor = ColorDialog1.Color End Sub Sub Procedure Calling Procedure
    • 25. Passing Arguments ByVal or ByRef <ul><li>ByVal -value </li></ul><ul><ul><li>Sends a copy of the argument’s value, original cannot be altered </li></ul></ul><ul><li>ByRef -reference </li></ul><ul><ul><li>Sends a reference to the memory location where the original is stored and therefore the procedure may change the argument’s original value can be altered </li></ul></ul><ul><li>If not specified arguments are passed by value </li></ul>
    • 26. Writing Function Procedures <ul><li>In the Editor window enclose the lines of code with Private Function( ) and End Function statements </li></ul><ul><li>Since the procedure returns a value a data type for the value must be specified </li></ul><ul><li>To use the Function, Call it by using it in an expression </li></ul><ul><li>Pass arguments ByVal or ByRef </li></ul>Private Function FunctionName ( ) As Datatype &apos; Statements to execute. End Function
    • 27. Returning the Result of a Function <ul><li>To return a value to the calling procedure set up a return value </li></ul><ul><li>The return value will be placed by VB in a variable with the SAME name as the Function&apos;s name </li></ul><ul><li>--OR-- </li></ul><ul><li>Use the Return statement to return the value </li></ul>
    • 28. Function Example Private Sub calculateButton_Click( ) Dim salesDecimal As Decimal salesDecimal = Decimal.Parse(salesTextBox.Text) commissionLabel.Text = Commission(salesDecimal.ToString( &amp;quot; C &amp;quot; )) End Sub Calling Procedure Private Function Commission(ByVal salesAmountDecimal As Decimal) _ As Decimal If salesAmountDecimal &lt; 100D Then Commission = 0D Else Commission = 0 . 15 * salesAmountDecimal End If End Function Function
    • 29. Functions with Multiple Arguments <ul><li>Functions can receive one or more arguments (values) </li></ul><ul><li>Sequence and data type of arguments in Call must exactly match arguments in function header </li></ul>Private Function Payment (ByVal rateDecimal As Decimal, _ ByVal timeDecimal As Decimal, ByVal amountDecimal _ As Decimal) As Decimal paymentLabel.Text = Payment (Decimal.Parse(rateTextBox.Text), _ Decimal.Parse(yearsTextBox.Text), _ Decimal.Parse(principalTextBox.Text)).ToString( ) End Function
    • 30. Breaking Calculations into Smaller Units <ul><li>Projects with many calculations are easier to understand and write if calculations are broken into small units </li></ul><ul><li>Each unit should perform one program function or logic block </li></ul>

    ×