MSU LBS 126 - Visual Basic Review
Course Lbs 126-
Pages 24

Unformatted text preview:

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 ObjectsDefinitionsForm contains the Main ProgramControls are user interface elements (text boxes, commands) in the FormForms and Controls are referred to as OBJECTSObjects in VBEverything in VB is objects, forms, buttons,…Three elements of objects.PropertiesMethodsEvent 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 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 SubVariables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 TypeDeclaring 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 SingleScope 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 IntegerA=0A=A+1Relational 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 functionsConcatenationString relations =, <>, <, >, <=,>=String functinsLen(), Val(), Str(), Ucase()…Read text pp. 51Sub 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 codeEnd SubNot linked with any eventCan be called by other part of code. Call ProcedureName(arguments list)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 functionPrivate Function FunctionName(arg1 As type1, arg2 As type2,…) As ReturnTypeBlock of codeFunctionName=expressionEnd FunctionPassing 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 falseBoolean 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_variableDecision structureSimple If statementIF Boolean_Condition ThenEnd IFIf-then-else blocksNested decision blocks.If –ElseIf- Elseif… – Else blocksSelect Case BlocksAllows multiple options (not just true or false)Select Case Variable Case Condition1 Case Condition2 Case Condition3 Case ElseEnd SelectRead text pp. 114Repetition structionDo while LoopWhen times of loop is unknowDo while conditionLoopFor Next loopFixed number of loopsFor variable = start# to end#Next varialbeThree 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 #1VB 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 typeEnd


View Full Document

MSU LBS 126 - Visual Basic Review

Course: Lbs 126-
Pages: 24
Download Visual Basic Review
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Visual Basic Review and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Visual Basic Review 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?