Visual Basic Programming - Baylor ECS

10m ago
46 Views
5 Downloads
353.17 KB
48 Pages
Last View : 2d ago
Last Download : 19d ago
Upload by : Nora Drum
Share:
Transcription

Visual Basic ProgrammingAn Introduction

Why Visual Basic?Programming for the Windows UserInterface is extremely complicated.H Other Graphical User Interfaces (GUI) areno better.H Visual Basic provides a convenient methodfor building user interfaces.H Visual Basic can interface with code writtenin C, for efficiency.H

What Visual Basic is notVisual Basic is not, a powerfulprogramming language that enables you todo anything you want.H Visual Basic is not, elegant or fast.H Visual Basic is not, a replacement for C.H Visual Basic is not, anything like any otherprogramming language you have ever used.H

When You Program in VB:You draw pictures of your user interface.H You draw buttons, text boxes, and otheruser-interface items.H You add little snippets of code to handle theuser interaction.H You add initialization code, usually as thelast step.H If you like, you can code more complexfunctions. (But many do not.)H

The Visual Basic InterfaceDraw YourProgramHere!

Drawing The ProgramSelect A Control From Here(Click on the appropriate button)Then Draw the control on the form

Types of ControlsStatic TextGroup BoxCheck BoxScroll BarDrop-Down ListTimerFolder HierarchyCircles and StuffPicturesPicturesEditable TextButtonRadio ButtonListScroll BarDrive ListFile ListLinesData Base AccessAnd the List Goes On and On .

A Simple ProgramDouble-Click toAdd CodeSingle-Click toSelect andChangePropertiesUsing controls: Static TextEditable TextButtons

The Properties WindowList of PropertiesFor Currently SelectedControlClick on Property, andType In New Value, orSelect New Value FromMenu.

Adding CodeControlNameExternal EventNameYou must WriteThe BodyYourselfWhat to Do When It Happens

More Complex ControlsHComplex Controls Have:– Action Properties to Execute Commands– Active Properties that Cause Actions WhenValues Are Assigned to Them– Many Types of Events for Program InteractionHExamples:– Spreadsheets– Word Processors– Web Browsers

Using C CodeWrite a DLL in CH Use the export Property on AppropriateFunctionsH Write Visual Basic Definitions for eachFunctionH Add VB Definitions to The (general)section of the VB ProgramH Use Functions as if they were VB functionsH

C Definition vs. VB DefinitionC:long FAR PASCAL export HexToLong (char *Hex)VB:Declare Function HexToLong Lib “FIRSTONE.DLL”(ByVal InString As String) As LongFunction Name Must Be The Same in Both Declarations.The Lib keyword Must Give The Name of the Library.Argument Name in VB is arbitrary.

A (Very Annoying) ProblemIt is sometimes difficult for VB to FIND the.DLL file.H If this occurs, copy the .DLL file to theWINDOWS directory.H Remember to Delete the file when you aredone.H

Alternative MethodsSome Versions of VB do not allow DLLfunction definitions in the (general) sectionof a form.H To Get Around this Problem, Create a newModule (File Menu)H Add the declarations to the (general) sectionof the moduleH You can add your own VB functions to the(general) section of a form or a module.H

Syntax ConsiderationsAll Functions are Global in VBH Variables are declared using the syntax:H– Dim Name As Type – Every variable must have a type– Dim A,B,C As Type will work, but givesweird resultsHMost Common Types: Integer, String, Long

More VB SyntaxHUse Integers for Booleans– As in C, 0 False, everything else True– Symbolic constants True and False may be used– True -1, False 0Assignments are the same as in CH The Val function converts strings to integersH The Format function converts integers tostringsH

VB StatementsAssignments are the Same as in CH Case is not significantH– Case will be adjusted for you on keywords– For Variable Names, Case is ignoredHThe Usual Operators can be used– AND is the same as both & and && dependingon context– OR and – NOT !

VB IF StatementsIf condition Then List of Statements Else List of Statements EndIfIf condition Then List of Statements EndIfComparators: , , , , , (not equal)Connectives: And, Or, NotDON’T FORGET THE ENDIF!

VB While StatementsWhile condition do List of Statements WendThe VB Manual Recommends a different structure.Use the alternative if you wish.

VB For StatementsFor Variable start to finish List of Statements Next Variable For Variable start to finish Step increment List of Statements Next Variable Example:For I 1 to 10 doA[I] A[I] 1Next I

VB ArraysIndices Always Start With ZeroH Dim A[10] As Integer Declares 11 elements,indexed from 0 through 10.H Multi-Dimensional Arrays are Permitted.H Arrays can be resized at run time (See VBHelp File for ReDim)H

VB StringsVariable LengthH Compare using standard comparatorsH Maximum length is about 64KbH Minimum length is zeroH Allocated from VB “String Space”, so mayrun out of space even on systems with muchmemory.H

And in Conclusion .GoHaveFun!

