Monday, March 21, 2011

Chapter 7 Summary

7.1 Multiple Forms
-We've only dealt with using one form so far. You can use several forms within a program. The StartUp form is the one form that is automatically displayed when the program executes.

Form Files/Form Names
-Name is stored in form's Name property. When a form is created, the code associated with the form is stored in a file that has the same name followed by a .vb extension. This is in the Solutions Explorer window.

-To rename an existing form, you can do two things. If you change the form's file name within the Solution Explorer window, it'll also rename the form. However, if you just change the form name in the Name property, the form's file name doesn't change automatically.

-To add a new form to a project, click Project on the menu bar followed by Add Windows Form...

-To switch between forms during design time, double-click on the form's entry in the Solution Explorer window.

-You can designate the StartUp form in the Solution Explorer window by going to each form property.

-To remove a form/delete the file, right click/delete in Solution Explorer
   -If you just want to remove a form from the current project but don't want to delete the actual file,
      -right click the form in Solution Explorer/press exclude from project

-A form's class declaration (Public Class formname End Class) doesn't create a form, but is merely a blue-print.
To display a form, create an instance of the form:
   -Dim ObjectVAriable As New ClassName
Example:
Dim frmError As New ErrorForm
-frmError is the variable that is declared.
-ErrorForm is the instance of the ErrorForm form that is created in memory.
NOTE: This statement doesn't cause the form to be displayed on the screen, it only creates an instance of the form in memory and assigns its address to the variable declared. You need to use the object variable to execute form methods.

Displaying a Form
-Forms can either be modal or modeless.
   -Modal: when a modal form is displayed, no other form in the capplication can receive focus until this form is closed. User must close this form before they can interact with other forms.
   -Modeless: allows user to switch focus to another form while it is displayed.
ShowDialog Method: causes form to be dsiplayed as a modal form.
Show Method: causes form to be displayed as a modeless form.

Examples: frmError.ShowDialog()-Modal; frmError.Show()-Modeless.

-To close a form, use the Close Method, like we've been using. Me.Close(). Causes the current executing form to close.

Hide Method: makes a form/control invisible, but it isn't removed from memory. Has same effect as setting visible property to FALSE. Use Me.Hide()

Load, Activated, FormClosing, and FormClosed Events
-Load Event: statements to be executed when the form initially loads.
-Activated Event: occurs when user switches focus to the form from another form/application.
-FormClosing Event: occurs when a form is in the process of closing, but before it has actually closed. You can use a FormClosing event handler to for instance ask the user if they really want to quit.
-FormClosed Event: occurs after a form has closed. Code to be executed immediately after a form has closed.

-To access controls on a different form, you can access them by calling them in code.
Example: Application has a form named "GreetingsForm"/on GreetingsForm there is a lbl called lblMessage.
Dim frmGreetings As New GreetingsForm
frmGreetings.lblMessage.Text="Good day!"
frmGreetings.ShowDialog()

This first creates an instance of GreetingsForm and assigns it to frmGreetings variable. the "frmGreetings." that precedes the lblMessage.Text command tells you that the form isn't the current form. The ShowDialog() will call the form's method to display the form on the screen.

Class-Level Variables
-Class level variables on one form are accessible anywhere in that form one form, but they are not accessible by default to statements outside the form file.
-To make a class-level variable available to methods outside the class, use the Public keyword.
-Remember, the AccessSpecifier is automatically set to "Public" if none is given. Remember to use the correct "Public" or "Private" keyword depending on your intention of use.

7.2 Modules
-A VB file that contains only code (procedures, functions, and declarations of variables/constants).
-These appear in the Solution Explorer when created, with a .vb extension.
-Ideal for calling the same function/procedure over several different forms.
-Format is:
Module ModuleName (this should contain "module" at the end of the name for easy reference)
   [Module Conters]
End Module

