GROUP WORK PRESENTATION GROUP MEMBERS 1) IRENE KENDI KINYUA ADM NO 2013/CS/26386 2) SYLIVIA A...

41
GROUP WORK PRESENTATION GROUP WORK PRESENTATION GROUP MEMBERS GROUP MEMBERS 1) 1) IRENE KENDI KINYUA ADM NO 2013/CS/26386 IRENE KENDI KINYUA ADM NO 2013/CS/26386 2) 2) SYLIVIA A CHIBUKOSIA ADM NO 2013/CS/26629 SYLIVIA A CHIBUKOSIA ADM NO 2013/CS/26629 3) 3) KEPHA MOTANYA BUNDI ADM NO 2013/CS/26375 KEPHA MOTANYA BUNDI ADM NO 2013/CS/26375 4) 4) BENJAMIN ONSOMU ADM NO 2013/CS/26376 BENJAMIN ONSOMU ADM NO 2013/CS/26376 5) 5) OLIVE WANJIRU KARENGE ADM NO OLIVE WANJIRU KARENGE ADM NO 2013/CS/26370 2013/CS/26370

Transcript of GROUP WORK PRESENTATION GROUP MEMBERS 1) IRENE KENDI KINYUA ADM NO 2013/CS/26386 2) SYLIVIA A...

GROUP WORK PRESENTATIONGROUP WORK PRESENTATION

GROUP MEMBERSGROUP MEMBERS

1)1) IRENE KENDI KINYUA ADM NO 2013/CS/26386IRENE KENDI KINYUA ADM NO 2013/CS/26386

2)2) SYLIVIA A CHIBUKOSIA ADM NO 2013/CS/26629SYLIVIA A CHIBUKOSIA ADM NO 2013/CS/26629

3)3) KEPHA MOTANYA BUNDI ADM NO 2013/CS/26375KEPHA MOTANYA BUNDI ADM NO 2013/CS/26375

4)4) BENJAMIN ONSOMU ADM NO 2013/CS/26376BENJAMIN ONSOMU ADM NO 2013/CS/26376

5)5) OLIVE WANJIRU KARENGE ADM NO 2013/CS/26370OLIVE WANJIRU KARENGE ADM NO 2013/CS/26370

Lecture 1 - What's Visual Basic About?Lecture 1 - What's Visual Basic About?

This lecture focuses on the main components of the Visual Basic environment. By the This lecture focuses on the main components of the Visual Basic environment. By the

end of this session you should be able to end of this session you should be able to

navigate competently and use many of the functions that are important in the navigate competently and use many of the functions that are important in the development of an application. development of an application.

Applications of Vb softwareApplications of Vb softwareVisual Basic is used to develop Visual Basic is used to develop 'rapid applications''rapid applications' inside a windows environment inside a windows environment

Visual Basic is a high level programming paradigm. Its concepts are based upon Visual Basic is a high level programming paradigm. Its concepts are based upon Event driven Event driven

programmingprogramming. The environment to edit, delete and write code as well as develop windows based . The environment to edit, delete and write code as well as develop windows based

applications is known as the applications is known as the 'Integrated Development Environment' (IDE)'Integrated Development Environment' (IDE). .

Visual basic can do the following;Visual basic can do the following;

Develop commercial applications, for example databases and the internet Develop commercial applications, for example databases and the internet

Links to other products such as Word, Excel and Access Links to other products such as Word, Excel and Access

Multimedia applications Multimedia applications

The Visual Basic Integrated Development The Visual Basic Integrated Development EnvironmentEnvironment

