The document discusses various ways to structure code in Visual Basic, including creating menus, common dialog boxes, context menus, sub procedures, and function procedures. Key points include:
- Menus can be created using the MenuStrip component and Menu Designer. Properties like Text and Name are used to define menu items.
- Common dialog boxes allow using standard Windows dialogs like for colors, fonts, and file operations. The ShowDialog method displays them modally.
- Context menus appear with right-clicks and are specific to components.
- Sub procedures contain reusable code that can be called from other locations. Arguments are passed ByVal or ByRef.
- Function procedures return values in addition to
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.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
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.
26.
27.
28. Function Example Private Sub calculateButton_Click( ) Dim salesDecimal As Decimal salesDecimal = Decimal.Parse(salesTextBox.Text) commissionLabel.Text = Commission(salesDecimal.ToString( " C " )) End Sub Calling Procedure Private Function Commission(ByVal salesAmountDecimal As Decimal) _ As Decimal If salesAmountDecimal < 100D Then Commission = 0D Else Commission = 0 . 15 * salesAmountDecimal End If End Function Function
29.
30.
Hinweis der Redaktion
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 &quot; Type Here &quot; 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