240 likes | 260 Views
This review covers the basics of Visual Basic programming, including object definitions, event handling, and the use of forms and controls. Learn about properties, methods, variables, arrays, and more.
E N D
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 • Forms and Controls are referred to as OBJECTS
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())
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
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
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
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
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.
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”
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.
String operations and functions • Concatenation • String relations =, <>, <, >, <=,>= • String functins • Len(), Val(), Str(), Ucase()… • Read text pp. 51
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.
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)
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
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.
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.
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
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
Decision structure • Simple If statement • IF Boolean_Condition Then • End IF • If-then-elseblocks • Nested decision blocks. • If –ElseIf- Elseif… – Else blocks
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
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
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
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