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.
No comments:
Post a Comment