1 / 24

Visual Basic Review

Visual Basic Review. LBS 126. VB programming. Project. Form 1. Form 2. Form 3. Button. Text box. Button. Text box. Picture box. Objects. Objects. Definitions. Form contains the Main Program Controls are user interface elements (text boxes, commands) in the Form

jdenham
Télécharger la présentation

Visual Basic Review

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 Review LBS 126

  2. VB programming Project Form 1 Form 2 Form 3 Button Text box Button Text box Picture box Objects Objects

  3. Definitions • Form contains the Main Program • Controls are user interface elements (text boxes, commands) in the Form • Forms and Controls are referred to as OBJECTS

  4. Objects in VB • Everything in VB is objects, forms, buttons,… • Three elements of objects. • Properties • Methods • Event handlers. • Important properties of common objects. VB object Properties (size, Caption…) Methods (Print…) Event Handler (Command1_Click())

  5. Handling the event Get Text1.text Properties Text box Methods Message Dispatching Block Event Handler User Properties Button Methods Event Handler Command1_Click Print Properties Picture Box You have got clicked. Methods Event Handler

  6. Define event handler • When you double click the command button, you only define the default event handler (command1_click). • Other event can be defined as well. • Then procedure name of the event handler. • Private Sub ObjectName_EventName() • End Sub

  7. Variables • Different Types: Byte, Integer, Long, Single, String • Naming • Starting with letter • No special symbols, like ^ $ % • Less than 255 characters. • Key words are reserved. • Declare Variable • Dim VariableName As Variable Type

  8. Declaring Arrays • Use Dim key word to declare arrays, just as what we do for variables. • Dim Data(1 to 50) As Single • Dim Species(1 to 4) As String • Accessing values in an array with subscript. • Data(44) = 22.5; • Species(1) = “ACTGACTCGTAACGT” • Red Number is INDEX • Use ReDim when declaring an array size based on a variable • User tells you there are 50 values (count = 50) • ReDim Values(1 to count) as Single

  9. Scope of a variable • Local scope. • Variable declared in a sub procedure • It only exists in this sub procedure. • Form-level scope. • Variable declared out of any sub procedure. • Any sub procedure can read it.

  10. Two meanings of “=“ sign • Assignment operator: assign the value of the RIGHT side to the LEFT side. Dim A as Integer A=0 A=A+1 • Relational operator If A = B Then Reads “A is assigned with A+1” Reads “If A equals to B then”

  11. Strings • String data is defined between two double quotations. “Hello world” • String variable is a name used to refer to a string. String1=“Hello world” • String is just a bunch of ASCII codes. • You can not do mathematical operations on string.

  12. String operations and functions • Concatenation • String relations =, <>, <, >, <=,>= • String functins • Len(), Val(), Str(), Ucase()… • Read text pp. 51

  13. Sub procedures and Functions • Procedures and functions are pre-defined code fragments. • Why using procedures and functions • Making code better structured. • Reusing code. • Three types of procedures. • Event procedure (event handler) • General procedure • Function procedure • Different between Sub procedure and Functions.

  14. General Procedures • Defined by key word Sub Private Sub ProcedureName() Block of code End Sub • Not linked with any event • Can be called by other part of code. • Call ProcedureName(argumentslist)

  15. Functions procedure • Function is a sub procedure with a returned value. • Function procedure is used in the same way as a built-in numeric or string function. Variable = FunctionName(arguments…) • Define a function Private Function FunctionName(arg1 As type1, arg2 As type2,…) As ReturnType Block of code FunctionName=expression End Function

  16. Passing arguments to and from procedure • Passing by reference • Sum(num1, num2) • Create a tunnel that a procedure can output the computing results. • Passing by value • Sum((num1),num2) • Isolate the procedure from it external world.

  17. Comparison Operators • See table 5.1 • = equal to • <> unequal to • < less than • > greater than • <= less than or equal to • >= greater than or equal to • Works both for numbers and strings.

  18. Logical Operators • And, Or, Not • a >=b And b <> 2 • a >=b Or b <> 2 • And statements: Both must be true • Or statements: one or both must be true • Not statements: True if statement is false

  19. Boolean value • Comparison operator and logical operator will return a Boolean value. • Dim VariableName as Boolean • Boolean value has only two possibilities: True and False • Boolean value can be directly used in decision structure, do while loop, etc. • If boolean_variable Then… • Do While boolean_variable

  20. Decision structure • Simple If statement • IF Boolean_Condition Then • End IF • If-then-elseblocks • Nested decision blocks. • If –ElseIf- Elseif… – Else blocks

  21. Select Case Blocks • Allows multiple options (not just true or false) Select Case Variable Case Condition1 Case Condition2 Case Condition3 Case Else End Select • Read text pp. 114

  22. Repetition struction • Do while Loop • When times of loop is unknow Do while condition Loop • For Next loop • Fixed number of loops For variable = start# to end# Next varialbe

  23. Three steps of reading data from file • Step 1:Open the plain text file • Open “filename.txt” For Input As #1 • Step 2: Read data • Input #1, variable • Step 3: Close file • Close #1

  24. VB and Excel • How to exchange data between VB and Excel. • .csv file • How to write a user defined function in Excel. Private Function FuncName(arg1 as type1) as type End Function

More Related