control statements. a conditional expression uses a comparison operator which results in true or...

50
CONTROL STATEMENTS

Upload: loraine-reynolds

Post on 29-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

CONTROL STATEMENTS

Page 2: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

CONTROL STATEMENTS• A conditional expression uses a comparison operator which results

in true or false• value. If the comparision is valid it results in true, other wise it

results in false. Here’s a simple conditional expression:• Price < 100Comparison Operators• = Equal to• < > Not equal to• > Greater than• < Less than• > = Greater than or equal to• < = Less than or equal to• Select case, If then Else, For .. Next repetition statement, Do While

loop statement and Do Until loop control statements are explained.

Page 3: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

1 Select Case

• Select Case is used for handling multiple paths. Select Case evaluates the expression and depending on the value the corresponding Case statement executes.Select Case expression

Case value1Block of one or more VB statementsCase value2Block of one or more VB StatementsCase value3Block of one or more VB statementsCase value4 . Case ElseBlock of one or more VB Statements

End Select

Page 4: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Type the following coding in the first command button event handler Private Sub Command1_Click() Form2.Cls End Sub

Type the following coding in the second command Select Case Weekday(Now) Case vbSundayForm2.Print “sunday” Case vbMondayForm2.Print “Monday”Case vbTuesdayForm2.Print “Tuesday” Case vbWednesdayForm2.Print “Wednesday” Case vbThursdayForm2.Print “Thursday” Case vbFridayForm2.Print “Friday”Case vbSaturdayForm2.Print “Saturday”End SelectEnd Sub

Page 5: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in
Page 6: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

If Then Else

• The control statement If Then Else is used for evaluating a condition and taking action based on the condition. If the condition is true the then part will execute otherwise the else block will execute.If .. Then…ElseIf condition ThenBlock of VB StatementsElseBlock of VB StatementsEnd If

Page 7: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

If Then Else

• Private Sub Command1_Click() If Val(Text1.Text) > 50 Then

• Form1.Print “pass” Else

• Form1.Print “fail” End If

• End Sub

Page 8: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

If Then Else

Page 9: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

For ..... Next Repetition Statement

To illustrate the working of for .. next control statement, place a command button and a label control on the form. Handle click event for the command button. Type the coding

given below as the event handler.

Private Sub Command1_Click() Label1.Caption = “for loop illustration”

For i = 1 To 10Form1.Print “ the value of i is”; iNextEnd Sub

Page 10: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

• general syntax of for loop is as follows.• For index = start value to end value (Step value) One or more VB

statements• Next

Page 11: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Do While Loop Statement

The statements within the do…while loop block executes till the condition given in while is true

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

Dim i As Integer i = 5Do While i > 0Form1.Print “i is “ & i

i = i – 1loopEnd Sub

Page 12: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in
Page 13: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Do Until Loop Statement

• The statements within the do…until loop block executes till the condition given in until becomes true. The working of do…until loop is explained with the event Form_ Mouse Down.

Do Until conditionBlock of one or more VB statementsLoop

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

• Dim i As Integer i = 5

Do Until i = 0Form1.Print “i is “ & i i = i - 1

loopEnd Sub

Page 14: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in
Page 15: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

MENUS AND TOOL BARS

• Menu options can be enabled, disabled, checked and popped up on an object

1. The Menu Object

2. The Menu Editor

3. The Caption Property

4. The Checked Property

5. The Enabled Property

Page 16: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

How to create a Menu

1. Start a new project by choosing file and then new project2. Choose Standard EXE as the Project type3. Make the Form1 active by clicking on it.4. Select Tools and then Menu Editor to open the menu

editor for designing.5. Give the caption as &display (display becomes the

menu title)6. Type a name . Let it be Mnudisp. This is user defined.7. To add menu items click the next button in the menu

editor8. Give the caption as &square (square becomes the menu

item)9. Type a name . Let it be mnusquare. This is user defined.

Page 17: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

How to create a Menu10. Associate a short cut key. A list box will pop up. Choose a key. Say

you have selected Ctrl + G. Than, to activate this menu item you need to press CTRL and G keys together.

