Академический Документы
Профессиональный Документы
Культура Документы
UNIT-1:
Attributes and event handling are associated with the controls; default values
are assigned to the controls while it is created and it is possible to change the
default values. This environment primarily allows to create a windows
application, additionally create executable (exe) files, ActiveX controls, and
DLL files. This environment provides a possibility to insert additional logic
based on the appropriate event handlers.
In the Visual Basic Environment preloaded forms are there; usage of these
forms will increase the responsiveness of the application. Usage of constructor
and destructor to set the value to variable will set when the classes get loaded.
Another type of SDI program is common these days. I call it the scatter effect
program. if the user chose the Training module while in the Employee screen,
the program would not replace the Employee screen. It would instead bring up
another form on the desktop, which would look like a separate program
altogether. However, it is not. The different screens can interact with each other
and exchange information.
In this scenario, the user can have four forms open on the desktop, one for each
module (Employee, Training, Payroll, and Scheduling). Can you imagine a
program that could have a dozen or more forms open on the desktop at once?
To me, this is chaos, but I bet you have already used a program that does this.
If you come from the MSVC++ world or you have not programmed yet, you
may not have seen this effect in VB 6.0. However, you do see it in Microsoft
Word. Word used to have an MDI interface. It used to be that you could open
up Word and then open as many documents as you wanted inside Word. Word
is arguably the most ubiquitous program in the world, and I bet many people
like to edit many documents at once. Word is now an SDI application, which
means that each document is in a separate window. In my opinion, all this does
is clutter up the taskbar.
This next example shows you quite a bit about how SDI programs work. It
includes five screens. There is a main screen that lets you choose to edit
different parts of an HR program.
Before you attack the coding of an SDI program, you need to come up with a
plan for how it will work. My plan for this one is based loosely on how
Microsoft Word works:
CONTROL DESCRIPTION
Project Explorer:-
1. Docked on the right side of the screen, just under the toolbar, is the
Project Explorer window.
2. The Project Explorer as shown in in figure servers as a quick
reference to the various elements of a project namely form, classes and
modules.
3. All of the object that makes up the application is packed in a
project.
4. A simple project will typically contain one form, which is a window
that is designed as part of a program's interface.
5. It is possible to develop any number of forms for use in a program,
although a program may consist of a single form.
6. In addition to forms, the Project Explorer window also lists code
modules and classes.
Properties Window:-
1. The Properties Window is docked under the Project Explorer
window.
2. The Properties Window exposes the various characteristics of
selected objects.
3. Each and every form in an application is considered an object.
4. Now, each object in Visual Basic has characteristics such as color
and size.
5. Other characteristics not only affect the appearance of the object but
the way it behaves too.
6. All these characteristics of an object are called its properties.
7. Thus, a form has properties and any controls placed on it will have
properties too.
8. All of these properties are displayed in the Properties Window.
Object Browser:-
1. The Object Browser allows us to browse through the various
properties, events and methods that are made available to us.
2. It is accessed by selecting Object Browser from the View menu or
pressing the key F2.
3. The left column of the Object Browser lists the objects and classes
that are available in the projects that are opened and the controls that have
been referenced in them.
4. It is possible for us to scroll through the list and select the object or
class that we wish to inspect.
5. After an object is picked up from the Classes list, we can see its
members (properties, methods and events) in the right column.
6. A property is represented by a small icon that has a hand holding a
piece of paper.
7. Methods are denoted by little green blocks, while events are
denoted by yellow lightning bolt icon.
Object naming conversions of controls (prefix)
1. Form -frm
2. Label-lbl
3. TextBox-txt
4. CommandButton-cmd
5. CheckBox -chk
6. OptionButton -opt
7. ComboBox -cbo
8. ListBox-lst
9. Frame-fme
10. PictureBox -pic
11. Image-img
12. Shape-shp
13. Line -lin
14. HScrollBar -hsb
15. VScrollBar -vsb
Key Visual Basic editing features
The following functions and statements are for file input and output.
Function or Purpose
Statement
UNIT-II
• Select “Windows Forms Application” in the pop-up New Project window and name the
project
In the form add the required controls and set the required properties, add the required code to
develop an application, click the run icon or press F5
Code Window
This displays the code associated with the item selected in the Project Explorer window.
Also sometimes referred to as a Module window.
You can even just double click the item in the Project Explorer window.
To display the code select the item in the Project Explorer window and either choose (View >
Code) or you can alternatively use the shortcut key (F7).
A code module will normally contain more than one procedure or function.
The IDE offers you a choice of whether to display the procedures one at a time (procedure
view) or all at one (full module view).
You can change the views by clicking on the small buttons in the lower-left corner of the
code window. Full Module view is the default.
Use the Code window to write, display, and edit Visual Basic code.
You can open as many Code windows as you have modules, so you can easily view the code
in different forms or modules, and copy and paste between them.
You can open a Code window from:
The Project window, by selecting a form or module, and choosing the View Code button.
A UserForm window, by double-clicking a control or form, choosing Code from the View
menu, or pressing F7.
icon - Full Module View Icon - Displays the entire code in the module.
This is the default.
Procedure View
This can be controlled from (Tools > Options)(Editor Tab, "Default to Full Module View")
icon - Procedure View Icon - Displays the selected procedure. Only one procedure at a time
is displayed in the Code window.
Edit Toolbar
List Properties / Methods - Displays a drop-down list in the code window that
contains all the properties and methods available for the object that precedes the
period (.).
List Constants - Displays a drop-down list in the code window that contains all the
constants that are valid choices for the property that precedes the equal sign (=).
Quick Info - Provides the syntax for a variable, method, function or procedure based
on the location of your position.
Parameter Info - Displays a popup in the code window containing information
about the parameters of the function in which the pointer is located.
Complete Word - Accepts the characters that the editor has automatically appended
as you were typing.
Indent - Shifts all the lines (in the current block) to the next tab stop. No shortcut key
Outdent - Shifts all the lines (in the current block) to the previous tab stop.
Toggle Breakpoint - Toggles the insertion of a breakpoint at the current line.
Comment Block - Adds the comment character ( ' ) to the beginning of each line in
the current selection.
UnComment Block - Removes the comment character ( ' ) from the beginning of
each line in the current selection.
Toggle Bookmark - Toggles the insertioon of a bookmark on or off for the active
line.
Next Bookmark - Moves the focus to the next bookmark in the bookmark stack.
Previous Bookmark - Moves the focus to the previous bookmark in the bookmark
stack.
Clear All Bookmarks - Removes all the breakpoints from the active project (or all
projects ??).
Declaration Statements, which name a variable, constant, or procedure, and can also
specify a data type.
Executable Statements, which initiate actions. These statements can call a method or
function, and they can loop or branch through blocks of code. Executable statements
include Assignment Statements, which assign a value or expression to a variable or
constant.
This topic describes each category. Also, this topic describes how to combine multiple
statements on a single line and how to continue a statement over multiple lines.
Declaration statements
You use declaration statements to name and define procedures, variables, properties, arrays,
and constants. When you declare a programming element, you can also define its data type,
access level, and scope. For more information, see Declared Element Characteristics.
The first declaration is the Sub statement. Together with its matching End Sub statement, it
declares a procedure named applyFormat. It also specifies that applyFormat is Public,
which means that any code that can refer to it can call it.
The second declaration is the Const statement, which declares the constant limit, specifying
the Integer data type and a value of 33.
The third declaration is the Dim statement, which declares the variable thisWidget. The data
type is a specific object, namely an object created from the Widget class. You can declare a
variable to be of any elementary data type or of any object type that is exposed in the
application you are using.
Initial Values
When the code containing a declaration statement runs, Visual Basic reserves the memory
required for the declared element. If the element holds a value, Visual Basic initializes it to
the default value for its data type. For more information, see "Behavior" in Dim Statement.
You can assign an initial value to a variable as part of its declaration, as the following
example illustrates.
Dim m As Integer = 45
' The preceding declaration creates m and assigns the value 45 to it.
If a variable is an object variable, you can explicitly create an instance of its class when you
declare it by using the New Operator keyword, as the following example illustrates.
Executable statements
An executable statement performs an action. It can call a procedure, branch to another place
in the code, loop through several statements, or evaluate an expression. An assignment
statement is a special case of an executable statement.
The following example uses an If...Then...Else control structure to run different blocks
of code based on the value of a variable. Within each block of code, a For...Next loop runs
a specified number of times.
The If statement in the preceding example checks the value of the parameter clockwise. If
the value is True, it calls the spinClockwise method of aWidget. If the value is False, it
calls the spinCounterClockwise method of aWidget. The If...Then...Else control
structure ends with End If.
The For...Next loop within each block calls the appropriate method a number of times
equal to the value of the revolutions parameter.
Assignment statements
Assignment statements carry out assignment operations, which consist of taking the value on
the right side of the assignment operator (=) and storing it in the element on the left, as in the
following example.
v = 42
In the preceding example, the assignment statement stores the literal value 42 in the variable
v.
The programming element on the left side of the assignment operator must be able to accept
and store a value. This means it must be a variable or property that is not ReadOnly, or it
must be an array element. In the context of an assignment statement, such an element is
sometimes called an lvalue, for "left value."
The value on the right side of the assignment operator is generated by an expression, which
can consist of any combination of literals, constants, variables, properties, array elements,
other expressions, or function calls. The following example illustrates this.
x = y + z + FindResult(3)
The preceding example adds the value held in variable y to the value held in variable z, and
then adds the value returned by the call to function findResult. The total value of this
expression is then stored in variable x.
In addition to numeric values, the assignment operator can also assign String values, as the
following example illustrates.
Dim a, b As String
a = "String variable assignment"
b = "Con" & "cat" & "enation"
' The preceding statement assigns the value "Concatenation" to b.
n += 1
The preceding example adds 1 to the value of n, and then stores that new value in n. It is a
shorthand equivalent of the following statement:
n = n + 1
A variety of compound assignment operations can be performed using operators of this type.
For a list of these operators and more information about them, see Assignment Operators.
The concatenation assignment operator (&=) is useful for adding a string to the end of already
existing strings, as the following example illustrates.
The value you assign to a variable, property, or array element must be of a data type
appropriate to that destination element. In general, you should try to generate a value of the
same data type as that of the destination element. However, some types can be converted to
other types during assignment.
For information on converting between data types, see Type Conversions in Visual Basic. In
brief, Visual Basic automatically converts a value of a given type to any other type to which
it widens. A widening conversion is one in that always succeeds at run time and does not lose
any data. For example, Visual Basic converts an Integer value to Double when appropriate,
because Integer widens to Double. For more information, see Widening and Narrowing
Conversions.
Narrowing conversions (those that are not widening) carry a risk of failure at run time, or of
data loss. You can perform a narrowing conversion explicitly by using a type conversion
function, or you can direct the compiler to perform all conversions implicitly by setting
Option Strict Off. For more information, see Implicit and Explicit Conversions.
Property Procedures
A property procedure is a series of Visual Basic statements that manipulate a custom property on a
module, class, or structure. Property procedures are also known as property accessors.
A Get procedure returns the value of a property. It is called when you access the
property in an expression.
A Set procedure sets a property to a value, including an object reference. It is called
when you assign a value to the property.
You usually define property procedures in pairs, using the Get and Set statements, but you
can define either procedure alone if the property is read-only (Get Statement) or write-only
(Set Statement).
You can omit the Get and Set procedure when using an auto-implemented property. For
more information, see Auto-Implemented Properties.
You can define properties in classes, structures, and modules. Properties are Public by
default, which means you can call them from anywhere in your application that can access
the property's container.
For a comparison of properties and variables, see Differences Between Properties and
Variables in Visual Basic.
Declaration Syntax
A property itself is defined by a block of code enclosed within the Property Statement and the
End Property statement. Inside this block, each property procedure appears as an internal
block enclosed within a declaration statement (Get or Set) and the matching End declaration.
The Modifiers can specify access level and information regarding overloading, overriding,
sharing, and shadowing, as well as whether the property is read-only or write-only. The
AccessLevel on the Get or Set procedure can be any level that is more restrictive than the
access level specified for the property itself. For more information, see Property Statement.
Data Type
A property's data type and principal access level are defined in the Property statement, not
in the property procedures. A property can have only one data type. For example, you cannot
define a property to store a Decimal value but retrieve a Double value.
Access Level
However, you can define a principal access level for a property and further restrict the access
level in one of its property procedures. For example, you can define a Public property and
then define a Private Set procedure. The Get procedure remains Public. You can change
the access level in only one of a property's procedures, and you can only make it more
restrictive than the principal access level. For more information, see How to: Declare a
Property with Mixed Access Levels.
Parameter Declaration
You declare each parameter the same way you do for Sub Procedures, except that the passing
mechanism must be ByVal.
If the parameter is optional, you must also supply a default value as part of its declaration.
The syntax for specifying a default value is as follows:
In a Get procedure, the return value is supplied to the calling expression as the value of the
property.
In a Set procedure, the new property value is passed to the parameter of the Set statement. If
you explicitly declare a parameter, you must declare it with the same data type as the
property. If you do not declare a parameter, the compiler uses the implicit parameter Value to
represent the new value to be assigned to the property.
Calling Syntax
You invoke a property procedure implicitly by making reference to the property. You use the
name of the property the same way you would use the name of a variable, except that you
must provide values for all arguments that are not optional, and you must enclose the
argument list in parentheses. If no arguments are supplied, you can optionally omit the
parentheses.
propertyname[(argumentlist)] = expression
lvalue = propertyname[(argumentlist)]
The following property stores a full name as two constituent names, the first name and the
last name. When the calling code reads fullName, the Get procedure combines the two
constituent names and returns the full name. When the calling code assigns a new full name,
the Set procedure attempts to break it into two constituent names. If it does not find a space,
it stores it all as the first name.
End Get
Set(ByVal Value As String)
Dim space As Integer = Value.IndexOf(" ")
If space < 0 Then
firstName = Value
lastName = ""
Else
firstName = Value.Substring(0, space)
lastName = Value.Substring(space + 1)
End If
End Set
End Property
Visual Basic loop structures allow you to run one or more lines of code repetitively. You can
repeat the statements in a loop structure until a condition is True, until a condition is False, a
specified number of times, or once for each element in a collection.
The following illustration shows a loop structure that runs a set of statements until a
condition becomes true:
The While...End While construction runs a set of statements as long as the condition
specified in the While statement is True.
Do Loops
The Do...Loop construction allows you to test a condition at either the beginning or the end of
a loop structure. You can also specify whether to repeat the loop while the condition remains
True or until it becomes True.
For Loops
The For...Next construction performs the loop a set number of times. It uses a loop control
variable, also called a counter, to keep track of the repetitions. You specify the starting and
ending values for this counter, and you can optionally specify the amount by which it
increases from one repetition to the next.
For Each Loops
The For Each...Next construction runs a set of statements once for each element in a
collection. You specify the loop control variable, but you do not have to determine starting or
ending values for it.