vb.net lecture

133
Visual Basic .net

Upload: bryant-salcedo

Post on 07-Apr-2015

422 views

Category:

Documents


13 download

TRANSCRIPT

Page 1: Vb.net Lecture

Visual Basic .net

Page 2: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Visual Studio .netMicrosoft’s Integrated Development

Environment (IDE) for creating running and debugging programs (called applications) written in variety of programming languages

Page 3: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Why move from VB6 to .Net?World of applications is changingHas a number of unique features

Page 4: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Overview of Visual Studio .net Start Page – contains list of helpful links There are two buttons on the page:

Open Project New Project

To create a new project click new project

Page 5: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Create New Project

Open Existing Project

Page 6: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Up close…Variety of languages

Visual Basic ProjectsVisual C# ProjectsVisual C++ ProjectsSetup and Deployment ProjectsVisual Studio Solutions

Page 7: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Visual Basic ProjectsWindows application – a program that

executes inside a windows OS. Include customized software that

programmers create, example Microsoft Word, IE6 ect.

Page 8: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Project Name

Create Applications

Page 9: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Project Name

Path

Page 10: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Console ProgrammingConsole application – applications contain

only text outputVisual Basic console applications consist of

pieces called module

Page 11: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Changing the default ModuleClick on Project > Console PropertiesOn the startup object change the startup

object to your startup module

Page 12: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

To run a programOn the menu bar press DebugClick start without debugging (F5 for

shortcut key)

Page 13: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Console ProgrammingPrinting a text

Consolse.Write(“String”)Console.WriteLine(“The text goes here!”)

Writeline position the text in the beginning of the next line

Page 14: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Example 1VB.net\LaboratoryExercise\PrintingText

Page 15: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

VariablesStores data or information during program

executionHow to declare

Dim variable_Name as Data_TypeTo insert a value:

Dim variable_Name as Data_Type = Value

Page 16: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

NoteBefore VB can use an object variable, it

must determine its type and perform the necessary conversions, if any.

Page 17: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Data TypesTypes of Variables

Numeric – Store Numbers

Page 18: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Page 19: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Arithmetic OperatorsAddition - +Subtraction - -Multiplication - *Division - /Division (Integer) \Modulus - ModExponentiation - ^

Page 20: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Example 2\VB.net\LaboratoryExercise\

ConsoleCalculator\ConsoleCalC

Page 21: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Character VariablesYou can initialize a character variable by

assigning either a character or a string to it. In the latter case, only the first character of the string is assigned to the variable.Dim string_varialble as stringDim string_varialble as string = “Value”

Page 22: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

ConcatenationJoining of two or more stringsExampleDim string1 as string = “a”Dim string 2 as string = “b”

String1 & string2ab

Page 23: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Example 3VB.net\LaboratoryExercise\

StringManipulation\StringManipulation

Page 24: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Equality Operators= - Equals<> - Not Equal> - Greater Than< - Less Than>= - Greater Than Equal<= - Less than equal

Page 25: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

If thenIf studentGrade>=60 then

Console.WriteLine(“Passed”)End If

Page 26: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Decision MakingIf/then

Allows a program to make a decision based on the truth or falsity of some expression

It is a condition

Page 27: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Module Condition

Sub Main() Dim lagyu As String Dim num1 As Integer Dim num2 As Integer Console.WriteLine("Enter first number: ") num1 = Console.ReadLine() Console.WriteLine("Enter second number: ") num2 = Console.ReadLine() If num1 > num2 Then Console.WriteLine("Number 1 is greater than number 2") Else Console.WriteLine("Number 2 is greater than number 3") End If

End Sub

End Module

Page 28: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

If else/ elseif / else If num1 > num2 Then

Console.WriteLine("Number 1 is greater than number 2!")

