VB - Unit Iii
VB - Unit Iii
CONTROL ARRAY
If we would have used Visual Basic for long time, we would have seen the message that appears
when using copy and paste to duplicate controls. Visual Basic asks, “You already have a control named
'command1'. Do you want to create a control array?”. If we choose Yes and we would have two controls
with the same name on the form. They are only distinguished by an index number.
LIST BOX
The list box is one of the most useful and powerful Visual Basic control. When you want to
present choices to the user, a list is more flexible than a mw of checkboxes or option buttons, because the
number of items in the list can vary from one or two to many thousands. Database like address books or
business records often use lists to present the information.
Example 1
Visual Basic automatically adds vertical scroll bars whenever the list box is too small for all the
items it contains,
Style - to change the style of the list box (i.e. Standard, Check Box)
Events: List boxes responds to 12 events like KeyDown, KeyUp, KeyPress, Click, MouseDown,
MouseUp, etc.,
COMBO BOX
A combo box is a one-line text box combined with a list bet. The advantage over a
normal text box is that the most common choices can be presented without any need to type them in. The
advantage over a list box is that the combo box takes less space. Combo boxes also allow the user to type
in a choice that is not on the list.
Example
Here is a person’s title usually this s one of a few common options Mr., Mrs., Ms., etc., or d like
Princess or President for which combo box is an ideal solution.
Additem To A ComboBox
Double click the form to open its Load event. Use the AddItem method to add items to the
combo box.
For Example
Private Sub Form_Load()
Combo1.AddItem ("Blue")
Combo1.Additcm ("Green")
Combo1.Additcm ("Red")End Sub
Set the Combo Box’s sorted property to True, so that items in the list will be sorted
alphabetically.
Removing Item From Combo Box
To remove an item from the list box select the item and use ComboName.RemoveItem index
during run-time specify the index number to be removed from the list. We can use clear method to clear
the list box (comboName.Clear) will clear the entire list.
FLEXGRID CONTROL
The Microsoft Hierarchical FlexGrid (MSHFlexGrid) control displays and operates on tabular
data. It allows complete flexibility to sort, merge, and format tables containing strings and pictures. When
bound to a data control, MSHFlexGrid displays read-only data. A flex grid is one step up from a list box.
Instead of just a list of items, the flex gird contains cell. Each cell can contain text, a picture or hot Flex
grids can have scroll bars. So you can fill more cell than w fit in the visible area.
The flex grid is an ActiveX Control. If it is not already in your toolbox, we have to bring it from
components dialog. For these choose components from Popup Menu. In that choose Microsoft Flex Grid
Control 6.0 and click OK to bring this tool into tool box which looks yellowish colour. The size of the
flex grid is depends on the users need. The property Cols and Rows specify the size of the grid.
Example
GridName.Cols = Number
Grid.Name.Rows = Number
ColWidth - Returns or sets the width of the column in the specified band, in twips. This property is not
available at design time.
twip
A screen-independent unit used to ensure that placement and proportion of screen
elements in your screen application are the same on all display systems. A twip is a unit of screen
measurement equal to 1/20 of a printer's point. There are approximately 1440 twips to a logical
inch or 567 twips to a logical centimeter (the length of a screen item measuring one inch or one
centimeter when printed).
Properties of MSFlexGrid
RowHeight - Returns or sets the height of the specified row, in twips. This property is not available at
design time.
TextMatrix - (Row As Long,Col As Long) As String. The property TextMatrix is used to add
values into the flex grid. Syntax is TextMatrix(Row As Long,Col As Long) As String
FixedCols - sets the total number of fixed (non - scrollable) rows for a Flex Grid.
FixedRows - sets the total number of fixed (non - scrollable)rows for a Flex Grid
The flex grid uses Fixed Row and fixed Cols to display information at all times regardless of how
the user scrolls through spreadsheet or we may want to display the column heading. 'This is shown in gray
color there can be any number of fixed row and columns.
FixedAlignment - Returns or sets the alignment of data in the fixed cells of a column in a
MSFlexGrid.
Cols - determine the total number of columns in a grid.
ColSel - Returns or sets the start or end column for a range of cells
RowSel - Returns or sets the start or end row for a range of cells.
Settings
LeaveCell - action to be taken when cursor leaves the cell action on changes from row to column or
vice versa selection changes activated.
RowColChange
SelChange
ColPosition - Occurs when the currently active cell changes to a different cell.
RowPosition - Returns the distance, in twips, between the upper-left corner of the MSFlexGrid and the
upper-left corner of a specified row.
DETERMINATE LOOPS
For... Loop
Syntax
For counter = start To end [Step step]
[statements]
[Exit For]
[statements]
Next [counter]
Part Description
counter Required. Numeric variable used as a loop counter. The variable can't be a
Boolean or an array element.
start Required. Initial value of counter.
end Required. Final value of counter.
step Optional. Amount counter is changed each time through the loop. If not
specified, step defaults to one.
statements Optional. One or more statements between For and Next that are executed the
specified number of times.
Remarks : The step argument can be either positive or negative. The value of the step argument
determines loop processing as follows:
Value Loop executes if
After all statements in the loop have executed, step is added to
Positive or 0 counter <= end counter. At this point, either the statements in the loop execute
Negative counter >= end
again (based on the same test that caused the loop to execute initially), or the loop is exited and execution
continues with the statement following the Next statement.
Tip Changing the value of counter while inside a loop can make it more difficult to read and debug your
code.
Any number of Exit For statements may be placed anywhere in the loop as an alternate
way to exit. Exit For is often used after evaluating of some condition, for example If...Then, and transfers
control to the statement immediately following Next
Flow Chart
Is counter Yes
variable
greater
than
ending
value
No
Body of the Loop
Increment Variable
Dim I As Integer
For I = 1 To 10 Step 1
Print I
Next I
Where For, To, Next and Step are key words. The starting value is set to 1 for the counter
variable I and ending value is set to 10.
1. At first VB sets the counter variable to the starting value.
2. Then it checks whether the counter variable is < or = to the ending value.
3. If the counter variable is greater than the ending value it does nothing and the control moves after the
Next Keyword.
4. If the counter variable is less than the ending value it executes the body of the loop.
5. After seen the Next Key word it increase 1 to the counter variable’s value.
6. Then the control comes to process 2 and checks the counter variable is greater then the ending value or
not. This process will continue up to the counter variable greater than the ending value. It exceeds the
control moves after the keyword Next.
Step: Step is a keyword; it mentions the Amount of counter is changed each time through the loop. If
not specified, step defaults to one.
Example
To calculate the sum Even Numbers, then Step can be increased as 2.
For I = 1 to 10 Step 2
Evensum = Evensum + I
Next I
Print Evensum
For Execution of the For Loop from highest value to owest value
For I = 10 to 1 Step -1
Print I
Next I
In the above example the counter variable is initialized to 10 and the ending value is 1. The
counter variable starts from 10 and decreased by 1 after seen the Next keyword.