11. Set the checked property of the menu item square by clicking on the check box for the checked property.

12. Double click on the menu item. It takes you to the code window 13. Type the code given below for the menu item square

Private Sub mnusquare_Click()If (Form1.mnusquare.Checked) = False ThenForm1.mnusquare.Checked = TrueElseForm1.mnusquare.Checked = FalseEnd IfEnd Sub

Page 18: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Menu editor

Page 19: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Structure of menu

Page 20: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Associating Toolbars for the menu

1. Add the tool bar control to your form2. Follow the steps to add the tool bar control to your form3. Right Click on the tool box4. 4.A pop up menu will be displayed5. Choose Components menu item6. The Components Dialog Box will appear7. Check Microsoft Windows Common Controls 5.0 (SP2)8. Click ok9. Steps 3 to 8 includes the tool bar control in the tool box10. Add the tool bar control to your form by clicking on it.11. The default name of the tool bar control would be Toolbar112. Set the align property.

Page 21: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Associating Toolbars for the menu

13. By default it would be 1- vbAlignTop14. Select the Custom properties of the toolbar object by Clicking15. The Property Pages Dialog box would appear.16. Choose the Buttons tab page17. Click Insert Button. This would place the first button on the tool bar.18. Set the Style as 3-tbrSeparator from the drop down box.19. Click insert button to add a new button. Set the Key to menu item

(in this case square)20. You can specify the tool tip. (set it as draw)21. Repeat Steps 17 to 20 to add buttons for the menu items or titles .

Only the first button will be a separator. The rest can have the value 0-tbrDefault

Page 22: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Check Box

A check box control takes the value as on or off or grayed. A grayed one is neither on or off. The user can change the setting of the grayed check box.

Page 23: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Project to illustrate the use of check Box

1. Select General in the objects drop down list box and Declarations in the events drop down list box.

• 2. Declare 2 Boolean variables Public a As Boolean Public j As Boolean

• 3. Design the form with 3 check boxes• 4. Handle Mouse Up event for the check1

control and type the handler as given below

Page 24: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Project to illustrate the use of check Box

Private Sub Check1_MouseUp(Button As Integer, Shift As Integer, X As Single, YAs Single)If a And j ThenCheck1.Value = 1Exit SubEnd IfIf a Or j ThenCheck1.Value = 2Exit SubEnd IfIf Not a And Not j ThenCheck1.Value = 0Exit Sub End If End Sub

5. Handle click event for check2 controlPrivate Sub Check2_Click()If Check2.Value = 1 ThenForm1.a = TrueElseForm1.a = FalseEnd IfEnd Sub

Page 25: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

6. Handle click event for check3 controlPrivate Sub Check3_Click()If Check3.Value = 1 ThenForm1.j = TrueElseForm1.j = FalseEnd IfEnd SubInitialize as follows Private Sub Form_Load() If Form1.a Then Check2.Value = 1ElseCheck2.Value = 0End IfIf Form1.j ThenCheck3.Value = 1ElseCheck3.Value = 0End IfEnd Sub

Page 26: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

7. Execute the project8. If check2 or check3 is checked by clicking on it and then check1 is clicked it

would become grayed. In this case the public variables a or j would become 1. This snippet in MouseUp will execute

If a Or j Then Check1.Value = 29. If check2 and check3 are made empty by clicking on them when they are

clicked and then check1 is clicked it would become empty. In this case the public variables a and j would become 0. This snippet in MouseUp will execute

If Not a And Not j Then Check1.Value = 010. If check2 and check3 are checked by clicking on them then check1 would

become checked. In this case the public variables a and j would become 1. This snippet in MouseUp will execute

If a And j Then Check1.Value = 1

Page 27: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

DIALOG BOXES

Two dialog boxes, namely, the message box and the input box are used in Visual Basic.A message box is used for displaying messages to users and input box is used by the user for inputting values.

Message BoxThe Message Box can be given a title. To specify a title for

the message box follow the command given below.Private Sub Command1_Click() Dim i As Integeri = 80MsgBox “i is “ & i, vbOKCancel, “demo” End Sub

