0% found this document useful (0 votes)
72 views7 pages

VB - Unit Iii

The document discusses various Visual Basic controls including command buttons, list boxes, combo boxes, and flex grids. It provides examples of how to use each control, describing properties like AddItem and RemoveItem to populate lists, and TextMatrix to add values to flex grids. Control arrays are also mentioned as allowing multiple controls with the same name but different indices.

Uploaded by

Nalls
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views7 pages

VB - Unit Iii

The document discusses various Visual Basic controls including command buttons, list boxes, combo boxes, and flex grids. It provides examples of how to use each control, describing properties like AddItem and RemoveItem to populate lists, and TextMatrix to add values to flex grids. Control arrays are also mentioned as allowing multiple controls with the same name but different indices.

Uploaded by

Nalls
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

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.

Using the control array


• Place a command button on a form.
• Select the command button copy and paste it on the same form in another location.
• Paste the command again and again. So that all are caked in the same name as Command 1.
Even though the name of these command buttons is same they differ in the index part. When we write a
event procedure for these button all these buttons share the same code. So, to discover which button is
clicked, we use the index parameter.
Example:
The main reason for using the control arrays is that of for the coding efficiency.

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,

Add Item To A List Box


Double click the form to open its Load event. Use the Additem method to add items to the list
box.
Set the list box’s sorted property to True, so that items in the list will Be sorted alphabetically.
Removing Item from List Box
To remove an item from the list box select the item and use listName.RemoveItem(Index as
integer)Index during run-time specify the index number to be removed from the list. We can use clear
method to clear the list box ListName.Clear, We clear the entire list Properties of List Box to sort the
list and to add value into the list.

Private Sub Form_Load()


List1.AddItem ("Red")
List1.AddItem ("BIue")
List1.AddItem ("Green")
End Sub

ListCount - used to count the number of elements in the list.

ListIndex - gives the value. o accQsS ien-’c Vt o hs b

MultiSelect - to select multiple from list.

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.

Properties of Combo Box


Sorting - to sort the list in the Combo Box
AddItem - to add value into the list of the Combo Box
Style - to change the style of the Combo Box (i.e. Dropdown, Combo box, Dropdown
List box, Simple Combo box).
Events - Combo boxes responds to 12 events like KeyUp, KeyPress, KeyDown etc.,

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.

Rows - determine the total number of rows 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.

ColSel and RowSel properties are not available at design time.

Settings

The settings for value are:

Constant Value Description


flexAlignLeftTop 0 The cell content is aligned left, top.
flexAlignLeftCenter 1 The cell content is aligned left, center.
flexAlignLeftBottom 2 The cell content is aligned left, bottom.
flexAlignCenterTop 3 The cell content is aligned center, top.
flexAlignCenterCenter 4 The cell content is aligned center, center.
flexAlignCenterBottom 5 The cell content is aligned center, bottom.
flexAlignRightTop 6 The cell content is aligned right, top.
flexAlignRightCenter 7 The cell content is aligned right, center.
flexAlignRightBottom 8 The cell content is aligned right, bottom.

Events and Methods of MSFlexGrid


EnterCell - Occurs when the currently active cell changes to a different cell.

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.

CONTROLLING PROGRAM FLOW


The parts of a programming language that let you make repeated operations or making decisions
are often called control structures. These are
 Determinate Loops
 Indeterminate Loops
 Decisions Making Statements
 Select Case
 GoTo Statements

DETERMINATE LOOPS
For... Loop
Syntax
For counter = start To end [Step step]
[statements]
[Exit For]
[statements]
Next [counter]

The For…Next statement syntax has these parts:

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

Set Counter Variable to


starting value

Is counter Yes
variable
greater
than
ending
value

No
Body of the Loop

Increment Variable

Move to statements after body of the


Loop (after Next keyword)

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

Calculates only the sum of even numbers.

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.

Nested For Loop Statement


You can nest For...Next loops by placing one For...Next loop within another. Give each loop a
unique variable name as its counter. In this there may be any number of for loop can exist within the other.
Hem the inner loop is completed before the outer loop.
. The following construction is correct:
For I = 1 To 3
For J = 1 To 5
For K = 1 To 10
Print k
Next K
Next J
Next I
Here, in the above example the I loop executes three times. J loop executes 15 times. K loop
executes 150 times.

You might also like