1 / 47

Visual Basic 6.0

Visual Basic 6.0. 1430/1431 2 nd term. Course Objectives. Understand the benefits of using Microsoft Visual Basic 6.0 for Windows as an application tool. Understand the Visual Basic event-driven programming concepts, terminology , and available tools.

evadne
Download Presentation

Visual Basic 6.0

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. Visual Basic 6.0 1430/1431 2nd term

  2. Course Objectives • Understand the benefits of using Microsoft Visual Basic 6.0 for Windows as an application tool. • Understand the Visual Basic event-driven programming concepts, terminology, and available tools. • Learn the fundamentals of designing, implementing, and distributing a Visual Basic application. • Learn to use the Visual Basic toolbox. • Learn to modify object properties. • Learn object methods. • Use the menu design window. • Understand proper debugging and error-handling procedures. • Gain a basic understanding of database access and management using data bound controls. • Obtain an introduction to ActiveX controls and the Windows Application Programming Interface (API).

  3. What is Visual Basic? • Visual Basic Is a tool that allows you to develop Windows (Graphic User Interface - GUI) applications. The applications have a familiar appearance to the user.

  4. What is Visual Basic? • Visual Basic is event-driven, meaning code remains idle until called upon to respond to some event (button pressing, menu selection, ...). Visual Basic is governed by an event processor. Nothing happens until an event is detected. Once an event is detected, the code corresponding to that event (event procedure) is executed. Program control is then returned to the event processor.

  5. Some Features of Visual Basic • Full set of objects - you 'draw' the application • Lots of icons and pictures for your use • Response to mouse and keyboard actions • Clipboard and printer access • Full array of mathematical, string handling, and graphics functions • Can handle fixed and dynamic variable and control arrays • Sequential and random access file support • Useful debugger and error-handling facilities • Powerful database access tools • ActiveX support • Package & Deployment Wizard makes distributing your applications simple

  6. Structure of a Visual Basic Application Get back to Contents

  7. Structure (continued) Form Control Button Window Get back to Contents

  8. Structure (continued) Form Frame Window Option Button Command Button Get back to Contents Label

  9. The code window

  10. Contents of the Application (the Project) • Forms - Windows that you create for user interface • Controls - Graphical features drawn on forms to allow user interaction (text boxes, labels, scroll bars, command buttons, etc.) Forms and Controls are objects.) • Properties - Every characteristic of a form or control is specified by a property. Example properties include names, captions, size, color, position, and contents. Visual Basic applies default properties. You can change properties at design time or run time. • Methods - Built-in procedure that can be invoked to impart some action to a particular object. • Event Procedures - Code related to some object. This is the code that is executed when a certain event occurs. • General Procedures - Code not related to objects. This code must be invoked by the application. • Modules - Collection of general procedures, variable declarations, and constant definitions used by application.

  11. User Interface Main Window Project Explorer Tool Box Properties Window Project Window Form Layout

  12. Main Window • The Main Window consists of the title bar, menu bar, and toolbar.

  13. Start Visual Basic 6

  14. Contents of the Main Window of VB6 The menu bar has drop-down menus from which you control the operation of the Visual Basic environment. The toolbar has buttons that provide shortcuts to some of the menu options.

  15. Form layout window Code editor tasks Project explorer Visual component manager Add form Open project Pause Toolbox Menu editor Run Save project Object browser Add project Stop Properties window Data view window

  16. ToolboxThe Toolbox is the selection menu for controls used in yourapplication.

  17. Properties window The Properties Window is used to establish initial property values for objects. The drop-down box at the top of the window lists all objects in the current form. Two views are available: Alphabetic and Categorized. Under this box are the available properties for the currently selected object.

  18. The Form Layout Windowshows where (upon program execution)your form will be displayed relative to your monitor’s screen: The Project Window displays a list of all forms and modules making up your application. You can also obtain a view of the Form or Code windows (window containing the actual Basic coding) from the Project window.

  19. Example: Stopwatch application The application is used to calculate the time elapsed between two events. The user starts a timer, once the second event tops, the user stops the timer, the time duration is displayed. The steps are: • Drawing controls • Setting properties of the controls • Writing code

  20. 1. Drawing Controls • Start new project • Draw 3 command buttons on the form • Draw 6 labels on the form

  21. 2. Setting properties of the controls The properties of the controls can be modified in two different ways: • Setting properties during design time. In this way you click on the object (form or control) in the form window, then click on the Properties Window or the Properties Window button in the tool bar. • Setting properties during run time. In this way you can set or modify properties while your application is running. To do this, you must write some code. The code format is: ObjectName.Property = NewValue Such a format is referred to as dot notation. For example, to change the BackColor property of a form name frmStart, you may type: frmStart.BackColor = BLUE

  22. Setting properties during design time • Here we selected the form window and clicked on the properties window. Notice that the from name here (which is the most important property) is frmStopWatch. • Object names can be up to 40 characters long, must start with a letter, must contain only letters, numbers, and the underscore (_) character.

  23. Setting Properties in stopwatch example The following table shows the properties you are going to change for the objects you put on the form. Property Object

  24. Writing Code • Variable Declaration • Variables are used by Visual Basic to hold information needed by the application. • Rules used in naming (declaring) variables: • No more than 40 characters • They may include letters, numbers, and underscore (_) • The first character must be a letter • You cannot use a reserved word (word needed by Visual Basic)

  25. Visual Basic Data Types

  26. There are three ways for a variable to be typed (declared):1. Default2. Implicit3. ExplicitIf variables are not implicitly or explicitly typed, they are assigned the variant type by default. The variant data type is a special type used by Visual Basic that can contain numeric, string, or date data. Private Sub Command1_Click() a% = Text1.Text Text2.Text = a ^ 2 End Sub Private Sub Command1_Click() Static a As Integer a = Text1.Text Text2.Text = a ^ 2 End Sub Implicit declaration of the variable a.

  27. Option Explicit Dim a As Integer Private Sub Command1_Click() a = Text1.Text Text2.Text = a ^ 2 End Sub Private Sub Command3_Click() a = Text1.Text Text3.Text = 2 * a + 5 End Sub Explicit declaration of the variable a.

  28. Variable Scope

  29. Global Declaration of a variable • For the variable to be declared globally, it should be declared in a module, to create a module,

  30. How to Create a Module • In the module declare the variables by writing the following line: • Global a, b, c As Long • Dim c As Integer • In this way the variables will be kept in the whole code as they been defined.

  31. The Visual Basic Language • Visual Basic Statements and Expressions The simplest statement is the assignment statement. It consists of a variable name, followed by the assignment operator (=), followed by some sort of expression. • StartTime = Now • Explorer.Caption = "Captain Spaulding" • BitCount = ByteCount * 8 • Energy = Mass * LIGHTSPEED ^ 2 • NetWorth = Assets – Liabilities Statements normally take up a single line with no terminator. Statements can be stacked by using a colon (:) to separate them. Example: • StartTime = Now : EndTime = StartTime + 10

  32. Visual Basic Operators arithmetic operators: • The simplest operators carry out arithmetic operations. These operators in their order of precedence are: Operator Operation • ^ Exponentiation • * / Multiplication and division • \ Integer division (truncates) • Mod Modulus • + - Addition and subtraction Parentheses around expressions can change precedence. comparison operators: Operator Comparison • > Greater than • < Less than • >= Greater than or equal to • <= Less than or equal to • = Equal to • <> Not equal to The result of a comparison operation is a Boolean value (True or False).

  33. logical operators Operator Operation • Not Logical not • And Logical and • Or Logical or • The Not operator simply negates an operand. • The And operator returns a True if both operands are True else, it returns a False. • · The Or operator returns a True if either one of its operands is True, else it returns a False. Logical operators follow arithmetic operators in precedence.

  34. Visual Basic Functions Some of the built in visual basic functions are given here for example

  35. Visual Basic Symbolic Constants Functions and objects always require data arguments that affect their operation and return values you want to read and interpret. These arguments and values are constant numerical data and difficult to interpret based on just the numerical value. To make these constants more understandable, Visual Basic assigns names to the most widely used values, these are called symbolic constants. You can find such constants here.

  36. MsgBox Arguments Constants

  37. Example: Finding the roots of the quadratic equation Find the roots of the quadratic equation Given the parameters a, b, and c. Check if the parameters give imaginary roots, if any, find the absolute values. The interface should be like shown

  38. Example: Temperature conversion The Fahrenheit temperature is converted into Celsius by the following formula The interface should look like shown here

  39. Creating File for Editing A file is being creating for input, output and as file editor. • Create new project • On the form draw a textbox as large as the size of the form • Add from the tool box a COMMON DIALOG BOX. If it is not there, follow the steps below • From PROJECT menu select components • Scroll the components to MICROSOFT DIALOG BOX and check the box . • It is now on the toolbox bar. • Notice its name which you will use in the code. • Now, use from TOOLS the menu editor to add (file) and (format) menus.

  40. File Menu • Use the MNUE EDITOR to write the file menu components. • The code: • Exit code • Click on exit in the created file menu, and start writing the code for ending the run. 'Make sure user really wants to exit Dim Response As Integer Response = MsgBox("Are you sure you want to exit the note editor?", vbYesNo + vbCritical + vbDefaultButton2, "Exit Editor") If Response = vbNoThen Exit Sub Else End End If

  41. New code Click on New in the created file menu, and start writing the code for creating new file. 'If user wants new file, clear out text Dim Response As Integer Response = MsgBox("Are you sure you want to start a new file?", vbYesNo + vbQuestion, "New File") If Response = vbYes Then txtEdit.Text = "" Else End If

  42. Save code Click on Save in the created file menu, and start writing the code for saving the created file with extension ned (it is arbitrary as long as you don’t select a known format. The common dialog box here is named cdlFiles. cdlFiles.Filter = "Files (*.ned)|*.ned" cdlFiles.DefaultExt = "ned" cdlFiles.DialogTitle = "Save File" cdlFiles.Flags = cdlOFNOverwritePrompt + cdlOFNPathMustExist On Error GoToNo_Save cdlFiles.ShowSave Open cdlFiles.FileName For Output As #1 Print #1, Val(txtEdit.Text) Close 1 Exit Sub No_Save: Resume ExitLine ExitLine: Exit Sub

  43. Open code Click on Open in the created file menu, and start writing the code for opening a saved file with extension ned (it is arbitrary as long as you don’t select a known format. The common dialog box here is named cdlFiles. cdlFiles.Filter = "Files (*.ned)|*.ned" cdlFiles.DefaultExt = "ned" cdlFiles.DialogTitle = "Open File" cdlFiles.Flags = cdlOFNFileMustExist + cdlOFNPathMustExist + cdlOFNHelpButton On Error GoToNo_Open cdlFiles.ShowOpen Open cdlFiles.FileName For Input As #1 txtEdit.Text = Input(LOF(1), 1) Close 1 Exit Sub No_Open: Resume ExitLine ExitLine: Exit Sub

  44. Format Menu • Use the MNUE EDITOR to write the format menu components (Bold, Underline, Italic, Size, Small, Medium and Large). The code: Bold code Click on Bold in the created format menu, and start writing the code for Bold. mnuFmtBold.Checked = Not (mnuFmtBold.Checked) txtEdit.FontBold = Not (txtEdit.FontBold) The code: Underline code Click on Underline in the created format menu, and start writing the code for Underline. mnuFmtUnderline.Checked = Not (mnuFmtUnderline.Checked) txtEdit.FontUnderline = Not (txtEdit.FontUnderline)

  45. The code: Italic code Click on Italic in the created format menu, and start writing the code for Italic. • mnuFmtItalic.Checked = Not (mnuFmtItalic.Checked) • txtEdit.FontItalic = Not (txtEdit.FontItalic) The code: Small code Click on Size submenuSmall in the created format menu, and start writing the code for giving the font a small size . Do the same steps for both Medium and Large submenus, giving in each case the value of the font to be 12 and 18, respectivily. mnuFmtSizeSmall.Checked = True mnuFmtSizeMedium.Checked = False mnuFmtSizeLarge.Checked = False txtEdit.FontSize = 8

More Related