COMPUTER TRAINING: Methodes and Events & Woking With Forms

Monday, 24 December 2012

Methodes and Events & Woking With Forms

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.

Methodes and Events
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:
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 toclick.
Visual Basic provides this in form of Command Button.
One method is used with the command button:
Move: Moves a control on the form

Methodes and Events
Events
A major part of interaction between people in every day life is in the form of events andresponse.
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.

Methodes and Events
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.
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 ofevents 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 asform1. 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.

Working with Forms
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.

Working with Forms
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 eventoccurs 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:
ActivateForm_Activate event is triggered when form becomes the active window.
ClickForm_Click event is triggered when user clicks on form.
DblClickForm_DblClick event is triggered when user double-clicks on form.
LoadForm_Load event occurs when form is loaded.This is a good place to initialize variables and sets any run-time property.

Working with Forms
The Form Methods
Different methods are used in forms. When you put a dot after name of the form then apopup 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 anotherform 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)

Working with Forms
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:
For Animated Presentation Click Here
when you run the above application then the form1's backcolor, mouse pointer and Caption will look like figure.

Working with Forms
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 thestartup form.
To change the startup form
1. From the Project menu, choose Project Properties.
For Animated Presentation Click Here
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 form2then after executing the application form2 will be shown.

No comments:

Post a Comment