Visual Basic ProgrammingAn Introduction

Why Visual Basic?Programming for the Windows UserInterface is extremely complicated.H Other Graphical User Interfaces (GUI) areno better.H Visual Basic provides a convenient methodfor building user interfaces.H Visual Basic can interface with code writtenin C, for efficiency.H

What Visual Basic is notVisual Basic is not, a powerfulprogramming language that enables you todo anything you want.H Visual Basic is not, elegant or fast.H Visual Basic is not, a replacement for C.H Visual Basic is not, anything like any otherprogramming language you have ever used.H

When You Program in VB:You draw pictures of your user interface.H You draw buttons, text boxes, and otheruser-interface items.H You add little snippets of code to handle theuser interaction.H You add initialization code, usually as thelast step.H If you like, you can code more complexfunctions. (But many do not.)H

The Visual Basic InterfaceDraw YourProgramHere!

Drawing The ProgramSelect A Control From Here(Click on the appropriate button)Then Draw the control on the form

Types of ControlsStatic TextGroup BoxCheck BoxScroll BarDrop-Down ListTimerFolder HierarchyCircles and StuffPicturesPicturesEditable TextButtonRadio ButtonListScroll BarDrive ListFile ListLinesData Base AccessAnd the List Goes On and On .

A Simple ProgramDouble-Click toAdd CodeSingle-Click toSelect andChangePropertiesUsing controls: Static TextEditable TextButtons

The Properties WindowList of PropertiesFor Currently SelectedControlClick on Property, andType In New Value, orSelect New Value FromMenu.

Adding CodeControlNameExternal EventNameYou must WriteThe BodyYourselfWhat to Do When It Happens

More Complex ControlsHComplex Controls Have:– Action Properties to Execute Commands– Active Properties that Cause Actions WhenValues Are Assigned to Them– Many Types of Events for Program InteractionHExamples:– Spreadsheets– Word Processors– Web Browsers

Using C CodeWrite a DLL in CH Use the export Property on AppropriateFunctionsH Write Visual Basic Definitions for eachFunctionH Add VB Definitions to The (general)section of the VB ProgramH Use Functions as if they were VB functionsH

C Definition vs. VB DefinitionC:long FAR PASCAL export HexToLong (char *Hex)VB:Declare Function HexToLong Lib “FIRSTONE.DLL”(ByVal InString As String) As LongFunction Name Must Be The Same in Both Declarations.The Lib keyword Must Give The Name of the Library.Argument Name in VB is arbitrary.

A (Very Annoying) ProblemIt is sometimes difficult for VB to FIND the.DLL file.H If this occurs, copy the .DLL file to theWINDOWS directory.H Remember to Delete the file when you aredone.H

Alternative MethodsSome Versions of VB do not allow DLLfunction definitions in the (general) sectionof a form.H To Get Around this Problem, Create a newModule (File Menu)H Add the declarations to the (general) sectionof the moduleH You can add your own VB functions to the(general) section of a form or a module.H

Syntax ConsiderationsAll Functions are Global in VBH Variables are declared using the syntax:H– Dim Name As Type – Every variable must have a type– Dim A,B,C As Type will work, but givesweird resultsHMost Common Types: Integer, String, Long

More VB SyntaxHUse Integers for Booleans– As in C, 0 False, everything else True– Symbolic constants True and False may be used– True -1, False 0Assignments are the same as in CH The Val function converts strings to integersH The Format function converts integers tostringsH

VB StatementsAssignments are the Same as in CH Case is not significantH– Case will be adjusted for you on keywords– For Variable Names, Case is ignoredHThe Usual Operators can be used– AND is the same as both & and && dependingon context– OR and – NOT !

VB IF StatementsIf condition Then List of Statements Else List of Statements EndIfIf condition Then List of Statements EndIfComparators: , , , , , (not equal)Connectives: And, Or, NotDON’T FORGET THE ENDIF!

VB While StatementsWhile condition do List of Statements WendThe VB Manual Recommends a different structure.Use the alternative if you wish.

VB For StatementsFor Variable start to finish List of Statements Next Variable For Variable start to finish Step increment List of Statements Next Variable Example:For I 1 to 10 doA[I] A[I] 1Next I

VB ArraysIndices Always Start With ZeroH Dim A[10] As Integer Declares 11 elements,indexed from 0 through 10.H Multi-Dimensional Arrays are Permitted.H Arrays can be resized at run time (See VBHelp File for ReDim)H

VB StringsVariable LengthH Compare using standard comparatorsH Maximum length is about 64KbH Minimum length is zeroH Allocated from VB “String Space”, so mayrun out of space even on systems with muchmemory.H

And in Conclusion .GoHaveFun!

What Visual Basic is not H Visual Basic is not, a powerful programming language that enables you to do anything you want. H Visual Basic is not, elegant or fast. H Visual Basic is not, a replacement for C. H Visual Basic is not, anything like any other programming language you have ever used.