Page 28: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Input Box

1. Start a new project

2. Choose standard exe

3. Add a command button to the form

4. handle double click for the command button and code the handler as given below.

5. ExecutePrivate Sub Command1_Click() Dim r As String

r = InputBox(“name”) MsgBox “hello “ & r

End Sub

Page 29: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Input box

6. The input box asks for an input

7. Once the input is entered(say mca) hello mca would be displayed using the message box; & is used for string concatentation

Page 30: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

FILE SYSTEM CONTROLS

• The drive list box, directory list box and the file list box.

• The drive list box control is used to display the disk drives on the system such as A drive ,C drive ,D drive etc.

• The down arrow in the control helps you to visualize all the available drives in the system. The directory list box is used to display the directories available in the selected drive.

• The file list box displays all the files in the selected directory.

Page 31: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

A Project to illustrate the use of file system controls

1. Create a project.2. Drag the drive list box, directory list box and file list box

from the tool box and drop on the form.3. Place a text box on the form.4. Handle change event for the drive list box object. The

object would be Drive1 by default.5. Handle change event for the directory list box object.

The object would be Dir1 by default6. Handle click event for the file list box object. The object

would be File1 by default7. CurDir$ is a Visual Basic keyword. It is used to denote

the path of the current directory, drive and file.

Page 32: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

8. Handle form load event. Code the form_load handler as given below.

Private Sub Form_Load()Drive1.Drive = CurDir$ Dir1.Path = CurDir$ File1.Path = CurDir$ File1.Pattern = “*.*”End Sub9. Handle change event for the Drive1 object. Code the handler as

given below.Private Sub Drive1_Change() Dir1.Path = Drive1.DriveEnd SubDir1.Path sets the path of the directory from the current drive.

Page 33: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

10. Handle change event for the Dir1 object. Code the handler as given below.

Private Sub Dir1_Change() File1.Path = Dir1.PathEnd SubFile1.Path sets the path of the file from the current directory.

11. Handle click event for the File1 object. Code the handler as given below.

Private Sub File1_Click() Text1.Text = File1.FileName End Sub

File1.FileName returns the selected file’s name and that is stored in the text box.Text1.

Page 34: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

File System Control

Page 35: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

MULTIPLE DOCUMENT INTERFACE

(MDI) WINDOWS • Single document interface (SDI) support one

open window or document at a time. • Microsoft’s notepad and paint are examples of

single document interface. • SDI applications have limited capabilities. They

have limited image and text- editing features. • Multiple document interface allows users to edit

multiple documents at the same time

Page 36: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

MDI Window

Page 37: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Steps for Creating A MDI Form

1. Create a new form and set IsMdiContainer property to true.

2. Create a child form class to add to the form.3. Right click the project in the solution explorer4. Select Add and then select Windows Form5. Create a new child form object and set the MdiParent

property to the parent form.6. Invoke the child form’s Show method.7. Write the coding below to create the child form in a

event handler.Dim childForm As New ChildFormClass() childForm.MdiParent = parentForm childForm.Show()

Page 38: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

• The main window in a MDI program is the parent window.

• All the other windows of the application are child windows.

• A MDI program will have only one parent window and many child windows.

• Only one child window will be open at one time. Uses of child windows can vary.

• A child window could be used for editing images, the second child window could be used for editing text.

• Closing or minimizing the parent window automatically closes the child windows.

Page 39: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

CONTROL ARRAY

• A control array is a group of like objects with the same name.

• Adding controls with control arrays uses fewer resources than simply adding multiple controls of the same type to a form at design time.

• Control arrays are also useful if you want several controls to share code.

• For example, if three option buttons are created as a control array, the same code is executed regardless of which button was clicked

Page 40: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

• If you want to create a new instance of a control at run time, that control must be a member of a control array.

• With a control array, each new element inherits the common event procedures of the array.

• Using the control array mechanism, each new control inherits the common event procedures already written for the array.

• For example, if your form has several text boxes that each receive a date value, a control array can be set up so that all of the text boxes share the same validation code.

