1 / 50

CONTROL STATEMENTS

CONTROL STATEMENTS. 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 < 100 Comparison Operators

Télécharger la présentation

CONTROL STATEMENTS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CONTROL STATEMENTS

  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 true, other wise it results in false. Here’s a simple conditional expression: • Price < 100 Comparison 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.

  3. 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 value1 Block of one or more VB statements Case value2 Block of one or more VB Statements Case value3 Block of one or more VB statements Case value4 . Case Else Block of one or more VB Statements End Select

  4. 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 vbSunday Form2.Print “sunday” Case vbMonday Form2.Print “Monday” Case vbTuesday Form2.Print “Tuesday” Case vbWednesday Form2.Print “Wednesday” Case vbThursday Form2.Print “Thursday” Case vbFriday Form2.Print “Friday” Case vbSaturday Form2.Print “Saturday” End Select End Sub

  5. 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…Else If condition Then Block of VB Statements Else Block of VB Statements End If

  6. If Then Else • Private Sub Command1_Click() If Val(Text1.Text) > 50 Then • Form1.Print “pass” Else • Form1.Print “fail” End If • End Sub

  7. If Then Else

  8. 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 10 Form1.Print “ the value of i is”; i Next End Sub

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

  10. 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 = 5 Do While i > 0 Form1.Print “i is “ & i i = i – 1 loop End Sub

  11. 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 condition Block of one or more VB statements Loop 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 = 0 Form1.Print “i is “ & i i = i - 1 loop End Sub

  12. MENUS AND TOOL BARS • Menu options can be enabled, disabled, checked and popped up on an object • The Menu Object • The Menu Editor • The Caption Property • The Checked Property • The Enabled Property

  13. How to create a Menu 1. Start a new project by choosing file and then new project 2. Choose Standard EXE as the Project type 3. 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 editor 8. Give the caption as &square (square becomes the menu item) • Type a name . Let it be mnusquare. This is user defined.

  14. How to create a Menu 10. 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 Then Form1.mnusquare.Checked = True Else Form1.mnusquare.Checked = False End If End Sub

  15. Menu editor

  16. Structure of menu

  17. Associating Toolbars for the menu 1. Add the tool bar control to your form 2. Follow the steps to add the tool bar control to your form 3. Right Click on the tool box 4. 4.A pop up menu will be displayed 5. Choose Components menu item 6. The Components Dialog Box will appear 7. Check Microsoft Windows Common Controls 5.0 (SP2) 8. Click ok 9. Steps 3 to 8 includes the tool bar control in the tool box 10. Add the tool bar control to your form by clicking on it. 11. The default name of the tool bar control would be Toolbar1 12. Set the align property.

  18. Associating Toolbars for the menu 13. By default it would be 1- vbAlignTop 14. Select the Custom properties of the toolbar object by Clicking 15. The Property Pages Dialog box would appear. 16. Choose the Buttons tab page 17. 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

  19. 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.

  20. 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

  21. 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 Then Check1.Value = 1 Exit Sub End If If a Or j Then Check1.Value = 2 Exit Sub End If If Not a And Not j Then Check1.Value = 0 Exit Sub End If End Sub 5. Handle click event for check2 control Private Sub Check2_Click() If Check2.Value = 1 Then Form1.a = True Else Form1.a = False End If End Sub

  22. 6. Handle click event for check3 control Private Sub Check3_Click() If Check3.Value = 1 Then Form1.j = True Else Form1.j = False End If End Sub Initialize as follows Private Sub Form_Load() If Form1.a Then Check2.Value = 1 Else Check2.Value = 0 End If If Form1.j Then Check3.Value = 1 Else Check3.Value = 0 End If End Sub

  23. 7. Execute the project 8. 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 = 2 9. 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 = 0 10. 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

  24. 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 Box The 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 Integer i = 80 MsgBox “i is “ & i, vbOKCancel, “demo” End Sub

  25. 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. Execute Private Sub Command1_Click() Dim r As String r = InputBox(“name”) MsgBox “hello “ & r End Sub

  26. 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

  27. 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.

  28. 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 default 6. Handle click event for the file list box object. The object would be File1 by default 7. CurDir$ is a Visual Basic keyword. It is used to denote the path of the current directory, drive and file.

  29. 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 Sub 9. Handle change event for the Drive1 object. Code the handler as given below. Private Sub Drive1_Change() Dir1.Path = Drive1.Drive End Sub Dir1.Path sets the path of the directory from the current drive.

  30. 10. Handle change event for the Dir1 object. Code the handler as given below. Private Sub Dir1_Change() File1.Path = Dir1.Path End Sub File1.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.

  31. File System Control

  32. 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

  33. MDI Window

  34. 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 explorer 4. Select Add and then select Windows Form 5. 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()

  35. 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.

  36. 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

  37. 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.

  38. 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 displayed 7. 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.

  39. 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

  40. DATA CONTROL • 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. • You can set this in the data control property. The syntax is Form.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”

  41. The second property is the Connect property. • The default is Access. • If you click the drop down list box you’ll observe that you can connect to Paradox, Dbase, Excel, Lotus and ODBC. • You need to set the Connect property. • The third property is the Recordset property. • It can be a table, a dyna set type record set or a snap shot type record set. • The Record Source can be a table or a SQL statement.

  42. Displaying The Database • Text box, check box, image box, labels, picture box can be bounded to the data • control for displaying the data base contents. • Two properties are essential for binding to the data control. • The DataSource and the DataField are the essential properties for binding to the data control. • The DataSource refers to the database and the DataField refers to the specific field of the record. • 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. • Txtcust. DataField = “customer name”

  43. 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 form 4. Set the DatabaseName property as given below Data1.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 the Customer table. 9. Run the project. 10. The records of the Customer table would be displayed.

  44. 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.

  45. 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.

  46. 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

  47. 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.

More Related