IDE Cont….IDE Cont….From the diagram it can be seen that the IDE is divided into separate areas or 'From the diagram it can be seen that the IDE is divided into separate areas or 'windowswindows'. We have the Toolbox control '. We have the Toolbox control which allows us to add objects on to Form window. We can change the properties using the properties windows for all which allows us to add objects on to Form window. We can change the properties using the properties windows for all the objects on the form. We can also edit/create the event handlers using the Code Window. When creating the objects on the form. We can also edit/create the event handlers using the Code Window. When creating applications in Visual Basic it is quite common to use multiple forms, modules etc. The project explorer window is used applications in Visual Basic it is quite common to use multiple forms, modules etc. The project explorer window is used to keep track of all the additional files used. to keep track of all the additional files used.

TOOL BOX TOOL BOX COMPONENTS OF A TOOL BOX EXPLAINEDCOMPONENTS OF A TOOL BOX EXPLAINED

CONTROLS AND THEIR FUCTIONS EXPLAINEDCONTROLS AND THEIR FUCTIONS EXPLAINED

Pointer-Pointer-For resizing forms and controls (a tool for selection and manipulation of objectsFor resizing forms and controls (a tool for selection and manipulation of objects

Label Label For changing text such as captionsFor changing text such as captions

Frames-Frames-Purely visual effect (used to group check boxes or option buttonsPurely visual effect (used to group check boxes or option buttons

Check Box-Check Box-For binary optionsFor binary options

Combo Box-Combo Box-Combination of list box and text boxCombination of list box and text box

Timers-Timers-Can be used to control animations or timing events (often invisible)Can be used to control animations or timing events (often invisible)

Picture Boxes-Picture Boxes-Allows the inclusion of bitmaps, icons on a formAllows the inclusion of bitmaps, icons on a form

Text Boxes-Text Boxes-Provide a standard way for accepting user input through the keyboardProvide a standard way for accepting user input through the keyboard

Command Button-Command Button-Standard Window command buttons such as 'OK' and 'Cancel'Standard Window command buttons such as 'OK' and 'Cancel'

The Form WindowThe Form Window

The Form window is your primary work area where the visual development of the application is created or The Form window is your primary work area where the visual development of the application is created or modified. modified.

The The form windowform window shows the main body of the application. The form can be resized to take up the width of the shows the main body of the application. The form can be resized to take up the width of the screen. Here objects are placed on to the form such as the command buttons, labels, text boxes, scrollbars, and screen. Here objects are placed on to the form such as the command buttons, labels, text boxes, scrollbars, and other controls to form the main elements of the other controls to form the main elements of the Graphical User InterfaceGraphical User Interface

The Code WindowThe Code Window

The The Code windowCode window is little more than a text editor with which you write the programming statements that tie the is little more than a text editor with which you write the programming statements that tie the application together. application together.

An alternative technique to view the code is to double click anywhere on the form or the object and view the code An alternative technique to view the code is to double click anywhere on the form or the object and view the code for that particular object. The diagram shows the for that particular object. The diagram shows the Code WindowCode Window for the form for the form

Example Example

Private Sub txtJAN_KeyPress(KeyAscii As Integer) Private Sub txtJAN_KeyPress(KeyAscii As Integer) Call TEST(KeyAscii, txtFEB)Call TEST(KeyAscii, txtFEB) End Sub End Sub

Code window cont….Code window cont…. When you or another user compiles or runs the source program, VB translates the program into an executable When you or another user compiles or runs the source program, VB translates the program into an executable

program. You cannot make changes directly to an executable program. If you see bugs when you run the program. You cannot make changes directly to an executable program. If you see bugs when you run the program, you must change the source application (which might contain multiple files in the project) and rerun or program, you must change the source application (which might contain multiple files in the project) and rerun or recompile the source. recompile the source.

The Project Explorer WindowThe Project Explorer Window

Gives you a tree-structured view of all the files in the application. Gives you a tree-structured view of all the files in the application. MicrosoftMicrosoft changed the formal name from Project changed the formal name from Project window to window to Project ExplorerProject Explorer window window

The Properties WindowThe Properties Window

The The propertyproperty window represents an objects associated window represents an objects associated propertiesproperties. Each property of a control . Each property of a control such as a such as a LabelLabel, , command buttoncommand button etc. has its own unique set of properties. etc. has its own unique set of properties.

A different list appears in the A different list appears in the Properties windowProperties window every time you click over a different Form every time you click over a different Form window object. The window object. The Properties windowProperties window describes properties (descriptive and functional describes properties (descriptive and functional information) about the form and its controls. Many information) about the form and its controls. Many propertiesproperties exist for almost every object in exist for almost every object in Visual Basic. The Visual Basic. The Properties windowProperties window lists all the properties of the Form window's lists all the properties of the Form window's selectedselected control. control.

The The property windowproperty window is activated by pressing the is activated by pressing the shortcut keyshortcut key F4F4. .

Running the ApplicationRunning the Application

To run the application To run the application

F5F5

Lecture 2 - Objects, Methods and Lecture 2 - Objects, Methods and PropertiesProperties

This lecture focuses on the underlying This lecture focuses on the underlying principles that support Visual Basic principles that support Visual Basic programming.programming.

ObjectsObjects During most of the course we will be referring to During most of the course we will be referring to objectsobjects as screen objects such as as screen objects such as Labels.Labels. command buttons.command buttons. text boxes.text boxes. combo boxescombo boxes other controls.other controls.

Each Each objectobject has its own set of has its own set of propertiesproperties (associated to the (associated to the ObjectObject). ).

A good example of this is the A good example of this is the LabelLabel and and Text boxText box object properties. For example, the object properties. For example, the CaptionCaption property property

is used in most controls including the is used in most controls including the LabelLabel, however not in the , however not in the Text boxText box control (which uses the control (which uses the Text Text

propertyproperty). ).

Objects cont…….Objects cont…….We can also set the property using the We can also set the property using the codecode rather than the rather than the properties windowproperties window, ,

the the codecode is inserted inside the is inserted inside the Code WindowCode Window. The general structure to . The general structure to reference an objects property via code is given below: reference an objects property via code is given below:

ObjectName.PropertyObjectName.Property An example is illustrated below: An example is illustrated below: Private Sub Form_Load() Private Sub Form_Load() Form1.Caption = "Ascending Numbers" Form1.Caption = "Ascending Numbers" lblTitle.Caption = "ASCENDING NUMBERS" lblTitle.Caption = "ASCENDING NUMBERS" lblTitle.FontBold = True lblTitle.FontBold = True lblTitle.FontSize = 12 lblTitle.FontSize = 12 lblNum.Caption = "Enter a number between 1 to 30" lblNum.Caption = "Enter a number between 1 to 30" & _& _ " you wish to see in " you wish to see in

ascending ORDER" ascending ORDER" lblOutput.Caption = “ " lblOutput.Caption = “ " lblOutput.Visible = FalselblOutput.Visible = FalselblOutput.FontBold = True cmdOK.Caption = "&OK" End Sub lblOutput.FontBold = True cmdOK.Caption = "&OK" End Sub

PropertiesProperties

PropertiesProperties behave like behave like variablesvariables, they , they storestore the current state of an the current state of an objectobject as described above, for example as described above, for example colourcolour, , size, size, widthwidth etc. etc. PropertiesProperties of an of an objectobject can be changed by two main ways: can be changed by two main ways:

Using the properties window Using the properties window

Using Visual Basic Code Using Visual Basic Code

It is quite straight forward to change the property using the It is quite straight forward to change the property using the properties windowproperties window, however, more thought is required to, however, more thought is required to

change the property using code. Below we demonstrate the two methods:change the property using code. Below we demonstrate the two methods:

Properties cont…..Properties cont….. Step 1Step 1

Highlight the object you wish to change (Label object).Highlight the object you wish to change (Label object). Step 2Step 2

Select Select F4F4 to view the property window, and thus change the to view the property window, and thus change the CaptionCaption property to "Please enter your name" However, property to "Please enter your name" However, when changing the property using code an alternative technique is used. If you have not done programming before this when changing the property using code an alternative technique is used. If you have not done programming before this will be new to you. It takes the following format will be new to you. It takes the following format Object.propertyObject.property and values are assigned to it. For example, the and values are assigned to it. For example, the following code does exactly the same as the following code does exactly the same as the stepssteps illustrated above: illustrated above:

Label1.Caption = "Please enter your name" Label1.Caption = "Please enter your name" 'General format: Object. Property 'General format: Object. Property

Here the code takes the Here the code takes the objectobject to reference the to reference the propertyproperty. To do this we use the . To do this we use the separator (full stop). and we then state the property we wish to change. In this case it separator (full stop). and we then state the property we wish to change. In this case it is is CaptionCaption property. Finally we give it a value by using the equals sign =. This is known property. Finally we give it a value by using the equals sign =. This is known as the as the AssignmentAssignment symbol. symbol. The equal sign assigns the statement The equal sign assigns the statement "Please enter your name""Please enter your name" to the objects property (Label1.Caption). to the objects property (Label1.Caption).

MethodsMethods

MethodsMethods are blocks of are blocks of codecode which will carry out a task, but the complexity of the task itself is hidden from the which will carry out a task, but the complexity of the task itself is hidden from the user. For example, when heating a meal inside a microwave, we set the timer and then press the user. For example, when heating a meal inside a microwave, we set the timer and then press the StartStart button, we button, we are not interested how the button works but the outcome of the result, i.e. heated food. In the same way we can are not interested how the button works but the outcome of the result, i.e. heated food. In the same way we can use use MethodsMethods which perform a task instead of writing the task itself and then which perform a task instead of writing the task itself and then executingexecuting it. it.

MethodsMethods behave like behave like proceduresprocedures and again are and again are associatedassociated with the with the objectobject. These are more commonly described . These are more commonly described as as hiddenhidden event handlersevent handlers which perform particular tasks. For example, the form has a couple of which perform particular tasks. For example, the form has a couple of methodsmethods of of which two are known as which two are known as showshow and and hidehide. These two . These two methodsmethods are used to display and view multiple forms. We will are used to display and view multiple forms. We will be looking at these methods later during the course. The syntax for this is shown below and is referenced via the be looking at these methods later during the course. The syntax for this is shown below and is referenced via the dotdot character ( character (..): ):

<OBJECT>.<METHOD><OBJECT>.<METHOD> Form1.Show Form1.Show

EventsEvents

Visual Basic is based on Visual Basic is based on EventEvent driven programming. This is slightly different if you have driven programming. This is slightly different if you have programmed in either Pascal or C. The first question is programmed in either Pascal or C. The first question is what is an event ?what is an event ?. Well in Visual Basic . Well in Visual Basic programming an event is programming an event is

CodeCode used to handle some response inside a control. This is usually termed used to handle some response inside a control. This is usually termed event handler event handler routinesroutines

The event is initiated by user action, for example, The event is initiated by user action, for example, ClickClick, , MouseMoveMouseMove, , MouseUpMouseUp, , LoadLoad etc. etc. In the example, there is only one object - the In the example, there is only one object - the command buttoncommand button placed on a form. Inside the object placed on a form. Inside the object

we have some event handler code. The event is called we have some event handler code. The event is called ClickClick. When the user . When the user initiatesinitiates the the eventevent by clicking on the by clicking on the command buttoncommand button, the , the message boxmessage box displays "Hello world" on to the screen.   displays "Hello world" on to the screen.   The format is: The format is:

<OBJECT>_<EVENT> <OBJECT>_<EVENT>

'Forexample:Command1_Click()'Forexample:Command1_Click()

Events cont……Events cont…… The format for the code shown is very important when writing code. The The format for the code shown is very important when writing code. The underscore characterunderscore character needs to be needs to be

placed between the object and the event. The parenthesis placed between the object and the event. The parenthesis ( )( ) indicates that values can be passed in to the indicates that values can be passed in to the Event Event proceduresprocedures. .

The procedure headings are created by The procedure headings are created by defaultdefault as well as the ' as well as the 'End SubEnd Sub' to end the routine itself. ' to end the routine itself.

Other Common EventsOther Common Events

There are various events that are used inside There are various events that are used inside Visual BasicVisual Basic the following list details the most common the following list details the most common events that are used. Most of these will be used throughout the course: events that are used. Most of these will be used throughout the course: Form_Load EventForm_Load EventThis is one of the most common event handlers and sits behind a form. This event occurs only once This is one of the most common event handlers and sits behind a form. This event occurs only once

in in the lifetime of the application. When the form loads the properties can be set at the lifetime of the application. When the form loads the properties can be set at run-timerun-time. The. TheForm_LoadForm_Load event occurs when a form is placed in to event occurs when a form is placed in to memorymemory, and is often used to set the , and is often used to set the initial initial propertiesproperties of a form itself. However, there are various events that occur before or after a of a form itself. However, there are various events that occur before or after a formform is is loadedloaded into memory. The sequence is important in relation to the into memory. The sequence is important in relation to the Form_LoadForm_Load event itself. The event itself. The sequenced events are illustrated below: sequenced events are illustrated below:

Initialize>Load>Resize>PaintInitialize>Load>Resize>PaintFrom the illustration above it is clear that the From the illustration above it is clear that the InitializeInitialize event occurs event occurs beforebefore the the Form_LoadForm_Load event and the event and the ResizeResize and and PaintPaint events occur after the Form_Load(). events occur after the Form_Load().

Lecture 3 - Data types, and variablesLecture 3 - Data types, and variables

This lecture focuses on the different storage capacities of objects inside Visual Basic. This lecture also This lecture focuses on the different storage capacities of objects inside Visual Basic. This lecture also gives the general understanding for the background principles in VB programming.gives the general understanding for the background principles in VB programming.

Naming ConventionsNaming Conventions

The objects on a form or listed in the The objects on a form or listed in the properties windowproperties window are given are given defaultdefault names such as "Label1, names such as "Label1,

Label2 ...Label3" etc. However, using more Label2 ...Label3" etc. However, using more meaningfulmeaningful naming conventions for naming conventions for objectsobjects such as such as cmdOKcmdOK, ,

lblNamelblName would suggest that the first would suggest that the first objectobject is a command button, and the property is is a command button, and the property is ‘OK’‘OK’, the second a l, the second a llabel holding the name. This can result in certain ambiguities being resolved. For example:label holding the name. This can result in certain ambiguities being resolved. For example:

User states a control which is a "option box" and displays the "Sex" of the user. By default the control isUser states a control which is a "option box" and displays the "Sex" of the user. By default the control isnamed as "Option1"named as "Option1"

Using correct naming methods the control can be renamed as "optSex" - i.e. the control is a "option Using correct naming methods the control can be renamed as "optSex" - i.e. the control is a "option

button" and holds the "Sex" to be button" and holds the "Sex" to be It is important to use the correct naming conventions when writing codeIt is important to use the correct naming conventions when writing code

Naming Conventions cont…..Naming Conventions cont…..

Combo BoxCombo Box

chk-Checkboxchk-Checkbox Cmd-Command buttonCmd-Command button Dir-Directory list boxDir-Directory list box Drv-Drive list boxDrv-Drive list box Fil-File list boxFil-File list box Fra-FrameFra-Frame Frm-FormFrm-Form Grd-GridGrd-Grid Hsb-Horizontal scroll barHsb-Horizontal scroll bar Img-ImageImg-Image Lbl-LabelLbl-Label Lne-LineLne-Line Lst-List boxLst-List box Mnu-MenuMnu-Menu Ole-OLE clientOle-OLE client Opt-Option buttonOpt-Option button Pic-Picture boxPic-Picture box Shp-ShapeShp-Shape Tmr-TimerTmr-Timer Txt-Text boxTxt-Text box Vsb-Vertical scroll barVsb-Vertical scroll bar

The Visual Basic data typesThe Visual Basic data types

A A data typedata type represents the storage capacity of a represents the storage capacity of a variablevariable. Suppose, somebody asks . Suppose, somebody asks "What is your name""What is your name" and you reply and you reply"Vas""Vas" if that is your name. Well, the value "Vas" is stored inside a 'container' somewhere inside your brain. The brain if that is your name. Well, the value "Vas" is stored inside a 'container' somewhere inside your brain. The brain will allocate some storage space to hold the container and therefore can specify the 'holding' value and retrieve it if will allocate some storage space to hold the container and therefore can specify the 'holding' value and retrieve it if neccessary inside a container or neccessary inside a container or variablevariable. .

Visual Basic Visual Basic data typesdata types act in a similar way, in which the type of storage amount is determined as to whether the user act in a similar way, in which the type of storage amount is determined as to whether the userintends to store numbers, characters or both inside the container. For example, a user may want to store intends to store numbers, characters or both inside the container. For example, a user may want to store whole whole numbersnumbers to allow this we use a to allow this we use a data typedata type called an called an integerinteger which has the storage capacity only to store which has the storage capacity only to store integerinteger values values only. only. Data TypeData Type Description and Range Description and Range     BooleanBoolean A data type that takes on one of two values only: True or False.A data type that takes on one of two values only: True or False.ByteByte 8 bits storage capacity, having the range of any character (from 0 to 255) from the ASCII character set.8 bits storage capacity, having the range of any character (from 0 to 255) from the ASCII character set.CurrencyCurrency Used to hold decimal values used for currency functions. The range is from -$922,337,203,685,477.5808 to Used to hold decimal values used for currency functions. The range is from -$922,337,203,685,477.5808 to

$922,337,203,685,477.5807. ($922,337,203,685,477.5807. (NoteNote: four decimal places ensure that proper rounding can occur. ): four decimal places ensure that proper rounding can occur. )DateDate Holds the date and time values. The date can range from January 1, 100, to December 31,9999. Holds the date and time values. The date can range from January 1, 100, to December 31,9999. DoubleDouble precision used for decimal values. The range for this data type is -1.79769313486232E+308 to 1.79769313486232E+308precision used for decimal values. The range for this data type is -1.79769313486232E+308 to 1.79769313486232E+308IntegerInteger Whole numbers which range from -32,767 to +32,768. The storage capacity for this data type is 2 bytes. Note: cannot Whole numbers which range from -32,767 to +32,768. The storage capacity for this data type is 2 bytes. Note: cannot

be used for decimal calculationsbe used for decimal calculationsLongIntegerLongInteger The data range for an integer is limited, due to its storage capacity. When an integer data type is above its range, there is The data range for an integer is limited, due to its storage capacity. When an integer data type is above its range, there is

an an over flow and erroneous results occur. LongInteger is more suitable with additional storage capacity and increased range over flow and erroneous results occur. LongInteger is more suitable with additional storage capacity and increased range to -to - 2,147,483,648 to 2,147,483,647.2,147,483,648 to 2,147,483,647.

ObjectObject A special data type which references objects such as controls and forms.A special data type which references objects such as controls and forms.SingleSingle Used as alternate data type for double precision known as Single-precision It requires less memory and lower data range Used as alternate data type for double precision known as Single-precision It requires less memory and lower data range

from -from - 3.402823E+38 to 3.402823E+383.402823E+38 to 3.402823E+38StringString String data type is an array of characters. Data that consists of 0 to 65,400 characters of alphanumeric data (numeric and String data type is an array of characters. Data that consists of 0 to 65,400 characters of alphanumeric data (numeric and

alphabetic)alphabetic)VariantVariant Data of any data type and used for control and other values for which the data type is unknown.Data of any data type and used for control and other values for which the data type is unknown.

VariablesVariables

When storing values into a computer, for example a students When storing values into a computer, for example a students namename, , ageage etc. We need to assign a etc. We need to assign a data typedata type to store to storethat value. A that value. A variablevariable is a is a containercontainer which is assigned some memory to hold the value. The value may be changed which is assigned some memory to hold the value. The value may be changed during program execution. during program execution.

Therefore, The program will have a place to hold information temporarily for calculations. To hold data that might Therefore, The program will have a place to hold information temporarily for calculations. To hold data that might change due to calculations or state changes within the application, you must declare change due to calculations or state changes within the application, you must declare variablesvariables. A . A variablevariable is a named is a named location that holds data. location that holds data.

Dim StatementDim StatementWhen declaring When declaring variablesvariables we use a special statement this is known as the we use a special statement this is known as the DimDim statement. This statement allocates statement. This statement allocates sufficient memory to hold the value of the data, which is associated with the data type. sufficient memory to hold the value of the data, which is associated with the data type.

Using the Using the DimDim statement to declare two variables (Dim stands for dimension) called statement to declare two variables (Dim stands for dimension) called Num1Num1 (which will hold numbers) (which will hold numbers) and and NameName (which will hold alphanumeric characters). The required memory is used to hold the data which is (which will hold alphanumeric characters). The required memory is used to hold the data which is 22 bytes bytes and and 256256 bytes respectively. Format: bytes respectively. Format:

Dim Dim variablevariable As As DataTypeDataType

Assignment StatementAssignment Statement

The assignment statement is used to assign values to The assignment statement is used to assign values to variablesvariables or or objectsobjects. The symbol used for the . The symbol used for the assignmentassignment

statement is the equals statement is the equals “ = "“ = " sign. For example: sign. For example:

'Use assignment statement'Use assignment statement'to set value of objects properties... 'to set value of objects properties... Label1.Caption = "Hi there how are you" Label1.Caption = "Hi there how are you" Command1.Caption = "OK" Command1.Caption = "OK" Label1.FontBold = True 'Use assignment statement 'to set value of variables... Label1.FontBold = True 'Use assignment statement 'to set value of variables... Dim Today As Date Today = "12/09/99" Dim Today As Date Today = "12/09/99"

Example 2Example 2Text1.Text = "Fred" 'Text1.Text is assigned the value "Fred “Text1.Text = "Fred" 'Text1.Text is assigned the value "Fred “Text2.text = "Smith" 'Text2.Text is assigned the value "Smith" Text2.text = "Smith" 'Text2.Text is assigned the value "Smith" Text1.Text = Text2.textText1.Text = Text2.text ‘ ‘Contents of text2 is copied onto Text1 ‘Contents of text2 is copied onto Text1 ‘Therefore Text1.Text = "Smith" 'AND Therefore Text1.Text = "Smith" 'AND Text2.text = "Smith" Text2.text = "Smith"

Option ExplicitOption Explicit

It is a good idea in Visual Basic to force It is a good idea in Visual Basic to force allall variables to be declared. For example using a variables to be declared. For example using a variablevariable which is undeclared which is undeclared

i.e. no dim declaration results in errors occuring within the application during i.e. no dim declaration results in errors occuring within the application during run-timerun-time

Go to the Go to the GeneralGeneral and and DeclarationsDeclarations section of the code window and section of the code window and Type inType in Option ExplicitOption Explicit as shown in the diagram: as shown in the diagram:

Reserved WordsReserved Words

These are words that are used to create the language structure for VB. These are words that are used to create the language structure for VB. The VB language reference provides a listing of The VB language reference provides a listing of

these words which these words which mustmust adhere to the adhere to the syntacticsyntactic rules of the language. rules of the language. The words form the body of the language itself The words form the body of the language itself

and are predominantly based upon and are predominantly based upon logiclogic. . Most of the Most of the Reserved WordsReserved Words have rules and must be defined in the have rules and must be defined in the

required or correct format. required or correct format.

Below are some common Below are some common Reserved wordsReserved words that are used in Visual that are used in Visual Basic. Most of these will be used regularly during Basic. Most of these will be used regularly during

the course. the course. CASECASE SELECTSELECT FORFOR TOTO

LESSON FOURLESSON FOUR

Label ControlsLabel Controls

A A LabelLabel control is used primarily to display control is used primarily to display information on to a form. By information on to a form. By defaultdefault any label any label placed on a form.placed on a form.

The label propertiesThe label properties

PropertyProperty DescriptionDescription CaptionProperty CaptionProperty to hold the textual to hold the textual

appearance of the objectappearance of the object

The The LabelLabel control can respond to a full set of control can respond to a full set of events, however, the events, however, the labellabel control control does not does not accept user inputaccept user input

Label ControlsLabel Controls

Example of the code used to change the Example of the code used to change the CaptionCaption property of a property of a LabelLabel: :

Label1.Caption = "Hello" Label1.Caption = "Hello"

'change caption property 'change caption property

Label1.FontBold = True Label1.FontBold = True

'add bold property 'add bold property

Label1.FontSize = 12 Label1.FontSize = 12

'change Font size property 'change Font size property

Text boxText box

The The texttext box is used to allow the user to box is used to allow the user to enter some enter some inputinput. The input can be used . The input can be used to enter values to match the required to enter values to match the required specification. specification.

Text boxText box

The caption The caption "Text1""Text1" in its caption property in its caption property Clearing the Caption property using the Clearing the Caption property using the

properties window or properties window or Changing the property using Visual Basic Changing the property using Visual Basic

code code

Clear text captionClear text caption

KeyPress EventKeyPress Event

Step 1Step 1 - Double Click on a - Double Click on a text boxtext box, to reveal the , to reveal the Code window.  The Code window.  The code windowcode window will reveal the will reveal the name of the object (Text1) and the procedure name of the object (Text1) and the procedure (Change event). This is illustrated in the diagram (Change event). This is illustrated in the diagram below: below:

Step 2Step 2 - Select the - Select the keypresskeypress event by dragging event by dragging

down the list and selecting the event.   This is down the list and selecting the event.   This is illustrated in the diagram below: illustrated in the diagram below:

Now enter the following code into the Now enter the following code into the KeyPressKeyPress event: event: Private Sub Text1_KeyPress(KeyAscii As Integer) Private Sub Text1_KeyPress(KeyAscii As Integer) If (KeyAscii = 13) Then If (KeyAscii = 13) Then

If Text1.Text = "" Then If Text1.Text = "" Then MsgBox "Please enter your name" MsgBox "Please enter your name"

Else Else MsgBox "Hi there " + Text1.Text MsgBox "Hi there " + Text1.Text

End If End If End If End If

End Sub End Sub

Command buttonCommand button

Command buttonsCommand buttons are different to labels are different to labels and text boxes. They are used in the event and text boxes. They are used in the event of user responses to navigate through of user responses to navigate through forms or accept responses.   forms or accept responses.  

Command buttonCommand button

A A command buttoncommand button is placed on to the is placed on to the form. The reason of having this object is form. The reason of having this object is that once the user has completed entering that once the user has completed entering their name, they Click on the their name, they Click on the command command buttonbutton to give control back to the to give control back to the application. application.

Program exampleProgram example

Private Sub Command1_Click() Private Sub Command1_Click()

If Text1.Text = "" Then If Text1.Text = "" Then

'if text box blank? 'if text box blank?

MsgBox "Please enter the MsgBox "Please enter the name" name"

'Display message 'Display message

Else 'if not blank? Else 'if not blank?

MsgBox "Hi there " + MsgBox "Hi there " + Text1.Text Text1.Text

'Display message and 'Display message and name name

End If End If

End Sub End Sub