300 likes | 560 Vues
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.
E N D
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
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
The Visual Basic module set. CP2028 VB2 CP1007 VB1 CP3013 App Dev in VB CP2030 VB For C++ CP1000 S.P. in C++
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.
Introduction to the Module [2] • Timetable details (see module guide) • 2 x 1-hour lectures per week • Tutorial • Workshop • Weekly contents • Assessments • Coursework • Exam
Review of V.B. Environment • Identify the components of the VB design environment? ? ? ? ? ?
Review of V.B. Environment • Identify the components of the VB design environment? Menu Bar ? Code
Review of V.B. Environment • Identify the components of the VB design environment? ? ? ? ? ?
Review of V.B. Environment • Identify the components of the VB design environment? Menu Bar Project Window Control Toolbox Properties Window Form
Visual Basic Program Structure. • Project File • ‘.VBP’ • Form Files • ‘.FRM’ • ‘.FRX’ • Modules • ‘.BAS’ • Custom Controls • ‘.VBX’ files
Control Bar - How many controls can you identify? Standard Edition Professional Edition
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?
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
Visual Basic Event Processing Trigger Event Code Executed
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)
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
Programming Practices • We need to consider • Control naming conventions • Variable naming conventions • Code documentation
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
Scope • The general rule is to declare variables at the lowest possible level. • Ie Control level Form level Module level
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.
Question calculator • Try the calculator with 5 divide 0 !!! • Why is the application not working ? • How can we correct the problem ?
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
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
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
End of Lecture Click House to Return to Main Menu