ElseIf (num2 > num1) Then Console.WriteLine("Number 1 is greater than

number 2!") Else Console.WriteLine("Number are equal!") End If

Page 29: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Example 4VB.net\LaboratoryExercise\IFCondition\

ifCondition

Page 30: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

While Repetition StructureA repetition structure allows the

programmer to specify that an action should be repeated depending on the value of the condition

Page 31: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Do while/LoopModule WhileLoop Dim ctr As Integer Sub Main() ctr = 1 While ctr <= 10 ctr = ctr + 1 Console.WriteLine(ctr) End While End Sub

End Module

Page 32: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Do Until/Loop RepetitionTest a condition for falsity for repetition to

continue. Statements in the body of a are executed repeatedly as long as the loop-continuation test evaluates to false

Page 33: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Module DoWhileLoop Dim ctr As Integer Sub Main() ctr = 10 Do Until ctr < 1 Console.WriteLine(ctr) ctr = ctr - 1 Loop

End Sub

End Module

Page 34: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Assignment Operators+= C=+7 C = c +7

-= C-=7 C = c - 7

*= C*=7 C = c * 7

/= c/=7 C = c /7

\= c\=7 C = c \ 7

^= C^=7 C = c ^ 7

&= D &= “llo” D= D & “llo”

Page 35: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Module Module1

Sub Main() Dim a As Integer Dim var1 As Integer Dim sum As Integer a = 1 sum = 0 While a <= 10 Console.WriteLine("Enter Number " & a & ":") var1 = Console.ReadLine() a += 1 sum = var1 + sum End While Console.WriteLine("The sum is " & sum) End Sub

End Module

Page 36: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

For/Next RepetitionRepetition structure handles the details of

counter-controlled repetition.

Page 37: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Module ForLoop

Sub Main() Dim counter As Integer

For counter = 1 To 10 Console.WriteLine(counter) Next End Sub

End Module

Page 38: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Using Dialog to Display MessageOn the solution explorer, right click on the

reference and add System.windows.forms

Page 39: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Message boxMessageBox.Show("Text", "Title Bar

caption", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)

Page 40: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Work SpaceForm – Gray rectangle which represents the

Windows Applications that the programmer is creating

The form which is the visual part of the program which the user interact

Page 41: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Introduction to Windows Application ProgramMessagebox

MesageboxIcon.Information

MessageBox.OK

Title Bar Text

Message Text

Page 42: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

MessageBoxIcon.Exclamation – Excalmation Point

MessageBoxIcon.Information – Icon Containing the letter I

MessageBoxIcon.Question – Icon Containing question mark

MessageBoxIcon.Error – Icon containing the red x circle

Page 43: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

MessageBoxButtons.OK – OK buttonMessageBoxButtons.OKCancel – OK and Cancel

buttonsMessageBoxButtons.YesNo – Yes and No ButtonsMessageBoxButtons.YesNoCancel – Yes, no and

cancel buttonsMessageBoxButtons.RetryCancel – Retry and

Cancel Button

Page 44: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Select Case Multiple-Selection StructureSelect Case variableCase <decision 1>

CodeCase <decision 2>

CodeCase Else

CodeEnd Select

Page 45: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Module SelectCaseModule Sub Main() Dim choys As Integer Console.WriteLine("Enter Choice") choys = Console.ReadLine

Select Case choys Case 1 Console.WriteLine(1) Case 2 Console.WriteLine(2) Case Else Console.WriteLine("Default") End Select End Sub

End Module

Page 46: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Exit Keyword in a RepetitionExit Do, Exit While, Exit For statements

alter the flow of control by causing immediate exit from a repetition structure

Page 47: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Exit Do – Exit a do while repetitionExit for – Exit a for loopExit While – Exit a while repetition

Page 48: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Logical OperatorsVisual Basic provides logical operators that

can be used to form complex conditions by combining simple ones

Page 49: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

AndAlsoAndOrElseOrXorNot

Page 50: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

AndAlsoFalse + False = FalseFalse + True = FalseTrue + False = FalseTrue + True = True

Page 51: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

OrElseFalse + False = FalseFalse + True = TrueTrue + False = TrueTrue + True = True

Page 52: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

NotTrue = FalseTrue = False

Page 53: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

XorFalse + False = FalseFalse + True = TrueTrue + False = TrueTrue + True = False

Page 54: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Sub ProceduresModule SubProc Sub Main() Dim choys As Integer Console.WriteLine("Enter Choice") choys = Console.ReadLine

Select Case choys Case 1 proc1("jpacs")

End Select

End Sub

Sub proc1(ByVal name As String) Console.WriteLine(name) End SubEnd Module

Page 55: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

ProcedureSimilar to sub procedure except that

function procedures return a valueFunction procedure-name(parameter list) as

return typeDeclarations and Statement

End Function

Page 56: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

MaximumGet the maximum value of two number

Math.Max(val1, val2)

Page 57: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Public Class frmMax Inherits System.Windows.Forms.Form Dim val1, val2, val3 As Double

Private Sub cmdMax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMax.Click

val1 = txtFirst.Text val2 = txtSecond.Text val3 = txtThird.Text

lblTotal.Text = Math.Max(Math.Max(val1, val2), val3)

End SubEnd Class

Page 58: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

More Math FunctionsAbs(x) – returns absolute value of xCeiling(x) – rounds x to the smallest integerCos(x) returns the trigonometric cosine

Page 59: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Exp(x) – returns the exponential ex

Floor(x) – rounds x to the largest integer not greater than x

Log(x) – returns the natural logarithm of x (base e)

Max(x,y) – returns the larger value of x and y

Min(x,y) – returns the smallest value of x and y

Page 60: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Pow(x,y) – Calculates x reaised to power y(xy)Sin(x) – returns the trigonometric sine of x(x

in radians)Sqrt(x) – returns the square root of xTan(x) – returns the trigonometric tangent of

x (x in radians)

Page 61: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

RandomDim randomObject as Random = New Random()Dim randomNumber as Integer =

randomObject.Next()

Page 62: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim randomObject As Random = New Random

Dim randomNumber As Integer

randomNumber = randomObject.Next(1, 10)

lblRandom.Text = randomNumber End Sub

Page 63: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

ModulesProgrammers use modules to group related

procedures so that they can be reused in other projects

Page 64: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

How to add modulesGoto File > Add new itemOn the template select module

Page 65: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Module Module1 Function reuse(ByVal lbl As Label) lbl.Text = "jpacs" End FunctionEnd Module

Page 66: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

ConvertUse to convert variables or value on a

desired data typeExample

Convert.ToString (val2)Convert.ToChar(1)

Page 67: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Array

Can store information and has indexes or memory address

Array have the same name and same typeThe first element of an array is 0th ElementThe position on the array is called index

Page 68: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Public Class Form1Inherits System.Windows.Forms.FormDim numarray(10) As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

numarray(0) = 1 Label1.Text = numarray(0)

End SubEnd Class

Page 69: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

GUILabel – An area in which icons or un editable

text is displayedTextbox – An area in which the user inputs data

from the keyboard. This can also display information

Button – An area that triggers an event when clicked

CheckBox – A component that is either selected on unselected

Page 70: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

ComboBox – A drop-down list of items from which the user can make a selection either by clicking an item in the list or by typing into a box

ListBox – An area in which a list of items is displayed. The user can make a selection from the list by clicking on any item. Multiple elements can be selected.

Page 71: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Panel – A container in which components can be placed

Scrollbar – A component that allows the user to access a range of elements that normally cannot fit in the control’s container

Page 72: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

ToolboxContains all the windows forms and

componentsTo add a component on a form, select the

component on the toolbox and drag it on the form

Page 73: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Page 74: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Unload a formApplication.exit

Page 75: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Form Properties and EventsAcceptButton – Button that is clicked when Enter is

pressedAutoScroll – Boolean value that allows or disallows

scrollbars to appear when neededCancelButton – Button that is clicked when the

Escape key is pressedFormBorderStyle – Border style for the form (e.g.,

none, single, 3d)

Page 76: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Font – font of text displayed on the form, and the default font of controls added to the form

Text – Text in the form’s title bar

Page 77: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Common MethodsClose – Closes a form and releases all

resources. A closed form cannot be open.Hide – Hides formShow – Display hidden forms

Page 78: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Code formName.ActiveForm.Close()

Page 79: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

TextboxPassing a value:

txtEmpNoOut.Text = txtEmpNo.TexttxtEmpNameOut.Text = txtLastName.Text &

" " & txtFirstName.Text

Page 80: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Mathematical Operator on Text Box txtPhilhealth.Text = Val(txtSalaryMain.Text) *

0.05 txtTax.Text = Val(txtSalaryMain.Text) * 0.04 txtSSS.Text = Val(txtSalaryMain.Text) * 0.03

Page 81: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Combo BoxAdding Items on runtime

ComboBox1.Items.Add("ABC")Clearing Items on runtime

ComboBox1.Items.Clear()

Page 82: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Properties of a combo boxDropDownStyle – Dropdownlist, Dropdown,

SimpleItems - use to insert all the choices on your

combo box

Page 83: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Adding Items on Combo on runtimecmbChoice.Items.Add("ABC")

Clearing the items on the combo boxcmbChoice.Items.Clear()

Page 84: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Getting the items on a combocmbChoice.Items.Item (index number)

Page 85: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Radio ButtonAllows a user to choose a optionsProperties

Check = combo box is selected (True = Selected, False = Un Selected)

Text = Text displayed on option Button

Page 86: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Example GUI 1CalculatorPayroll System

Page 87: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Picture BoxesA picture box displays an image. The

image, set by an object of class Image, can be a bitmap, a GIF, JPEG, icon or metafile format.

Page 88: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Picture Box PropertiesImage – sets the image to display in the

Picture BoxSizeMode – Enumeration that controls

image sizing and positioning. StretchImage, AutoSize and Center Image)

Page 89: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Normal places the image in top-left corner of picture box

CenterImage puts the image in middle (both truncate image if it is too large)

StretchImage resizes image to fit in PictureBox.

AutoSize resizes PictureBox to hold image

Page 90: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Loading Picture on Runtime

picOne.Image = Image.FromFile("D:\VB.net\LaboratoryExercise\Picture Swapper\Pictures\Pic2.jpg")

Page 91: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Selecting the Current directory or Path.fromfile (directory.GetCurrentDirectory)

Page 92: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Example GUI 2Picture Swapper

Page 93: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

List BoxAllows the user to view and select from

multiple items in a list

Page 94: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

List Box PropertiesItems – Collection of an item in a listboxSelectedIndex – Returns the index of the

selected item. If selected multiple items, the property returns arbitrary returns one of the selected indeces. If no items are selected, the property returns to -1

Page 95: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

SelectedIndeces – Returns a collection containing the indeces for all selected items

SelectedItem – Returns a reference to the selected item (If multiple items are selected, it returns the items with the lowest number of index)

SelectedItems – Returns a collection of selected items

Page 96: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

SelectionMode – Determines the number of items that can be selectedValues(None, One, Multiple)

Sorted – Indicates whether items are sorted alphabetically

Page 97: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Adding of ItemlistOrder.Items.Add(“Item to Add”)

Page 98: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Removing of items listOrder.Items.Clear()

Counting of items on a listlistOrder.Items.Count

Page 99: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

CheckedListBoxesControl derives from Listbox but contains

checkbox before each item

Page 100: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

PropertiesCheckedItems – Contains the collection

of items that are checkedCheckedIndeces – Returns all indexes

for checked itemsSelectionMode – Determines how many

items can be checked. One(Multiple) and None(Does not allow)

Page 101: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

To get checked itemtmp = listOrder.GetItemChecked(ctr - 1)

If tmp = True Then

listOrder.Items.RemoveAt(ctr - 1)

End If

Page 102: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Remove itemlistOrder.Items.RemoveAt(ctr - 1)

Page 103: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Example GUI 3Font BrowserPOS

Page 104: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Creating ShapesImport System.Drawing on your program

Page 105: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Imports System.DrawingPublic Class frmShapes Dim mygraphics As Graphics = MyBase.CreateGraphics Dim mypen As New Pen(Color.DarkRed)

Private Sub cmbShapes_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbShapes.SelectedIndexChanged

If cmbShapes.Text = "Ellipse" Then mygraphics.Clear(Color.White) mygraphics.DrawEllipse(mypen, 100, 100, 100, 10) End If End SubEnd Class

Page 106: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Ellipsemygraphics.DrawEllipse(color of pen, x, y,

height, width)

Page 107: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Tree ViewDisplays nodes hierarchically in a tree.Nodes are objects that contain values and

can refer to other nodesParent node can contain child nodesA child node can be a parent node to other

nodes

Page 108: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

First node on the tree is the root nodeTwo child nodes are called siblings node

Page 109: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Properties of Tree ViewCheckboxes – Indicates whether a

checkboxes appear next to nodeImageList – Specifies ImageList containing

the node iconNodes – List the collection of TreeNode, in

the control

Page 110: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Methods on Treeview.NodeAdd (Adds a tree node object)Clear (Clears the entire collection of tree

node)Remove (Delete a specific node)

Page 111: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

CodePublic Class frmTree Private Sub btnAdd_Click(ByVal sender As

System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

Dim StudName As String StudName = txtLName.Text + ", " +

txtFName.Text + " " + txtMI.Text If cmbCourse.Text = "ACT" Then

treeStudents.Nodes(0).Nodes(0).Nodes.Add(StudName)

End If End SubEnd Class

Page 112: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Font PropertiesNew Font(Font,Size,Font Style)

Page 113: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

CodeLabel1.Font = New Font("Comic Sans

Serif", 14, FontStyle.Italic, GraphicsUnit.Pixel)

Page 114: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Checked boxSmall white square that either is blank or

contains a checkmark. When a checkbox is selected, a blank checkmark appears in the box.

Page 115: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Properties of a checkboxChecked – Indicates whether the checkbox

is checked or markedCheckState – Indicates whether the

checkbox is checked or uncheckedText – Specifies the text displayed to the

right of the Checkbox called label

Page 116: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

CodeIf chkBold.CheckState =

CheckState.Checked Then txtOutput.Font = New

Font(cmbFonts.Text, 12, FontStyle.Bold)

Else txtOutput.Font = New

Font(cmbFonts.Text, 12, FontStyle.Regular)

End If

Page 117: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Common Dialog BoxA rather tedious, but quite common, task in

nearly every application is to prompt the user for filenames, font names and sizes, or colors to be used by the application

Page 118: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

OpenFileDialogOpenFileDialog Lets users select a file

to open. It also allows the selection of multiple files, for applications that must process many files at once (change the format of the selected files, for example).

Page 119: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Files and StreamsCreate Directory contains information

about a directoryAppendText – Returns a StreamWriter that

appends to an existing file or creates a file if one does not exist

Page 120: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Database Programming using ADO.netAdo.net object model provides an API for

accessing database systems programmatically.

Was created for the .NET framework and is the next generation of ActiveX Data Objects (ADO), which was designed to interact with Microsoft’s Components Object Model (COM) framework

Page 121: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

ADO.netsimplify the code you writeallow you to bind graphical controls to

DataSets

Page 122: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Primary Library of ADO.netSystem.dataSystem.data.OleDbSystem.Data.SqlClientSystem.Data.Dataset

Page 123: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Data Setuses the DataSet to store disconnected

data

Page 124: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Adding OledbAdapterClick on Tools > Choose Toolbox Items Click on .Net Framework ComponetsSelect OledbAdapter and Click OK

Page 125: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

oleDbConnectionThe oleDbConnection object uses OLE DB

to connect to a data source. ADO.NETalso includes a new type of connection:

SQLConnection

Page 126: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Dim connString As String = _“Provider= SQLOLEDB.1;Data

Source=localhost;” & _“uid=sa;pwd=;Initial Catalog=northwind;”Dim myConn As New oleDbConnection()myConn.ConnectionString = connStringmyConn.Open()

Page 127: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Example DatabaseDbase Application

Page 128: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

The End!!!!

Page 129: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Data Adapter WizardNew Connection

Data Source – Database ConnectionDatabase File Name – File Name of the

databaseChange the Datasource

Microsoft Access Database FileClick OkFind the Access FileTest the connection

Page 130: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

ContinuationClick OKChoose Use SQL StatementsInsert your SQL Click Finish

Page 131: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

More…You will be returned at the design tabThere are two components that has been

added: oleDbDataAdapter1 oleDbConnection1

Page 132: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

The oleDbConnection1 control describes the database connection;

The oleDbDataAdapter1 control describes your actual query

In VB.NET, the object you use to store data is called a DataSet.

Page 133: Vb.net Lecture

VB .net © COPYRIGHT BY BADBOY INC PRESENTED BY:SIR “JPACS”

Data SetClick once on the form. Notice that on the menu bar, a new menu

item has appeared: DataSetDrag the DataSet on the formSelect untype data sets