-To add a module, click Project and then Add Module.
-Variables declared inside a module, but not inside a procedure/function, is a module-level variable.
   -These are accessible to any procedure/function in the module.
   -If "Dim" or "Private" keywords are used, the variable is not accessible to statements outside the module.
   -If "Public" is used, it is accessible to statements outside the module. (known as a "global variable"

-You can add a module that you've already created to future projects by using the Project-Add Existing Item selection in the menu bar.

7.3 Menus
-Menu system: collection of commands organized in one or more drop-down menus.
-Menu designer: allows you to visually create a custom menu system for any form in an application.
See book example pg. 439 for the screenshots.

-Menu Names: each drop-down menu has a name.
-Menu Command: User selects a command by clicking it, entering access key, or shortcut key.
-Shortcut Key: key/combination of keys that cause a menu command to execute.
-Disabled menu command: cause a menu command to be disabled when you don't want user to select it.
-Checked menu command: usually one that turns an option on/off.
-Submenu:Some commands on a menu are actually names of submenus. These are characterized by a right arrow next to the command.
-Separator Bar: horizontal bar used to separate groups of commands.

MenuStrip Control
-To construct a menu, use this control.
   -Everytime you add an item to a menu, you create a ToolStripMenuItem object.
-Use the Menu Designer to create menu items.
-Every ToolStripMenuItem object you create in a menu is given the same default names like all other controls and can be changed in the properties window.

Explore the properties window for all the various things you can do with menus.

-To insert a menu item above an existing menu item, start the menu designer/right-click the existing menu item/select insert and then MenuItem.
-To delete a menu item, right click the menu item to delete, select delete.
-To rearrange menu items, simply click/drag.

-You don't need to write code to display a menu/submenu. If a menu item has a menu/submenu to display, VB will display it automatically.
-If a menu item doesn't have a menu/submenu to display, use a click event handler.
7.4 Building the high Adventure Travel Agency Price Quote Application
-See book, starting pg. 451.

Friday, March 18, 2011

Chapter 6 Summary

6.1 Procedures
-Collection of statements that performs a task.
-Unlike a function, procedures don't return a value.

Use the call method to call procedures.

Modularize: breaking apart code into smaller procedures.

Format:
[Access Specifier] Sub ProcedureName ([parameterList])
    [Statements]
End Sub

-When making a variable local to a procedure, that variable will be destroyed once the procedure executes. If you want to keep that variable, use "Static" instead of "Dim" when declaring the variable.

6.2 Passing Arguments to Procedures
-Values that are passed to a procedure to execute are called arguments.
ByVal: passing an argument using this means that it's only a copy of the argument, the original argument isn't changed from what the ByVal statement brings back.
ByRef: is the original argument, changes can be made.

Example of how to pass arguments:

Sub DisplayValue (ByVal intnumber As Integer)
   MessageBox.Show(intNumber.ToString())
End Sub

The name of the procedure is "DisplayValue". "intNumber" is the variable that is created to store the value that the code passes the argument to. If the code were to be:
DisplayValue(txtPhone.text); it'll pass whatever is in the text-box to the intNumber variable, eventually displaying a message box with the text-box.

-You can pass multiple arguements to a procedure, just make sure you have enough variables.

6.3 Functions
-A function returns a value to the part of the program that called the function.

Format:
[AccessSpecifier] Function FunctionName ([ParameterList]) As DataType
   [Statements]
Return Value
End Function

Example:
Function Sum(ByVal dblNum1 As Double, ByVal dblNum2 As Double) As Double
   Return dblNum1+dblNum2
End Function

In this function, you're declaring a Function name as "Sum". dblNum1/dblNum2 will be the variables to hold the user input. The "As Double" after the parantheses is the data type that the function will return the value as.

If the code was: dblTotal=Sum(dblVal1+dblVal2), it'll pass the dblVal1/dblVal2 variables to the dblNum1/dblNum2 variables in the function. It'll return a value, and Sum will assign that value to the dblTotal variable.

6.4 Debugging Continued
Step Into: When an application is break mode, step into causes the currently highlighted line to execute.
   -Activate by pressing F8, select Debug in the menu-Step Into, or click the Step Into button.

Step Over: It'll cause the currently highlighted line to execute like Step Into, but if the line contains a function/procedure, it'll execute the function/procedure without stopping through it's statements.
   -Activate by pressing Shit+F8, select Step Over from Debug, or press the Step Over button.

Step Out: Use this when single-stepping through a function/procedure if you want the remainder of the function/procedure to finish executing without single-stepping.
   -Activate by pressing Ctrl+Shift+F8, select Step Out from Debug, or press Step Out button.

6.5 Bagel/Coffee Price Calculator Application
See book starting pg. 381.

Saturday, March 12, 2011

Chapter 5 Summary

5.1 Input Boxes
-A simple/quick way to ask a user to enter data. An input box displays a message and allows the user to enter input into a text-box. The box also has Ok/Cancel buttons.
-Use the InputBox function:
InputBox(Prompt [, Title] [, Default] [, Xpos] [, Ypos])
-Title is a string that appears in the input box's title bar.
-Default is the string initially displayed in the text-box when it first displays.
-Xpos/Ypos determine where the input box will be displayed on the screen.
-Prompt is the text you designate to be in the input box as a label.

5.2 List Boxes
-Displays a list of items and also allows users to select the items in the list.
-When a list box contains more items than can be displayed, a scroll bar will appear in the list box.
Items Property:
-Entries in the list box are stored in the property named Items. You can store values in the Item Property using the String Collection Editor.
Items.Count Property:
-Use this to determine the number of items stored in the list box. When no items are in the Items property, the Items.Count property=0.
Item Indexing:
-The first item in a list box collection has the value of "0", like in a string. Always think of "n-1"  to get the last index value, where "n" is the actual number of items in the collection.

-To access the items in code, use something like: lstEmployees.Items (0), where 0 is the first object stored.
-If you want to assign an value in a list box to a variable, remember to convert it to the appropriate format (Using the CInt for an integer, .ToString( ) for string, etc).
-Use try and catch statements to handle exceptions caused by out of range values.

SelectedIndex Property:
-When the user selects an item in a list box, the index of the item is stored in the SelectedIndex property.
-When no item is selected, the SelectedIndex=-1.

SelectedItem Property:
-SelectedIndex contains index of the currently selected item. SelectedItem contains the item itself.
-Example: List box "lstFruit" contains Apples, pears, bananas. If the user has selected pears, the following statement copies the string pairs to variable strSelectedFruit:
   strSelectedFruit=lstFruit.SelectedItem.ToString()

Sorted Property:
-Use this to alphabetize the items in the Items property. This is set to False by default. Set it to True to alphabetize.

Items.Add Method:
-To store values in Item property in realtime, use Items.Add.
   ListBox.Items.Add (Item)
-List box=name of control
-Item=value to be added to Items property

Items.Insert Method:
-Insert an item at a specific position using the Items.Insert method.
   ListBox.Items.Insert(Index, Item)
-Listbox=name of control
-Index=specifies the position where Item is to be placed.
-Item=value to be added

Items.Remove/Items.Remove At Methods:
-Both methods remove one item from a list box Items property.
   Listbox.Items.Remove(Item)
   Listbox.Items.RemoveAt(Index)

Items.Clear Method:
-Erases all items in Items property.
   ListBox.Items.Clear()

See pg. 295 for more properies of collections

5.3  Do While Loop
-Loops, or repetition structure, causes one or more statements to repeat.

-Do While loop has two important parts: 1)Boolean expression that is tested for True or False value, and 2) statement or group of statements that is repeated as long as the Boolean Expression is true.
Format:
   Do While BooleanExpression
         Statement
         (More statements may follow)
   Loop