Page 41: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Steps to create a control array oftextbox.

1. Create a VB project.2. Click the command button and paste it on the form.3. Drag the Text Box control and place it on the form.4. Copy the Text Box control.5. Paste the copied Text Box control on the form.6. The following message would be displayed7. You already have a control named Text1’. Do you want

to create a control array?8. Select yes and the Text Box control would be pasted on

the form.

Page 42: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

9. Paste as many times as you want. For instance to create a control array with 3 elements paste the control 2 times.

10. Handle click event for the command button.

11. Include the following code in the event handler.Private Sub Command1_Click()

Dim an As Integer

For an = 0 To 2

Text1(an).Text = Val(Text1(an).Text) + 5

Next an

End Sub

Page 43: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

DATA CONTROL

1. The data control is used to connect to a database. Three properties are essential to connect to a database.

The first property is the database name. 1. You can set this in the data control property. The syntax isForm.datacontrol.DatabaseName = pathname

pathname is the location of the database file name. If the database is ex.mdb, and the data control is Data1 then to set the DatabaseName property do the following initialization.

Data1.DatabaseName = “C:\ex.mdb”

Page 44: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

1. The second property is the Connect property. 2. The default is Access.3. If you click the drop down list box you’ll

observe that you can connect to Paradox, Dbase, Excel, Lotus and ODBC.

4. You need to set the Connect property. 5. The third property is the Recordset property. 6. It can be a table, a dyna set type record set or a

snap shot type record set.7. The Record Source can be a table or a SQL

statement.

Page 45: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Displaying The Database

1. Text box, check box, image box, labels, picture box can be bounded to the data

2. control for displaying the data base contents.3. Two properties are essential for binding to the data

control.4. The DataSource and the DataField are the essential

properties for binding to the data control. 5. The DataSource refers to the database and the

DataField refers to the specific field of the record.6. If a text box is used for displaying the names of the

customer then the DataField property of the text box should be initialized as follows.

7. Txtcust. DataField = “customer name”

Page 46: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

A Project to Display a Database

1. Assume there are 5 fields describing a customer. Id, name, address, city and state.

2. Place 5 text boxes on the form to display the records.3. Place a data control on the form4. Set the DatabaseName property as given belowData1.DatabaseName = “C:\ex.mdb”5. Set the Record Source for the data control as Customer by selecting the

table from the drop down list box in the property box.6. Set the Record Type to 0 to indicate Customer table of ex.mdb .7. Bind the text boxes to the data control. This is achieved by setting the Data

Source to Data1 for all the text boxes one at a time.8. Set DataField property for each text box and set to the different fields of theCustomer table.9. Run the project.10. The records of the Customer table would be displayed.

Page 47: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

The Refresh Method

• The refresh method moves to the first record in the record set. To invoke the refresh method follow the syntax given below.

Form1.Data1.Refresh

• Adding Records• 1. As in the previous case Set the DatabaseName

property as “C:\ex.mdb”, Set the Record Source for the data control as Customer and Set the Record Type to 0.

• 2. Design a form with 5 text boxes and 2 command buttons new and update.

Page 48: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Adding Records

Set the DataSource to Data1 for all the text boxes.4. Set the DataField property for each text box to

the different fields of the Customer table.5. Handle Click event for the command button

new and type the handler as given below

data1.RecordSource = “Customer” data1.Refresh data1.Recordset.AddNew

Add New adds a buffer in memory for a new record.

Page 49: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Adding Records

Handle Click event for the command button update and type the handler as given below

data1.Recordset(“Id”) = CInt(text1.Text)

data1.Recordset(“name”)= text2.Text

data1.Recordset(“address”)= text3.Text

data1.Recordset(“city”)= text4.Text

data1.Recordset(“state”)= text5.Text

data1.Recordset.Update

Update writes the record into the database

Page 50: CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in

Deleting Records

• To delete a record use the Delete method. To delete a record from the customer table follow the coding given below.data1.RecordSource = “Customer”

data1.Recordset.Delete

• The above coding would delete the current record.