1 / 28

CP2028 Visual Basic Programming 2

CP2028 Visual Basic Programming 2. Week 1 Lecture 1 & 2 Introduction to the module The VB module set Contents and structure of this module Review of the Visual Basic environment Review of programming practices Variables & Scope. CP2028 Module Aims and Objectives.

Télécharger la présentation

CP2028 Visual Basic Programming 2

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. CP2028Visual Basic Programming 2 • Week 1 Lecture 1 & 2 • Introduction to the module • The VB module set • Contents and structure of this module • Review of the Visual Basic environment • Review of programming practices • Variables & Scope

  2. CP2028 Module Aims and Objectives • Reinforce the skills and knowledge gained in VB1. • Further develop programming abilities. • Topics covered include: • storing and accessing information in files • using Databases within VB. • File handling • error handling

  3. The Visual Basic module set. CP2028 VB2 CP1007 VB1 CP3013 App Dev in VB CP2030 VB For C++ CP1000 S.P. in C++

  4. Introduction to the Module [1]The Module Guide • The module consists of 12 weeks of study, plus a revision week. • As in VB1 the aim is to increase your skills and knowledge in program design and development. • Visual Basic is the target language, but the skills gained are applicable to other languages.

  5. Introduction to the Module [2] • Timetable details (see module guide) • 2 x 1-hour lectures per week • Tutorial • Workshop • Weekly contents • Assessments • Coursework • Exam

  6. Review of V.B. Environment • Identify the components of the VB design environment? ? ? ? ? ?

  7. Review of V.B. Environment • Identify the components of the VB design environment? Menu Bar ? Code

  8. Review of V.B. Environment • Identify the components of the VB design environment? ? ? ? ? ?

  9. Review of V.B. Environment • Identify the components of the VB design environment? Menu Bar Project Window Control Toolbox Properties Window Form

  10. Visual Basic Program Structure. • Project File • ‘.VBP’ • Form Files • ‘.FRM’ • ‘.FRX’ • Modules • ‘.BAS’ • Custom Controls • ‘.VBX’ files

  11. Control Bar - How many controls can you identify? Standard Edition Professional Edition

  12. Control bar - Properties Run Time Vs Design Time (Startup Defaults) Name Setting control positions when place on form Vs via the properties window What if we want to resize our forms do we resize controls? How? Are there tools to do it?

  13. Review of Event Driven Programming • Application program is composed of a number of subroutines, which are triggered by events within the environment. • Typical events include • Mouse-click • Keyboard use • Field value changes • Events happen to a control. • Mostly user generated events • Controls can also cause events

  14. Visual Basic Event Processing Trigger Event Code Executed

  15. Types of Events • Events can be classified as: • User generated • (e.g. command button click) • Computer generated • (e.g. specific time elapsed, from a timer control) • Program generated • (i.e. program explicitly generates an event from within the code)

  16. Visual Basic Events • The events that can happen to a control are pre-determined • Each type of control has a relevant set of events • The events that can happen to a Command Button

  17. Programming Practices • We need to consider • Control naming conventions • Variable naming conventions • Code documentation

  18. Scope of Variables Form1 General Declarations • Shows declarationsat form level, knownas: General Declarations • Shows variabledeclarations withinan event handler Dim sName1 As String Dim iNum1 As Integer Sub Command1_Click () Dim sName2 As String Dim iNum2 As Integer Available variables:sName1, sName2, iNum1, iNum2 Sub Command2_Click () Dim sName3 As String Dim iNum3 As Integer Available variables:sName1, sName3, iNum1, iNum3

  19. Scope • The general rule is to declare variables at the lowest possible level. • Ie Control level Form level Module level

  20. Question Calculator Create a front end for a simple calculator, You should be able to accept two numbers and perform addition, subtraction, multiplication and division.

  21. Question calculator • Try the calculator with 5 divide 0 !!! • Why is the application not working ? • How can we correct the problem ?

  22. Solution

  23. Code

  24. code

  25. Static Variables • A static variable will hold its value when it goes out of scope:Sub Command1_Click() 'declare variablesDim iDimCount As IntegerStatic iStaticCount As Integer 'increment variables iDimCount = iDimCount + 1 iStaticCount = iStaticCount + 1'display variables Label3.Caption = Str$(iDimCount) Label4.Caption = Str$(iStaticCount)End Sub • A static variable can only be declared inside a procedure • Note the use of a comment • Note use of Str$ function

  26. Static Variables: Effects • The effect of using a static variable can be seen below: • This is has the same effect as if the variable had been declared at the form’s general declaration level • Except the scope is local to the procedure

  27. CP2028Visual Basic programming 2 • Week 1 - Summary • Structure of this module and its position in the VB module set. • Review of VB environment. • Review of event-driven programming

  28. End of Lecture Click House to Return to Main Menu

More Related