vb.net introduction. visual studio 2008 it supports vb.net, j#, c#, and c++. demo: –start page:...
TRANSCRIPT
![Page 1: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/1.jpg)
VB.Net Introduction
![Page 2: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/2.jpg)
Visual Studio 2008• It supports VB.Net, J#, C#, and C++.• Demo:
– Start page: Recent projects– Starting project:
• File/New Project/Project types: Windows Application– View:
• Solution Explorer/Data Sources• Server Explorer• Property Window• ToolBox
– Form design view/Form code view– Tools/Option
– Environment– Projects and Solutions
» VB defaults
– Project/Add New Item– Project properties: Right-click project name and choose Properties
• Start Up form– Property window example
![Page 3: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/3.jpg)
Introduction to Visual Basic .Net
• Event-driven programming– The interface for a VB program consists of one
or more forms, containing one or more controls (screen objects).
– Form and controls have events that can respond to. Typical events include clicking a mouse button, type a character on the keyboard, changing a value, etc.
– Event procedure
![Page 4: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/4.jpg)
Form
• Properties:– Name, FormBorderStyle, Text, BackColor,
BackImage, Opacity
• Events:– Load, FormClosing, FormClosed– GotFocus, LostFocus– MouseHover, Click, DoubleCLick
![Page 5: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/5.jpg)
Typical VB.Net Controls
• TextBox• Label• Button• CheckBox• RadioButton• ListBox• ComboBox• PictureBox
![Page 6: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/6.jpg)
Text Box• Properties:
– AutoSize, BorderStyle, CauseValidation, Enabled, Locked, Multiline, PasswordChar, ReadOnly, ScrollBar, TabIndex, Text, Visible, WordWrap, etc.
• Properties can be set at the design time or at the run time using code.
• To refer to a property: – ControlName.PropertyName– Ex. TextBox1.Text– Note: The Text property is a string data type and
automatically inherits the properties and methods of the string data type.
![Page 7: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/7.jpg)
Typical VB.Net Programming Tasks
• Creating the GUI elements that make up the application’s user interface.– Visualize the application.– Make a list of the controls needed.
• Setting the properties of the GUI elements
• Writing procedures that respond to events and perform other operations.
![Page 8: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/8.jpg)
Demo
Num1
Num2
Sum =
.Control properties
.Event: Click, MouseMove, Form Load, etc.
.Event proceduresSum: textBox3.text=CStr(CDbl(textBox1.text)+CDbl(textBox2.text))Or (CDbl(textBox1.text)+CDbl(textBox2.text)).toString
.Demo: Text alignment (TextAlign property)
![Page 9: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/9.jpg)
Configure VB Project
• Project property page– Application– Compile– References
• Tools/Options– Environment
– Projects and Solutions
» VB defaults
![Page 10: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/10.jpg)
VB Defaults
• Option Explicit: – On --- must declare variables before use
• Option Strict:– Off --- VB will convert the data
• Option Compare:– Binary --- case sensitive– Text --- case insensitive
• Option Infer– On --- When you set Option Infer to On, you can declare variables
without explicitly stating a data type. The compiler infers the data type of a variable from the type of its initialization expression.
![Page 11: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/11.jpg)
Variable Declarations• Option Explicit• Dim variableName as DataType• Variable naming rules:
– The first character must be a letter or an underscore character.
– Use only letters, digits, and underscore.– Cannot contain spaces or periods.– No VB keywords
• Naming conventions:– Descriptive– Consistent lower and upper case characters.
• Ex. Camel casing: lowerUpper, employeeName
![Page 12: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/12.jpg)
Control Naming Conventions
• The first three letters should be a lowercase prefix that indicates the control’s type.– frm, txt, lbl, btn.
• The first letter after the prefix should be uppercase.– txtSalary, lblMessage
• The part of the control name after the prefix should describe the control’s purpose in the application.
![Page 13: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/13.jpg)
VB Data Types
• Boolean (True/False): 2 bytes• Byte: Holds a whole number from 0 to 255.• Char: single character• Date: date and time, 8 bytes.• Decimal: Real number up to 29 significant digits, 16 bytes• Double: real, 8 bytes• Single: real, 4 bytes• Integer: 4 bytes (int32, uint32)• Long: 8 bytes integer• Short: 2 bytes integer• String• Object: Holds a reference of an object
![Page 14: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/14.jpg)
Variable Declaration Examples
• Dim empName as String
• Declare multiple variables with one Dim:– Dim empName, dependentName, empSSN as String
• Dim X As Integer, Y As Single
• Initiatialization– Dim interestRate as Double = 0.0715
![Page 15: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/15.jpg)
Variable Scope• Block-level scope: declared within a block of code
terminated by an end, loop or next statement.– If city = “Rome” then
• Dim message as string = “the city is in Italy”
• MessageBox.Show(message)
– End if
• Procedural-level scope: declared in a procedure• Class-level, module-level scope: declared in a
class or module but outside any procedure with either Dim or Private keyword.
• Project-level scope: a module variable declared with the Public keyword.
![Page 16: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/16.jpg)
Data Conversion• Implicit conversion: When you assign a value of
one data type to a variable of another data type, VB attempts to convert the value being assigned to the data type of the variable if the OptionStrict is set to Off.
• Explicit conversion:– VB.Net Functions: CStr, Ccur, CDbl, Cint, CLng,
CSng, Cdate,Val, etc.– .Net System.Convert
• Type class’s methods:– toString
![Page 17: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/17.jpg)
Date Data Type
• Variables of the Date data type can hold both a date and a time. The smallest value is midnight (00:00:00) of Jan 1 of the year 1. The largest value is 11:59:59 PM of Dec. 31 of the year 9999.
• Date literals: A date literal may contain the date, the time, or both, and must be enclosed in # symbols:– #1/30/2003#, #1/31/2003 2:10:00 PM#– #6:30 PM#, #18:30:00#
• Note: ControlPanel/RegionalOptions/Date
![Page 18: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/18.jpg)
• Date Literal Example:– Dim startDate as dateTime– startDate = #1/30/2003#
• Use the System.Convert.ToDateTime function to convert a string to a date value:– startDate = System.Convert.ToDateTime(“1/30/2003”)– If date string is entered in a text box:
• startDate = System.Convert.ToDateTime(txtDate.text)• Or startDate=Cdate(txtDate.text)
• Date data type format methods
![Page 19: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/19.jpg)
Some Date Functions
• Now: Current date and time• Today: Current date• TimeOfDay• DateDiff:• Demo
– Days between two dates– Days to Christmas
• DateDiff(DateInterval.Day, Today(), #12/25/2009#)
– Date data type properties and methods
![Page 20: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/20.jpg)
Using Online HelpExample:Search Help for DateDiff
• MSDN VB Developer Center– http://msdn.microsoft.com/en-us/vbasic/
default.aspx– Library/Visual Studio/Visual Basic
• Reference – Language reference
» Functions:
» DateDiff Function
» Statements
![Page 21: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/21.jpg)
Arithmetic and String Operators
• +, -, *, /. \, ^
• String Concatenation: &, +
• Compound operator:: X= X+1 or X +=1
![Page 22: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/22.jpg)
The If … Then Statement• If condition Then
Statements
• End If• If condition Then
StatementsElse
Statements
• End If• Condition:
– Simple condition:• Comparison of two expressions formed with relational operators:>, <,
=, < >, >=, <=• Boolean variable
– Complex condition:• Formed with logical operators: ( ), Not, And, Or
![Page 23: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/23.jpg)
Complex Condition
• If 12<=Age<=65 Then Fee = 20, else Fee = 5• Admission rules: Applicants will be admitted if
meet one of the following conditions:– GPA>3.0– GPA>2.5 AND SAT >700
• Scholarship rules: Meet all the conditions:– GPA>3.2– Must be Accounting or CIS
• Admission rules: Applicants will be admitted if meet all the following conditions:– SAT>700 Or Income > 40000– Not GPA < 2.5
![Page 24: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/24.jpg)
IF Statement
• IF condition THEN
statements
[ELSEIF condition-n THEN
[elseifstatements]
[ELSE
[elsestatements]]]
End If
![Page 25: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/25.jpg)
Using Boolean Variables as Flags
• A flag is a Boolean variable that signals when some condition exists in the program.
Dim goodStudent as Boolean=TrueIf daysAbsent > 10 Then
goodStudent = False
End ifIf goodStudent Then
TextBox1.text=“Good Student”
ElsetextBox1.text=“Not good student”
End If
![Page 26: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/26.jpg)
Select Case Structure
• SELECT CASE testexpression
[CASE expressionlist-n
[Statements]
[CASE ELSE
[elsestatements]
END SELECT
![Page 27: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/27.jpg)
Select Case Example• SELECT CASE temperature
CASE <40Text1.text=“cold”
CASE < 60Text1.text=“cool”
CASE 60 to 80Text1.text=“warm”
CASE ELSEText1.text=“Hot”
End Select
![Page 28: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/28.jpg)
The Expression list can contain multiple expressions, separated by commas.
Select Case number
Case 1, 3, 5, 7, 9
textBox1.text=“Odd number”
Case 2, 4, 6, 8, 10
textBox1.text=“Even number”
Case Else
End Select
![Page 29: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/29.jpg)
Loop
• FOR index – start TO end [STEP step]
[statements]
[EXIT FOR]
NEXT index
DO [{WHILE| UNTIL} condition]
[statements]
[EXIT DO]
LOOP
![Page 30: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/30.jpg)
Example: Display characters entered in textbox1one at a time
• Dim index As Integer• index = 0• For index = 0 To TextBox1.Text.Length - 1
MessageBox.Show(TextBox1.Text.Substring(index, 1))
• Next
• Problem : How many “a” entered in textbox1?
![Page 31: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/31.jpg)
Do While/Do UntilPrivate Sub Command1_Click()Dim counter As Integercounter = 0Do While counter <= 5 Debug.write(counter) counter = counter + 1LoopText1.Text = counterEnd Sub
Private Sub Command2_Click()Dim counter As Integercounter = 0Do Until counter > 5 Debug.write(counter) counter = counter + 1LoopText1.Text = counterEnd Sub
![Page 32: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/32.jpg)
With … End With
With TextBox1
.Height = 250
.Width = 600
.Text = “Hello”
End With
Convenient shorthand to execute a series of statements on a single object. Within the block, the reference to the object is implicit and need not be written.
![Page 33: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/33.jpg)
Procedures
. Sub procedure:
Sub SubName(Arguments)
…
End Sub– To call a sub procedure SUB1
• CALL SUB1(Argument1, Argument2, …)
![Page 34: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/34.jpg)
Function
• Private Function tax(salary) As Double
• tax = salary * 0.1
• End Function
– Or• Private Function tax(salary)
• Return salary * 0.1
• End Function
![Page 35: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/35.jpg)
Call by Reference Call by Value
• ByRef– The address of the item is passed. Any changes
made to the passing variable are made to the variable itself.
• ByVal– Default– Only the variable’s value is passed.
![Page 36: VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project](https://reader036.vdocuments.net/reader036/viewer/2022081501/5697c0281a28abf838cd6f3f/html5/thumbnails/36.jpg)
ByRef, ByVal example
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myStr As String
myStr = TextBox1.Text
Call ChangeTextRef (myStr)
TextBox1.Text = myStr
End Sub
Private Sub ChangeTextRef(ByRef strInput As String)
strInput = "New Text"
End Sub