Visual Basic ReviewVB programmingDefinitionsObjects in VBHandling the eventDefine event handlerVariablesDeclaring ArraysScope of a variableTwo meanings of “=“ signStringsString operations and functionsSub procedures and FunctionsGeneral ProceduresFunctions procedurePassing arguments to and from procedureComparison OperatorsLogical OperatorsBoolean valueDecision structureSelect Case BlocksRepetition structionThree steps of reading data from fileVB and ExcelVisual Basic ReviewLBS 126VB programmingProjectForm 1 Form 2 Form 3Text box Button Picture box ObjectsText box Button ObjectsDefinitionsForm contains the Main ProgramControls are user interface elements (text boxes, commands) in the FormForms and Controls are referred to as OBJECTSObjects in VBEverything in VB is objects, forms, buttons,…Three elements of objects.PropertiesMethodsEvent handlers.Important properties of common objects.Properties (size,Caption…)Methods (Print…)Event Handler (Command1_Click())VB objectHandling the eventMessageDispatchingBlock UserText boxPropertiesMethodsEvent HandlerButtonPropertiesMethodsEvent HandlerCommand1_ClickPictureBoxPropertiesMethodsEvent HandlerPrintGet Text1.textYou have got clicked.Define event handlerWhen 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 SubVariablesDifferent Types: Byte, Integer, Long, Single, StringNamingStarting with letterNo special symbols, like ^ $ %Less than 255 characters.Key words are reserved.Declare VariableDim VariableName As Variable TypeDeclaring ArraysUse Dim key word to declare arrays, just as what we do for variables.Dim Data(1 to 50) As SingleDim Species(1 to 4) As StringAccessing values in an array with subscript.Data(44) = 22.5;Species(1) = “ACTGACTCGTAACGT”Red Number is INDEXUse ReDim when declaring an array size based on a variableUser tells you there are 50 values (count = 50)ReDim Values(1 to count) as SingleScope of a variableLocal 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 “=“ signAssignment operator: assign the value of the RIGHT side to the LEFT side.Dim A as IntegerA=0A=A+1Relational operatorIf 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 functionsConcatenationString relations =, <>, <, >, <=,>=String functinsLen(), Val(), Str(), Ucase()…Read text pp. 51Sub procedures and FunctionsProcedures and functions are pre-defined code fragments.Why using procedures and functionsMaking code better structured.Reusing code.Three types of procedures.Event procedure (event handler)General procedureFunction procedureDifferent between Sub procedure and Functions.General ProceduresDefined by key word Sub Private Sub ProcedureName()Block of codeEnd SubNot linked with any eventCan be called by other part of code. Call ProcedureName(arguments list)Functions procedureFunction 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 functionPrivate Function FunctionName(arg1 As type1, arg2 As type2,…) As ReturnTypeBlock of codeFunctionName=expressionEnd FunctionPassing arguments to and from procedurePassing by reference Sum(num1, num2)Create a tunnel that a procedure can output the computing results.Passing by valueSum((num1),num2)Isolate the procedure from it external world.Comparison OperatorsSee table 5.1= equal to<> unequal to< less than> greater than<= less than or equal to>= greater than or equal toWorks both for numbers and strings.Logical OperatorsAnd, Or, Nota >=b And b <> 2a >=b Or b <> 2And statements: Both must be trueOr statements: one or both must be trueNot statements: True if statement is falseBoolean valueComparison operator and logical operator will return a Boolean value.Dim VariableName as BooleanBoolean value has only two possibilities: True and FalseBoolean value can be directly used in decision structure, do while loop, etc.If boolean_variable Then…Do While boolean_variableDecision structureSimple If statementIF Boolean_Condition ThenEnd IFIf-then-else blocksNested decision blocks.If –ElseIf- Elseif… – Else blocksSelect Case BlocksAllows multiple options (not just true or false)Select Case Variable Case Condition1 Case Condition2 Case Condition3 Case ElseEnd SelectRead text pp. 114Repetition structionDo while LoopWhen times of loop is unknowDo while conditionLoopFor Next loopFixed number of loopsFor variable = start# to end#Next varialbeThree steps of reading data from fileStep 1:Open the plain text fileOpen “filename.txt” For Input As #1Step 2: Read dataInput #1, variableStep 3: Close fileClose #1VB and ExcelHow to exchange data between VB and Excel..csv fileHow to write a user defined function in Excel.Private Function FuncName(arg1 as type1) as typeEnd
View Full Document