Functions |
|
Visual Basic Functions |
A function is a preprogrammed calculation .It can be carried out on
request from any point in a Visual Basic program. |
A function takes one or more arguments and returns a single value and
it can be included in an expression. |
|
Argument: |
It is a value on which a function or procedure operates. |
For example, in the Visual Basic statement Str(10), |
number 10 is the argument. |
|
Visual Basic includes built-in functions like Sqr, Cos or Chr etc.We
are discussing some of them here: |
|
(1) Sqr() |
Returns a Double specifying the square root of a number. |
|
Syntax |
Sqr(number) |
|
(2) ABS() |
Returns the absolute, positive value of the given numeric
expression. |
|
Syntax |
ABS(numeric_expression) |
|
Arguments |
numeric_expression |
Is an expression of the exact numeric or approximate numeric
data type category. |
Return Types |
Returns same type as numeric_expression. |
|
(3) POWER() |
Returns value of given expression to the specified power. |
|
Syntax |
POWER(numeric_expression, y) |
Arguments |
numeric_expression |
Is an expression of the exact numeric or approximate numeric
data type category. |
y Is the power to which to raise numeric_expression. y can be an
expression of the exact numeric or approximate numeric data
type category. |
|
(4) FLOOR () |
Returns largest integer less than or equal to the given numeric
expression. |
|
Syntax |
FLOOR(numeric_expression) |
Arguments |
numeric_expression |
Is an expression of exact numeric or approximate numeric data
type category. Return Types |
Returns the same type as numeric_expression. |
|
For Example: |
FLOOR(122.45), Return value=122 |
FLOOR(-123.45) ,Return value=124 |
|
(5) Round() |
Returns a numeric expression, rounded to the specified length
or precision. |
|
Syntax |
ROUND (numeric_expression, length) |
Arguments |
numeric_expression |
Is an expression of the exact numeric or approximate numeric
data type category. |
length Is the precision to which numeric_expression is to be rounded.
length must be int. |
When length is a positive number, numeric_expression is rounded to
the number of decimal places specified by length. |
When length is a negative number, numeric_expression is rounded on
the left side of the decimal point, as specified by length. |
Return Types |
Returns the same type as numeric_expression. |
|
For Example: |
Round(123.4545,2) ,Return Value=123.4500 |
|
(6) SQUARE() |
Returns the square of given expression. |
|
Syntax |
SQUARE(float_expression) |
Arguments |
float_expression |
Is an expression of type float. |
Return Types |
float |
|
(7) PI () |
Returns the constant value of PI. |
|
Syntax |
PI() |
Return Types |
float |
|
(8) SIGN () |
Returns the positive (+1), zero (0), or negative (-1) sign of the
given expression. |
|
Syntax |
SIGN(numeric_expression) |
Arguments |
numeric_expression |
Is an expression of the exact numeric or approximate numeric
data type category. |
Return Types |
Float |
|
(9) LOG10 () |
Returns the base-10 logarithm of the given float expression. |
|
Syntax |
LOG10(float_expression) |
Arguments |
float_expression |
Is an expression of the float data type. |
Return Types |
Float |
|
(10) RAND () |
Returns a random float value between 0 and 1. |
|
Syntax |
RAND([seed]) |
Arguments |
seed |
Is an integer expression (int) giving the starting value. |
Return Types |
float
Methodes and Events |
|
Methods |
Methods are associated with objects, and define the actions they carryout for their object. |
|
For Example: |
|
The methods of your radio are on, off, volume control etc, Which give you control on your radio.Each Method performs a particular action. Method is an action that an object is capable of performing. |
|
For example: |
|
list boxes have methods called AddItem, RemoveItem and Clear, for maintaining list contents. |
|
Syntax for Calling Method |
Objectname-Method |
|
Here objectname specifies the name of an object, and Method specifies the name of a method. |
|
Example: |
|
Form1.Print "Hello World" |
Here Form1 is form name , and method Print is called to print the String "Hello World" to form. |
There are different methods associated with different controls.Methods are useful because they hide the details of how an action is performed. |
The programmer just needs to call the method ,how the action is performed it is not his or her headache. |
|
Note That: |
Put a dot after the name of control then various options will look like in figure. |
Methods associated with some commonly used controls: |
|
List Box |
It is control that provides a list of items. |
|
Methods of List Box: |
|
1) Additem: This method adds the specified item to the list |
2) Removeitem: This method is used to delete an item from the list. |
3) SetFocus: Used to make the list box the Current active element. |
|
|
Combo Box: |
A combo box has all the properties exhibited by the text box and list box. |
|
Methods of combo box: |
1.Additem: This method will add the specified item to the combo box. |
2.Remove Item: This method will delete an item from the combo box. |
|
Option Button: |
|
Option button are used when the user can select one and only one of the multiple options. |
Typically,there are no special methods associated with option buttons.A method associated with option |
button is: |
Move: Moves a control on the form. |
|
Command Button: |
|
The easiest way to allow the user to interact with an application is to provide a button to click. |
Visual Basic provides this in form of Command Button. |
One method is used with the command button: |
Move: Moves a control on the form |
Events |
A major part of interaction between people in every day life is in the form of events and response. |
|
For Example |
|
When some one knocks at the door, then you open the door.Knocking at door is event and opening the door is response to the event. |
|
In technical words an event is an user action directed at the application. |
Example Of An Event In An Application |
1.Clicking the mouse. |
2.Pressing a key on the keyboard. |
|
When you click on a button it is recognized as an event by the button. |
|
A calculator (Provided in your Windows) shown in figure is the perfect example of events and their quick responses. |
|
For Example |
|
Clicking on a number Button will display a particular number in the text Box.Some of events like methods require additional information in the form of arguments. |
|
For Example, |
|
Click event does not requires arguments whereas DragDrop event requires arguments as shown in example: |
|
Public Sub Command1_Click() |
Statements |
End Sub |
Public Sub Commnand1_DragDrop(Source As Control,X as Single,Y As Single) |
Statements |
End Sub |
Working with Forms |
|
Form Basics |
A form is a window that contains application code and other objects placed on it to create the user interface. Forms may fill the entire screen or have other forms contained with in it. |
|
Note that: |
Visual basic initially includes a default form Form1 file in each project.You can change the name and caption of the form to identify the purpose of the form. |
|
Every form has its own properties ,events and methods associated with it. |
|
|
|
Here a sample form is shown, VISUAL BASIC gives default name and caption to a form as form1. It is shown in above figure. |
|
Adjusting of height and width is shown in figure below: |
|
|
|
Form objects are the basic building blocks of a Visual Basic application. It is actual window with which a user interacts when they run the application. |
Forms have their own properties, events and methods with which you can control their appearance and behavior. |
Form Properties |
Many properties of a form affect its physical appearance. The Caption property determines the text that is shown in the form's title bar, the Icon property sets the icon that is displayed when a form is minimized. |
The MaxButton and MinButton properties determine whether the form can be maximized or minimized. By changing the BorderStyle property, you can control the resizing behavior of the form. |
|
There are some specific properties of the form and a brief description of some selected properties is given here. |
|
Property window of a form is shown below: |
|
|
|
Border Style: |
Form can have different types of borders. |
|
The borders are as follows: |
|
|
|
Caption: |
The title of the window is stored in Caption property.It is shown at top of the form. |
|
Icon: |
This property specifies the icon for the window in the upper-left corner of the window. |
|
Mouse Pointer: |
This displays the value that indicates the type of mouse pointer. |
|
Max Button: |
This property indicates whether the maximize Button should be shown and Maximize choice available in the control box menu. |
|
Min Button: |
This property indicates whether the minimize Button should be shown and minimize choice available in the control box menu. |
|
MDI Child: |
This property specifies if this window must be shown within a multiple document interface (MDI) window. You will discuss them in detail in further section of tutorial. |
|
Back Color: |
Back Color is used to fill the back ground color of the form. |
|
Autoredraw: |
Setting AutoRedraw to True always produces normal layering. While using AutoRedraw is the easiest way to layer graphics, applications with large forms may suffer from reduced performance due to the memory demands of Autoredraw. |
|
For Example: |
By setting Autoredraw property of form to true ,You print any text at the form. |
The Form Events |
As objects, forms can perform methods and respond to events.The Resize event of a form is occured whenever a form is resized, either by user interaction or through code. This allows you to perform actions such as moving or resizing controls on a form. |
|
The Activate event occurs whenever a form becomes active form, the Deactivate event occurs when another form or application becomes active. These events are convenient for initializing or finalizing the form'sbehaviour. |
OR For writing the code for any from use key combination Alt+V+C. |
|
A form's events are shown below in the figure: |
|
|
|
For Writing code of a particular event just click on a particular event ,then the following code window will appear: |
|
|
|
Here for example you have clicked the MouseUp event. |
|
Using the unload Event: |
|
The unload event procedure is used to verify that the form should be unloaded.The unload event is occurred when: |
|
1.The Form is unloaded using the Unload statement. |
2.The form is closed by either clicking the Close command on the application menu or clicking the close button on application title bar. |
|
Commonly used Form Events: |
|
Activate | Form_Activate event is triggered when form becomes the active window. |
Click | Form_Click event is triggered when user clicks on form. |
DblClick | Form_DblClick event is triggered when user double-clicks on form. |
Load | Form_Load event occurs when form is loaded.This is a good place to initialize variables and sets any run-time property. |
|
The Form Methods |
Different methods are used in forms. When you put a dot after name of the form then a popup menu is displayed. |
|
There are different methods for forms .Some of them are describing here. |
|
|
|
Show method |
|
Show method is used to display the form object. |
For Example to display the form frmStudents, |
following code is written: |
frmStudents.Show |
|
Hide Method |
|
The Hide method is used to hide a form. |
For example to hide a form, following code is written: |
frmStudents.Hide |
|
The Cls Method |
|
All text and graphics on the form that were created with Print and graphics methods can be deleted with the Cls method. |
Example: frmStudents.Cls |
|
The Refresh Method |
|
Forces a complete repaint of a form. |
For example: |
form1.Refresh |
Use the Refresh method when you want to Completely display one form, while another form loads. |
|
Point Method |
|
Returns the red-green-blue (RGB) color of the specified point on a Form. It returns color as a long integer. |
form1.Point(x, y) |
Setting Runtime & Design Time Properties |
Visual Basic operates in three modes. |
|
1. Design mode - used to build application. |
2. Run mode - used to run the application. |
3. Break mode - used for debugging application. |
|
Many properties can be set at either design time or run time. At design time controls are added to the form, their properties are set and code is written. During runtime you examine your program in action, just as the end user will see it. |
|
To set a property at design time , following steps are followed: |
|
1. Select the form or object whose characteristics are to be changed. |
2. Activate the properties window. |
3. Scroll the property you want to set and select its name. |
4. Enter a new setting. |
|
At runtime, property can be changed by a program code. |
|
This is done like this: |
|
|
|
|
when you run the above application then the form1's backcolor, mouse pointer and Caption will look like figure. |
|
|
Setting Start Up Form |
The first form in your application is designated as the startup form. When your application starts running, this form is displayed. User will see Startup form when application starts. |
If you want a different form to display when your application starts, you must change the startup form. |
|
To change the startup form |
|
1. From the Project menu, choose Project Properties. |
|
|
|
|
2. Choose General tab. |
3. In the Startup Object list box, select the form you want as the new startup form. |
4. Choose OK. |
|
|
|
The following figure will describe the above procedure: |
|
In above figure you can choose the start up form from1 or form2. If you opt for form2 then after executing the application form2 will be shown. |
|
No comments:
Post a Comment