680 likes | 919 Views
VB .NET. The Basics. The .NET Framework . Common Language Runtime Managed Code MS Intermediate Language – MSIL JIT Compiler Common Type System Common Language Specification Class Libraries – Namespace Multiple Language Support. Writing Windows Applications with VB.
E N D
VB .NET The Basics
The .NET Framework • Common Language Runtime • Managed Code • MS Intermediate Language – MSIL • JIT Compiler • Common Type System • Common Language Specification • Class Libraries – Namespace • Multiple Language Support
Writing Windows Applications with VB • Windows Graphical User Interface • VB.Net is an object-oriented language • Write application programs that run in Windows or on the Internet • Window = Form • Toolbox of elements called Controls • Text Box • Label • Check Box • Button
Programming Languages • Procedural • Program specifies exact sequence • Event Driven (VB 6.0 and previous) • Object Oriented Programming (VB.NET) • User controls sequence • Click event • Double Click event • Change event
Object Model • Object ==> Noun • Form and Controls • Property ==> Adjective • Color of a Form • Method ==> Verb • Move a Form
Object Model (cont.) • Event ==> Occurs when the user takes action • User clicks a button, User moves a form • Class ==> Template to create new object • Each control added is an Instance of a Class
Dot Notation • Used to reference object's properties and methods in code • Object dot Property • Form.Text, TextBox.Text • Object dot Method • Form.Hide( ), TextBox.Focus( ) • To reference an object's events use an underscore instead of a dot • Button_Click, ListBox_TextChanged
Intellisense label1.FlatStyle =label1.Enter() label1.Equals() Event Method Property
VB Application Files • One Solution File .sln • May contain multiple projects • Stores the names of the project and config info • Solution User Options File .suo • User customization options • IDE screen layout • Project Files .vbproj • Describes the project and list the files required • May contain multiple forms • Project User Options File .vbproj.user • Form Files .vb /.aspx • Resource File for the Form .resx • Code Behind .aspx.vb
Visual Studio Environment • Integrated Development Environment (IDE) • Form Designer • Editor for entering code • Compiler • Debugger • Object Browser
IDE Main Window • Toolbars • Document Window • Form Designer • Solution Explorer • Properties Window • The Name property of a control is used to refer to the control in code. • The Text property holds the words that the user sees on the screen. • Toolbox
VB Toolbox • Holds the controls you place on a form
Visual Studio Help • Extensive Help feature • Includes Microsoft Developer Network library (MSDN) • Entire reference manual • Coding examples • Dynamic Help • Technet • http://msdn.microsoft.com/library/en-us/vblr7/html/vboriVBLangRefTopNode.asp
Modes • Design Time • Designing the interface and writing code • Run Time • Testing and running project • Break Time • Runtime error or pause • Debugging “Look at the Title Bar”
Naming Rules – Handout • Always use standard names for objects • Numbers, letters, & underscore • Must start with letter or underscore • No spaces or punctuation marks • 3 letter lowercase prefix identifies control type • Button-btn • Label-lbl • Form-frm • If multiple words capitalize 1st letter of each word – Camel casing • btnExitProgram
Object Class Prefix Example Form frm frmDataEntry Button btn btnExit TextBox txt txtPaymentAmount Label lbl lblTotal Radio Button rad radBold CheckBox chk chkPrintSummary Horizontal ScrollBar hsb hsbRate Vertical ScrollBar vsb vsbTemperature PictureBox pic picLandscape ComboBox cbo cboBookList ListBox lst lstIndegredients Recommended Naming Conventions for VB Objects
Steps for Writing VB Projects • Design/Define the User Interface • Interface Design Planning Form • Plan/Set the Properties • Object and Properties Planning Form • Plan/Write the Code • Event Procedure Planning Form • One of each of the above for each *.vb form file • Test and Debug
Form Design Plan EXIT PUSH ME
Properties Design Plan • Label • Name lblMessage • Text leave blank • Button 1 • Name btnPush • Text Push Me • Button 2 • Name btnExit • Text Exit • Form • Name frmHello • Text Hello World by your name
btnPush_Click btnExit_Click Set lblMessage.text to “Hello World” Exit program Event Design Plan
Set the Project's Startup Object • The default startup object if Form1 • The name of the form should always be changed to adhere to naming rules • Using Project menu, Properties change the startup object to match the new name
Write the Code • While the project is running the user can perform actions • Each action by the user causes an Event to occur • Write code for the events you care about, the events you want to respond with code • Code is written as event procedures • VB will ignore events for which you do not write code
Editor Window • Declarations Section • Class list • Method list
Remark Statement • Also known as Comment, used for documentation • Non-executable • Automatically colored Green in Editor • Begins with an apostrophe ( ' ) • On a separate line from executable code • At the right end of a line of executable code ' Display the Hello World message
Test and Debug • Save Project - File Menu, Save All • Run Project • Debug Menu, Start (F5) • Start With Full without Debugging (CTRL F5) • Correct any Errors and Rerun • Compile errors • Run-Time Errors • Logic errors • Syntax errors "Help is always available from the Help Menu or by pressing F1."
Finding and Fixing Errors • Syntax Errors • Run-Time Errors • Logic Errors
Assigns a value to a property or variable Operates from right to left Enclose text strings in quotation marks (" ") Assignment Statement lblMessage.Text=" Hello World "
Ending a Program • Execute the Close Method of the Form • Methods always have parentheses (this will help you distinguish them from Properties which never have parentheses) • Current Form may be referenced as Me Me.Close( )
Button (btn) • Used to run/activate an Event Procedure • Click event Label (lbl) • Used for • Output on a form • Identification of objects • Directions/Information • Cannot by modified by user
Text Box (txt) • Used for user input/data entry • Text Property • What is displayed in text box • What user entered in text box • TextAlign Property • Controls alignment of text in the Text Box • Change Event
Check Box (chk) • Used for user input/data entry • Allows the user to select or deselect 1 or more in any group • Checked Property - Boolean • Checked = True • Unchecked = False • CheckChanged Event
Radio Button (rad) • Used for user input/data entry • Allows the user to select only 1 in any group • First create a group and then create each radio button inside the group • Checked Property - Boolean • Checked = True • Unchecked = False • CheckChanged Event
Variables & Constants • Variable • Memory locations that hold data that can be changed during project execution • Ex: hours worked • Named Constant • Memory locations that hold data that cannot be changed during project execution • Ex: Sales tax percentage, SSI rate
Constants • Named • User defined • Intrinsic • System defined within Visual Studio • Color.red
Declaration Statements • DIM|PUBLIC|PRIVATE|FRIEND|STATICused to declare Variables • CONST used to declare Named Constants • Declaration includes • Name, follow Naming Convention Rules • Data Type • Required Value for Constants • Optional Initial Value for Variables
Declaration Examples Dim strName, strSSN As String Dim intAge As Short Dim decPayRate As Decimal = 8.25 Dim datHireDate As Date Dim blnInsured As Boolean Dim lngPopulation As Long Const decDISCOUNT_RATE As Decimal = .15 Note: Constants are named using all uppercase letters EXCEPT the prefix.
Type-Declaration Characters • Append single character to the end of the Constant's Value to indicate the Data Type Short – S Integer – I Long – L Decimal – D Single – F Double – R
Variables – Scope & Lifetime • Global/Public (use sparingly and cautiously) • Available to all modules and procedures of Project • Initialized at start of Project • Module/Private (Form) • Available to one module and all procedures within that module • Initialized 1st time the Form is loaded • Local • Available only to the procedure it is declared in • Initialized every time the Procedure runs • Block (not used until later in this course) • Available only to the block of code inside a procedure it is declared in • Initialized every time the Procedure runs
Calculations • Calculations can be performed using properties of certain objects, variables, constants, and numeric literals • Do Not use Strings in calculations • Values from Text property of Text Boxes • Are Strings, even if they contain numeric data • Must be converted to a Numeric Data Type
Conversion Functions (cont.) • Function Convert To • CInt ** Integer • CDec Decimal • CStr String ** CInt rounds to the nearest Even Number
Conversion Examples(also review info p 485) intQuantity = CInt(txtQuantity.Text) decPrice = CDec(txtPrice.Text) intWholeNumber = CInt(decFractionalValue) decDollars = CDec(intDollars) strValue = CStr(decValue) Function Name Argument To Be Acted Upon