1.6k likes | 2.09k Views
Foundation of Computer Programming Visual Basic 6.0 Programming. Instructor : Mr. Nguyen Cao Tri caotri@dit.hcmut.edu.vn http://www.dit.hcmut.edu.vn/~caotri Documents : Course syllabus, lecture’s slide, samples can be found at http:// www.dit.hcmut.edu.vn/~caotri/VB6.
E N D
Foundation of Computer ProgrammingVisual Basic 6.0 Programming Instructor: Mr. Nguyen Cao Tri caotri@dit.hcmut.edu.vn http://www.dit.hcmut.edu.vn/~caotri Documents: Course syllabus, lecture’s slide, samples can be found at http://www.dit.hcmut.edu.vn/~caotri/VB6
Course Information • Course objectives: • Understand basic theories on Computer software programming • Understand Microsoft Visual Basic 6.0 syntax and its major features • Design and develop GUI Windows applications • Develop critical thinking of software programming • Text book/reading material: • Visual Basic 6.0 how to program ,Deitel & Deitel • Cyber classroom CD :Visual Basic 6.0 how to program • Internet Web site access
Introduction Computing concepts Programming concepts Flowchart
Computing Concepts CPU • Computer • Processor • I/O Devices • Storage system: Primary & Secondary • Operating system • User interface: command line , GUI • Programming concepts: • What’s Program • Programming languages: machine code, high level languages • Developing environmental • Algorithms I/O devices Storage System
Programming languages & IDE • Programming languages • GO TO Programming : basic, assembly • Structured Programming: Pascal, C,.. • Object Programming: Java, C++ • Visual approach for GUI programming. • Compiler • Interpreter • Compiler • Integrated Development Environment (IDE) • Project manager, Code editor, debug, compiler
Object Oriented Programming (OOP) • What’s Object? • Object properties • Object method • What’s Class? • Message/Even • Object vs. Structure • How can an OOP program works? • Object and Visual Basic Object Name (ID) Properties Methods Object 1 Object 1 Messages Object 1
Algorithms & Flowchart • What’s algorithms • How to present an algorithms? • Natural language, Programming languages • Others? • Flowchart • Characteristic: picture base, chart base • Capability • Advantage: • Simple • Easy to understand • Powerful • Better view and control of Flowchart
Flowchart components • Start or Begin of algorithms. One START point only. Start • Process Flow • Input / Output data • Process description / data calculation No Condition • Process Direction Control Yes Evaluation Item • Other Process direction Control Value 1 Value i Others Stop • Stop or End of algorithms. One/Multi STOP point.
Flowchart Example Algorithms ax + b=0 equation solving. Start Read a, b a=0 ? b=0 ? Yes Yes No No X=-b/a Any number is solution No Solution Stop
Control Structures in programming languages • If <condition> then Statement; • If <condition> then Statement 1 else Statement 2; • Case <Value> of value 1 : Statement 1; ……….. value n : Statement n; else : Statement 0 end; • While <condition> do Statement; • Repeat Statement until <condition>; • For counter=start value to end value do Statement; • For counter=start value downto end value do Statement;
Other terms in computer programming • Data & Data type • Basic data type • integer, long, character, byte, …. • Real (double, float) • Others: string • Structured data type: array, string, record,.. • Variable, Constant • Reserve words, keywords • Error : Syntax error, Semantic error, Run time error • Debug : trace, break point, test case • Packet & installation tools
What’s Visual Basic (VB) • Microsoft Windows Programming language • Microsoft IDE for Visual studio developer • Visual Basic versions • Version history • Standard , Professional & Enterprise • What can programmers do with VB? • Windows GUI applications • Database applications • Computer Graphic applications • Internet / WEB applications • ………
Visual Basic programming steps • User interface design. • Forms design • Objects/Components properties define • Resource edit : icon, button image,.. • Code Writing form “even base” concept. • Compile/Build program • Debug source code • Packet application • Deploy application. • Maintenance
Microsoft Visual Studio IDE Integrated Development Environment Project Tools Box Form layout Properties Menu Bar and Tool Bar Simple Program develop
IDE Overview Menu Bar Tool Bar Project Window Properties Window
Project Windows Standard EXE application Title Bar Project View Tool Box Properties Form
Other IDE Components • Tool Box • Content all objects/components for FORM design. • Properties Windows • Control all properties of Form/Objects/Components • Display, font, format, enable,… • Project Windows • Project folder view • Form view • Code view
Simple Welcome Program • Program description • The program will show/hide the text “Welcome to Visual Basic 6.0” when button Show/Hide is clicked • The exit button used to exit the program. It is available after first click of Show button only. • How to do that with Visual Basic 6.0 • Design form with Label object name Label1 and caption “Welcome to Visual Basic 6.0” Set it visible properties to false. • Add button cmdShowHide & Button cmdExit to form. • Click on each button for writing code to it base on “Click” even • Click Play button on IDE to run program. • See sample below
Simple application FORM design form1 Label1 Visible = false Caption = “welcome to Visual Basic 6.0” cmdExit Enable = false cmdShowHide Caption =“&Show
Simple Application Run Time Click Show button: Exit enable, show Text, Show -> Hide First run: Only Show button available Click Hide button: Hide Text Click Exit button: Exit Program
Review & Home Work • Review from page 25 -> 47 • Drawing the Flowchart for Solving the equation ax2 +bx + c =0 • Redo the simple program • Using Visual Basic 6.0 IDE to build the form design from page 48->49 • Submit to instructor by email. • Pre-View chapters 3 and 4 for next lecture.
Introduction to Visual Basic programming Get familiar with Visual Basic Programming Get familiar with fundamental data types Using arithmetic operators Writing simple program in Visual Basic
Visual Programming & Even Programming • Visual tool of Visual basic for user interface design (GUI) • Tool Box components • From • Components • Component evens • Even procedure • Some basic components: Label Button (CmdButton) Text box Frame, Selected box, List box …….
Even drive programming • What’s Even? • Component evens • Even Procedure The sub procedure will be done when an even happened to component Each component has its own evens.
Writing first program with Visual Basic • Print Line of Text on form • Button Exit : exit program on click • Button print: On click, print a line of text “Hello, Welcome to Visual Basic” on form. • CODE • Private Sub Command1_Click() • Print " Hello! Welcome to Visual Basic" • End Sub • Private Sub Command2_Click() • End • End Sub
Other Application • Accumulate ADD Program allow user to enter an integer number and click add button. The numbers will be accumulate and display on form. Dim sum As Integer Private Sub cmdClear_Click() sum = 0 txtInput.Text = " " txtAdd.Text = 0 End Sub Private Sub cmdExit_Click() End End Sub Private Sub cmdAdd_Click() sum = sum + txtInput.Text txtAdd.text = sum txtInput.Text = "" End Sub Private Sub Form_Load() cmdClear_Click() End Sub
Syntax and statement • Data type • Variable declaration DIM variablename As datatype Ex: Dim Sum As Integer Dim Total As Integer • Keywords and reserved words • Statement Statement end with “end of line” • Assign statement Variable = Assigned value
Example of comparison operator Private Sub cmdClear_Click() txtInput1.Text = "" txtInput2.Text = "“ Label3.Caption = “Result” End Sub Private Sub cmdCompare_Click() Dim input1, input2 As Integer input1 = txtInput1.Text input2 = txtInput2.Text If input1 = input2 Then Label3.Caption = "First number equal second number" Else If input1 < input2 Then Label3.Caption = "First number less than second number" Else Label3.Caption = "First number greater than second number" End If End If End Sub • Write a windows program which allow user input 2 numbers A and B and then compare and show the result. Private Sub cmdExit_Click() End End Sub
Visual Basic Control structures Understanding control structures of VB Flowchart and control structures Using control structure by samples
Syntax if <condition> then statements end if Example If cmdShowHide.Caption = “&Show” then cmdShowHide.Caption = “&Hide” Label1.Visible = true end if If/Then structure Condition Yes Statements
Syntax if <condition> then statements else statements end if Example If cmdShowHide.Caption = “&Show” then cmdShowHide.Caption = “&Hide” Label1.Visible = true else cmdShowHide.Caption = “&Show” Label1.Visible = false end if If/Then/Else structure Condition No Yes Statements Statements
Syntax While <condition> statements Wend Example Dim product As integer product = 1 While product <= 1000 product = product * 2 print product Wend What’s on form 4 8 16 32 64 128 256 512 1024 While/Wend structure Condition Yes Statements No Beware of infinite loop
Syntax Do while <condition> statements Loop Example Dim product As integer product = 1 Do while product <= 1000 product = product * 2 print product Loop What’s on form 4 8 16 32 64 128 256 512 1024 Do while/Loop structure While/Wend || Do while/Loop
Syntax Do until <condition> statements Loop Example Dim product As integer product = 1 Do until product >1000 product = product * 2 print product Loop What’s on form 4 8 16 32 64 128 256 512 1024 Do until/Loop structure Condition Statements No Yes Beware of infinite loop
Syntax Forcounter=startValueToendValueStepStepsize statements Nextcounter Example For j = 2 to 80 step 5 a= a+2 Next j For/Next structure Counter = StartValue Counter = Counter + Stepsize statements Counter > endValue ** Stepsize = 1 is default value No Yes
Sample of For/Next For years = 1 To 10 Step 1 amount = principal * (1 + interestRate) ^ years lstDisplay.AddItem Format$(years, "@@@@") & vbTab & Format$(Format$(amount, "Currency"), _String$(17, "@")) Nextyears
Select Case structure • Syntax Select Case mAccessCode CaseIs < 1000 message = "Access Denied" Beep Case 1645 To 1689 message = "Technician Personnel" Case 8345 message = "Custodial Services" Case 55875 message = "Special Services" Case 999898, 1000006 To 1000008 message = "Scientific Personnel" Case Else message = "Access Denied" End Select
Do/loop while structure • Syntax counter = 1 Do Print counter & Space$(2); counter = counter + 1 Loop While counter <= 10 • Result 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
Do/Loop Until structure • Syntax counter = 1 Do Print counter & Space$(2); counter = counter + 1 Loop Until counter >= 10 • Result 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 Statements Do Loop While Condition Yes No Statements Do Loop Until Condition No Yes
For x=1 to 10 if x=5 then exit for end if print x next x Result: 1 2 3 4 5 counter = 1 Do Print counter & Space$(2); counter = counter + 1 if n = 5 then Exit Do Loop Until counter >= 10 Result: 1 2 3 4 5 Exit Do / Exit For Why do exit do and exit be used.
Variable & Constant – Logical operators • Dim VariableName As DataType • Const ConstantName = value As DataType • Logical operators • NOT • AND • OR • Where ‘s Logical operators used
LAB Works • All Samples from text book • Exercises at end of chapters • Assignment Calculator application • Your own applications (option)
SUB & Function procedure Procedure in VB Sub/Function Recursive
Module programming • What’s module? • Why do we need to write program in module? • Modules in Visual Basic • Even procedure • Visual basic procedure • SUB procedure • Function procedure • What should be used ? • Visual basic procedures • Sub/function procedures