-You must add something to a loop so it may stop. This is where Counters come into play as they regularly increment/decrement a variable each time a loop repeats. This gives it a chance of finally stopping, or else it'd be an infinite loop.
Example:
   intCount=0
   Do While intCount<10
         lstOutput.Items.Add("Hello")
         intCount +=1      <--------This adds "1" to intCount every time these statements are looped.
   Loop

-Pretest Loop: The boolean expression is tested first. If it's true, the loop executes. Repeats until expression is false.
-Posttest Loop: The loop is executed first, and then the boolean expression is tested. If it's true, it repeats. If false, it stops.

Difference from Pretest/Posttest loops:
Dim intCount As Integer=100
Do While intCount<10
   Messagebox.Show("Hello World")
   intCount +=1
Loop       This is a pre-test.

Dim intCount As Integer=100
Do
   Messagebox.Show("Hello World")
   intCount+=1
Loop While intCount<10    This is a post-test.

-You can use loops to keep a running total. variables used as accumulators are used.

5.4 Do Until/For Next loops
-Do Until loops are basically the opposite of the Do While loops. For Do Until, it'll keep looping as long as the boolean expression is false and stops when the expression is true.

Do Until Boolean Expression
   Statement
Loop

-For Next loops is ideal for situations that require a counter. These initialize, test, and increment a counter variable.
   For Countervariable=StartValue to EndValue Step (Increment)
          Statement
   Next (CounterVariable)
Example:
For intCount=1 to 10
   MessageBox.Show("Hello")
Next

-The step value is the value add to the counter variable at the end of each loop in the For..Next loop.

5.5 Nested Loops
-A nested loop is a loop inside another loop. A clock is a good example of this, as each hand spins at a different rate.

Rules:
1) An inner loop goes through all iterations for each iteration an outer loop does.
2) Inner loops complete their loops before outer loops do.
3) To get total number of iterations of a nested loop, multiply the number of iterations of all the loops.

5.6 Multicolumn List Boxes, Checked List Boxes, and Combo Boxes
Multicolumn
-The listbox control itself has a MultiColumn property that can be set to true or false. Default is false. If you set it to true, it causes the list box to display its list in columns. You set the size of the columns, in pixels, with ColumnWidth property.

Checked
-Variation of the ListBox control.
-CheckOnClick property determines whether items become checked.
   -When set to false, user clicks an item once to select it, then clicks again to check it.
   -When set to true, the user clicks an item only once to both select/check.
-You can access selected items in checked list boxes the same as you would with a list box
    CheckedListBox.GetItemChecked(Index)

Combo Boxes
-Similar to list boxes
   -Both display list of items to user.
   -Both have Items,Items.Count, SelectedIndex, SelectedItem, and Sorted properties.
   -Both have Items.Add, Items.Clear, Items.Remove, and Items.RemoveAt methods.
   -All of these properties and methods work the same with combo/list boxes.

Different styles such as: Drop-Down, Simple, Drop-Down List

List boxes vs. Combo boxes
-Use a drop-down or simple combo box when you want to provide the user a list of items to select from but don't want to limit the user's input to items on the list.
-Use a list box or a drop-down list combo box when you want to limit the user's selection to a list of items.

5.7 Random Numbers
-Computers aren't capable of generating truly random numbers.
-Computers rely on carefully crafted formulas to generate pseudo-random numbers.
-To create random numbers, create a special type of object known as a Random object in memory.
   Example: Dim rand as New Random

The Next Method
-Once you created the Random object, call its Next method to get a random integer number.
   Example: Dim intNum as Integer
                 Dim rand as New Random
                 intNum=rand.Next()
   This will contain a random number in "intNum" when this code executes. This has a random range of 0-2.1 billion. You can specify an upper limit by putting for example "100" in the parantheses to limit it from 0-99. The random integer's range also doesn't have to start at zero.
    Example: intNum=rand.Next(10)+1
This statement will first have a range of 0-9, but the +1 will make the range 1-10 instead.

The NextDouble Method
-This will get a random floating-point number between 0.0 and 1.0 (not including 1.0).

5.8 Simplifying Code with the With...End With Statement
-Instead of writing:
txtName.Clear ()
txtName.Forecolor=Color.Blue
txtName.BackColor=Color.Yellow
etc, you can use With...End With to simplify that using
With txtName
   .Clear ()
   .ForeColor=Color.Blue
   .BackColor=Color.Yellow
etc.

5.9 ToolTips
-Small box displayed when the user holds the mouse cursor over a control. A short description is shown of the control.
-ToolTip Control allows you to create tooltips for other controls on the form.