39687643-vb6-0

Upload: vinyogaesh1

Post on 10-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 39687643-VB6-0

    1/34

    1

    Visual Basic 6.0 Visual Basic 6.0

    Presented by Presented by-- Vinoth.K.S Vinoth.K.S

  • 8/8/2019 39687643-VB6-0

    2/34

    2

    How to open a Pr oje ct?How to open a Pr oje ct?

  • 8/8/2019 39687643-VB6-0

    3/34

    3

    How to open a Pr oje ctHow to open a Pr oje ct--22

  • 8/8/2019 39687643-VB6-0

    4/34

    4

    How to w rite code s in an even tHow to w rite code s in an even t

  • 8/8/2019 39687643-VB6-0

    5/34

    5

    C ommen ts li ne in VBC ommen ts li ne in VBH ow to write comments in VBH ow to write comments in VB

    ' This is a c ommen t' This is a c ommen tSome impo rta n t data ty pe s in VBSome impo rta n t data ty pe s in VB

    StringStringDo ubl eDo ubl eIn tege rIn tege rD ateD ate

    How to de clar e v ariabl esHow to de clar e v ariabl esImp licitImp licit

    T otal = T otal + 10 T otal = T otal + 10

    Ex plicitEx plicitD im Total as do ubl eD im Total as do ubl e

  • 8/8/2019 39687643-VB6-0

    6/34

    Scope of a Variabl eScope of a Variabl eT he term T he term ScopeScope refers to whether the variable is available outside the procedure in which itrefers to whether the variable is available outside the procedure in which itappears. T he scope isappears. T he scope is proced ur e proced ur e--leve lleve l oror mod ulemod ule--leve lleve l..

    A variable declared with Dim at the beginning of a procedure is only available in that procedure. A variable declared with Dim at the beginning of a procedure is only available in that procedure. When the procedure ends, the variable disappears. Consider the following example: When the procedure ends, the variable disappears. Consider the following example:

    Option ExplicitOption ExplicitDim T otal2 As IntegerDim T otal2 As Integer

    Private Sub Command1_Click ()Private Sub Command1_Click ()Dim T otal1 As IntegerDim T otal1 As IntegerStatic T otal3 As IntegerStatic T otal3 As Integer T otal1 = T otal1 + 1 T otal1 = T otal1 + 1 T otal2 = T otal2 + 1 T otal2 = T otal2 + 1 T otal3 = T otal3 + 1 T otal3 = T otal3 + 1

    End SubEnd SubPrivate Sub Command2_Click ()Private Sub Command2_Click ()

    Dim T otal1 As IntegerDim T otal1 As Integer T otal1 = T otal1 + 1 T otal1 = T otal1 + 1 T otal2 = T otal2 + 1 T otal2 = T otal2 + 1 T otal3 = T otal3 + 1 T otal3 = T otal3 + 1

    End SubEnd Sub

  • 8/8/2019 39687643-VB6-0

    7/34

    7

    W hat ar e d ifferen t prope rties of a W hat ar e d ifferen t prope rties of a

    T ext B ox T ext B ox

  • 8/8/2019 39687643-VB6-0

    8/34

    8

    D ifferen t con trols of a T ext B oxD ifferen t con trols of a T ext B oxN ame prefix is asN ame prefix is as txt,txt, e. g. txtN amee. g. txtN ameE.g.E.g.

    T ext1.BackColor = vbRed T ext1.BackColor = vbRedT ext1.FontSize = 20 T ext1.FontSize = 20T ext1.Alignment = 2 T ext1.Alignment = 2T ext1.ForeColor = vbYellow T ext1.ForeColor = vbYellow T ext1.BorderStyle = 0 T ext1.BorderStyle = 0

    T ext1.FontBold = T rue T ext1.FontBold = T rueT ext1.tooltiptext=enter something T ext1.tooltiptext=enter somethingT ext1.text= T ext1 T ext1.text= T ext1

  • 8/8/2019 39687643-VB6-0

    9/34

    9

    M ulti li ne prope rty of T ext b oxesM ulti li ne prope rty of T ext b oxes

    Select the multi lineSelect the multi lineproperty as one of theproperty as one of thefollowing:following:

    T rue T ruefalsefalse

    Select the scroll barSelect the scroll baroptionoption

    H orizontalH orizontal Vertical VerticalN oneN one

    BothBoth

  • 8/8/2019 39687643-VB6-0

    10/34

    10

    D ifferen t con trols of a lab el boxD ifferen t con trols of a lab el box

    N ame prefix is asN ame prefix is as lbl,lbl, e. g. lblN amee. g. lblN ameE.g.E.g.

    Label1.BackColor = vbRedLabel1.BackColor = vbRedLabel1.FontSize = 20Label1.FontSize = 20Label1.Alignment = 2Label1.Alignment = 2Label1.ForeColor = vbYellow Label1.ForeColor = vbYellow Label1.BorderStyle = 0Label1.BorderStyle = 0Label1.FontBold = T rueLabel1.FontBold = T rueLabel1. T ool T ip T ext = "enterLabel1. T ool T ip T ext = "enterSomething"Something"Label1.Caption = " H I"Label1.Caption = " H I"

  • 8/8/2019 39687643-VB6-0

    11/34

    11

    F rame C on trolF rame C on trol

    Frame1.BackColor = vbRedFrame1.BackColor = vbRedFrame1.FontSize = 20Frame1.FontSize = 20Frame1.FontBold = T rueFrame1.FontBold = T rueFrame1.BorderStyle = 1Frame1.BorderStyle = 1Frame1.Caption = " H ello"Frame1.Caption = " H ello"Frame1. T ool T ip T ext = "enterFrame1. T ool T ip T ext = "enterSomething"Something"

  • 8/8/2019 39687643-VB6-0

    12/34

    12

    C omm and butt on C on trolC omm and butt on C on trolPrivate Sub Command1_Click()Private Sub Command1_Click()

    MsgBox "You have Clicked SubmitMsgBox "You have Clicked Submitbutton", vbInformationbutton", vbInformation

    End SubEnd Sub

    Private Sub Form_Load()Private Sub Form_Load()Command1. T ool T ip T ext = "Click here"Command1. T ool T ip T ext = "Click here"Command1.Caption = "Submit"Command1.Caption = "Submit"

    End SubEnd Sub

  • 8/8/2019 39687643-VB6-0

    13/34

    13

    C on trols of C heck b oxesC on trols of C heck b oxesPrivate Sub Check1_Click()Private Sub Check1_Click()

    MsgBox "You have Clicked Assam"MsgBox "You have Clicked Assam"End SubEnd Sub

    Private Sub Command1_Click()Private Sub Command1_Click()

    Check1.BackColor = vbYellow Check1.BackColor = vbYellow Check1.Caption = "Command1"Check1.Caption = "Command1"Check2.BackColor = vbBlack Check2.BackColor = vbBlack Check2.ForeColor = vbWhiteCheck2.ForeColor = vbWhite

    End SubEnd Sub

    Private Sub Form_Load()Private Sub Form_Load()Check1.Caption = "Assam"Check1.Caption = "Assam"Check2.Caption = "Bihar"Check2.Caption = "Bihar"

    End SubEnd Sub

  • 8/8/2019 39687643-VB6-0

    14/34

    14

    C on trols of O ption Butt onC on trols of O ption Butt on

    Private Sub Command1_Click()Private Sub Command1_Click()If Option1.Value = T rue T henIf Option1.Value = T rue T henMsgBox "You have selected the firstMsgBox "You have selected the firstoptionoption

    End If End If

    If Option2.Value = T

    rue T

    henIf Option2.Value = T

    rue T

    henMsgBox "You have selected the secondMsgBox "You have selected the secondoption"option"End If End If

    End SubEnd Sub

  • 8/8/2019 39687643-VB6-0

    15/34

    15

    C om bo Box C on trolC om bo Box C on trolPrivate Sub cmdAdd N ew_Click()Private Sub cmdAdd N ew_Click()

    Combo1.AddItem T ext1. T extCombo1.AddItem T ext1. T ext T ext1. T ext = "" T ext1. T ext = ""

    End SubEnd Sub

    Private Sub cmdClear_Click()Private Sub cmdClear_Click()Combo1.ClearCombo1.Clear

    End SubEnd Sub

    Private Sub Form_Load()Private Sub Form_Load()Combo1.BackColor = vbRedCombo1.BackColor = vbRedCombo1.FontSize = 5Combo1.FontSize = 5Combo1.ForeColor = vbWhiteCombo1.ForeColor = vbWhiteCombo1.List(0) = "Assam"Combo1.List(0) = "Assam"Combo1.List(1) = "Bihar"Combo1.List(1) = "Bihar"Combo1.List(2) = "Urissa"Combo1.List(2) = "Urissa"Combo1.List(3) = "Uttar Pradesh"Combo1.List(3) = "Uttar Pradesh"

    End SubEnd Sub

  • 8/8/2019 39687643-VB6-0

    16/34

    16

    L ist B ox C on trolL ist B ox C on trolPrivate Sub cmdAdd N ew_Click()Private Sub cmdAdd N ew_Click()

    List1.AddItem T ext1. T extList1.AddItem T ext1. T ext T ext1. T ext = "" T ext1. T ext = ""

    End SubEnd Sub

    Private Sub cmdClear_Click()Private Sub cmdClear_Click()List1.ClearList1.Clear

    End SubEnd Sub

    Private Sub Form_Load()Private Sub Form_Load()List1.BackColor = vbRedList1.BackColor = vbRedList1.FontSize = 5List1.FontSize = 5List1.ForeColor = vbWhiteList1.ForeColor = vbWhiteList1.List(0) = "Assam"List1.List(0) = "Assam"List1.List(1) = "Bihar"List1.List(1) = "Bihar"List1.List(2) = "Urissa"List1.List(2) = "Urissa"List1.List(3) = "Uttar Pradesh"List1.List(3) = "Uttar Pradesh"

    End SubEnd Sub

  • 8/8/2019 39687643-VB6-0

    17/34

    17

    IFIF --Els e C ond itionEls e C ond itionPrivate Sub cmdAdd_Click()Private Sub cmdAdd_Click()

    If If txtFirst N umber = txtFirstN umber = T hen T henMsgBox "Please Enter First N umber"MsgBox "Please Enter First N umber"txtFirstN umber.SetFocustxtFirstN umber.SetFocus

    ElseIf ElseIf txt N umber T wo = txtN umber T wo = T hen T henMsgBox "Please Enter Second N umber"MsgBox "Please Enter Second N umber"txtN umber T wo.SetFocustxtN umber T wo.SetFocus

    ElseIf ElseIf Is N umeric(txtFirstN umber) = FalseIsN umeric(txtFirstN umber) = False T hen T henMsgBox "Please Enter N umberic Value"MsgBox "Please Enter N umberic Value"txtFirstN umber.SetFocustxtFirstN umber.SetFocus

    ElseIf ElseIf Is N umeric(txtN umber T wo) = FalseIsN umeric(txtN umber T wo) = False T hen T henMsgBox "Please Enter N umberic Value"MsgBox "Please Enter N umberic Value"txtN umber T wo.SetFocustxtN umber T wo.SetFocus

    ElseElse

    lblResult = Val(txtFirstN

    umber) + Val(txtN

    umber T

    wo)lblResult = Val(txtFirstN

    umber) + Val(txtN

    umber T

    wo)End If End If End SubEnd Sub

    Private Sub cmdClear_Click()Private Sub cmdClear_Click()txtN umber T wo = ""txtN umber T wo = ""txtFirstN umber = ""txtFirstN umber = ""

    End SubEnd Sub

  • 8/8/2019 39687643-VB6-0

    18/34

    18

    SE L E C T CA SESE L E C T CA SECan be used as an alternative to theCan be used as an alternative to theIf ...Th en ...Els eIf ...Th en ...Els e structure, especially whenstructure, especially whenmany comparisons are involved.many comparisons are involved.

    Private Sub Command1_Click()Private Sub Command1_Click()

    Select Case Val(txtEnterCase)Select Case Val(txtEnterCase)Case 1:Case 1: lblResult = "Small"lblResult = "Small"Case 2:Case 2: lblResult = "Medium"lblResult = "Medium"Case 3:Case 3: lblResult = "big"lblResult = "big"Case 4:Case 4: lblResult = "Extra Large"lblResult = "Extra Large"Case ElseCase Else

    MsgBox "Invalid Choice"MsgBox "Invalid Choice"End SelectEnd Select

    End SubEnd Sub

  • 8/8/2019 39687643-VB6-0

    19/34

    19

    D O.. L OOPD O.. L OOPUsed to execute a block of statements an unspecifiedUsed to execute a block of statements an unspecifiednumber of times.number of times.

    Do While conditionDo While conditionstatementsstatements

    LoopLoop

    First, the condition is tested; if condition is T rue,First, the condition is tested; if condition is T rue,then the statements are executed. When it gets to thethen the statements are executed. When it gets to theLoop it goes back to the Do and tests conditionLoop it goes back to the Do and tests conditionagain. If condition is False on the first pass, theagain. If condition is False on the first pass, thestatements are never executed.statements are never executed.

    Dim i, j As IntegerDim i, j As IntegerPrivate Sub Command1_Click()Private Sub Command1_Click()

    i = InputBox("Enter the limit", "DOi = InputBox("Enter the limit", "DO- -W H ILE W H ILE--LOOP", 5)LOOP", 5)j = 1j = 1Do While j

  • 8/8/2019 39687643-VB6-0

    20/34

    20

    W HI L E. W EN D W HI L E. W EN DUsed to execute a block of statements an unspecifiedUsed to execute a block of statements an unspecifiednumber of times.number of times.

    W hil e W hil e conditionconditionstatementsstatements

    W end W endFirst, the condition is tested; if condition is T rue,First, the condition is tested; if condition is T rue,then the statements are executed. When it gets to thethen the statements are executed. When it gets to theLoop it goes back to the Do and tests conditionLoop it goes back to the Do and tests conditionagain. If condition is False on the first pass, theagain. If condition is False on the first pass, thestatements are never executed.statements are never executed.

    Dim i, j As IntegerDim i, j As IntegerPrivate Sub Command1_Click()Private Sub Command1_Click()

    i = InputBox ("Enter the limit", "DOi = InputBox ("Enter the limit", "DO- -W H ILE W H ILE--LOOP", 5)LOOP", 5)j = 1j = 1

    While j

  • 8/8/2019 39687643-VB6-0

    21/34

    21

    Pri v ate Sub F orm_ L oad() ' d is play th e num bers f rom 0 to 9 F or i = 0 T o 9

    Text1.Text = i & ","

    N ext i E nd Sub

    -----------------------------------------------------------------------------------------------------

    L et us tak e a cl oser loo k at our for loop:

    ' For - for loop

    ' i - use i as our integer

    ' 0 - start value = 0

    ' To - between start and stop value

    ' 9 - stop value = 9

    ' N ext - go to next step (if i < 9 then end for loop)

    De scri ption : ' T he for loop will loop from the given start walue to the given stop value. ' T he amount of loops willthen be 10 (first loop i=0).

    F OR.NETF OR.NET

  • 8/8/2019 39687643-VB6-0

    22/34

    22

    F uncti on proced ure and SubF uncti on proced ure and SubPr oced ur e in VBPr oced ur e in VB

    Difference between Function & Procedure isDifference between Function & Procedure is A procedure is a set of code that does the work but does not return a value whereas a A procedure is a set of code that does the work but does not return a value whereas afunction accepts parameters and does the calculation and does return a value back.function accepts parameters and does the calculation and does return a value back.H ow to write Function Procedure and Sub Procedure in Visual BasicH ow to write Function Procedure and Sub Procedure in Visual Basic

    Sub tellOperator(ByVal task As String)Sub tellOperator(ByVal task As String)

    Dim stamp As DateDim stamp As Datestamp = T imeofDay()stamp = T imeofDay()MsgBox ("Starting " & task & " at " & CStr(stamp))MsgBox ("Starting " & task & " at " & CStr(stamp))

    End SubEnd Sub

    Private Sub Command1_Click()Private Sub Command1_Click()Call tellOperator("Dwipen Laskar")Call tellOperator("Dwipen Laskar")

    End SubEnd Sub

    Private Function T imeofDay()Private Function T imeofDay() T imeofDay = T ime T imeofDay = T ime

    End FunctionEnd Function

  • 8/8/2019 39687643-VB6-0

    23/34

    23

    How to Pass A rray i n a F uncti onHow to Pass A rray i n a F uncti onPrivate Sub Command1_Click()Private Sub Command1_Click()

    Dim x(3) As IntegerDim x(3) As Integer 'Declare a Static Integer Array of 4'Declare a Static Integer Array of 4elementselementsx(0) = 10x(0) = 10x(1) = 20x(1) = 20x(2) = 30x(2) = 30x(3) = 40x(3) = 40Call AcceptArray(x)Call AcceptArray(x) 'Call the procedure and pass the Array 'Call the procedure and pass the Array

    End SubEnd Sub

    Private Sub AcceptArray(intArray() As Integer)Private Sub AcceptArray(intArray() As Integer)Dim obj As VariantDim obj As VariantFor Each obj In intArray For Each obj In intArray

    Form1.Print objForm1.Print objN extN ext

    End SubEnd Sub

    Private Function ReturnArray() As VariantPrivate Function ReturnArray() As VariantDim x(3) As IntegerDim x(3) As Integer 'Declare a Static Integer Array of 4 elements'Declare a Static Integer Array of 4 elementsx(0) = 1x(0) = 1x(1) = 2x(1) = 2x(2) = 3x(2) = 3x(3) = 4x(3) = 4ReturnArray = x 'Pass the array back as a return valueReturnArray = x 'Pass the array back as a return value

    End FunctionEnd FunctionPrivate Sub Command2_Click()Private Sub Command2_Click()

  • 8/8/2019 39687643-VB6-0

    24/34

    24

    How to us e M essa ge BoxHow to us e M essa ge Box

    Used for displaying Used for displaying messagesmessages

    PromptPrompt

    SymbolSymbolButtonButtontitletitle

  • 8/8/2019 39687643-VB6-0

    25/34

    25

    How to us e Inp ut B ox?How to us e Inp ut B ox?

    Used for taking inputsUsed for taking inputsfrom usersfrom users

    PromptPromptT itle T itleDefaultDefaultX X--positionposition

    Y Y--positionposition

  • 8/8/2019 39687643-VB6-0

    26/34

    26

    Ho rizon tal/ V ertical Scr oll BarHo rizon tal/ V ertical Scr oll BarPrivate Sub Form_Load()Private Sub Form_Load()

    H Scroll1.Value = 0H Scroll1.Value = 0H Scroll1.Min = 0H Scroll1.Min = 0H Scroll1.Max = 100H Scroll1.Max = 100

    VScroll1.Value = 0 VScroll1.Value = 0 VScroll1.Max = 100 VScroll1.Max = 100 VScroll1.Min = 0 VScroll1.Min = 0Label1.Caption = H Scroll1.ValueLabel1.Caption = H Scroll1.Value

    End SubEnd Sub

    Private Sub H Scroll1_Change()Private Sub H Scroll1_Change()Label1 = ""Label1 = ""Label1 = H Scroll1.ValueLabel1 = H Scroll1.Value

    End SubEnd Sub

    Private Sub VScroll1_Change()Private Sub VScroll1_Change()Label1 = ""Label1 = ""Label1 = VScroll1.ValueLabel1 = VScroll1.Value

    End SubEnd Sub

  • 8/8/2019 39687643-VB6-0

    27/34

    27

    How to inclu de add ins a nd How to inclu de add ins a nd L

    ibrari esL

    ibrari es

  • 8/8/2019 39687643-VB6-0

    28/34

    28

    How to us e th e Ti me r?How to us e th e Ti me r?Private Sub Command1_Click()Private Sub Command1_Click() T imer1.Enabled = T rue T imer1.Enabled = T rueEnd SubEnd Sub

    Private Sub T imer1_ T imer()Private Sub T imer1_ T imer() T ext1. T ext = T ime T ext1. T ext = T imeEnd SubEnd Sub

  • 8/8/2019 39687643-VB6-0

    29/34

    29

    How to us e M on th Vi ewe rHow to us e M on th Vi ewe rPrivate Sub Form_Load()

    MonthView1.BackColor = vbGreenMonthView1.Appearance = ccFlatMonthView1.ForeColor = vbRed

    End Sub

    Private Sub MonthView1_DateClick(ByValDateClicked As Date)MsgBox "you have clicked " &Format(MonthView1.Value, "dd-mm-yyyy"), vbInformation

    End Sub

  • 8/8/2019 39687643-VB6-0

    30/34

    30

    How to us e D ate Pick erHow to us e D ate Pick er

  • 8/8/2019 39687643-VB6-0

    31/34

    31

    How to C reate M anusHow to C reate M anus

  • 8/8/2019 39687643-VB6-0

    32/34

    32

    How to us e M anusHow to us e M anusPrivate Sub mnDisplay_Click()Private Sub mnDisplay_Click()

    MsgBox "H ello, You have Clicked Display"MsgBox "H ello, You have Clicked Display"End SubEnd Sub

  • 8/8/2019 39687643-VB6-0

    33/34

    33

    How to us e C ommon D ialog How to us e C ommon D ialog C

    on trolC

    on trolPrivate Sub mnDisplay_Click()Private Sub mnDisplay_Click()On Error Go T o err:On Error Go T o err:CommonDialog1.Action = 3CommonDialog1.Action = 3'display color dialog box'display color dialog boxForm2.BackColor =Form2.BackColor =CommonDialog1.ColorCommonDialog1.ColorExit SubExit Suberr:err:MsgBox "Dialog is canceled"MsgBox "Dialog is canceled"

    End SubEnd Sub

  • 8/8/2019 39687643-VB6-0

    34/34

    34

    How to Us e M SHow to Us e M S--F lex Gri dF lex Gri dPrivate Sub Command1_Click()Private Sub Command1_Click()

    MSFlexGrid1. T extMatrix(9, 2) =MSFlexGrid1. T extMatrix(9, 2) = Val(MSFlexGrid1. T extMatrix(1, 2)) + Val(MSFlexGrid1. T extMatrix(1, 2)) + Val(MSFlexGrid1. T extMatrix(2, 2)) + Val(MSFlexGrid1. T extMatrix(2, 2)) + Val(MSFlexGrid1. T extMatrix(3, 2)) Val(MSFlexGrid1. T extMatrix(3, 2))

    End SubEnd Sub

    Private Sub Form_Load()Private Sub Form_Load()MSFlexGrid1. T extMatrix(9, 2) = 0MSFlexGrid1. T extMatrix(9, 2) = 0MSFlexGrid1. T extMatrix(0, 0) = "S. N o"MSFlexGrid1. T extMatrix(0, 0) = "S. N o"MSFlexGrid1. T extMatrix(0, 1) = " N ame"MSFlexGrid1. T extMatrix(0, 1) = " N ame"MSFlexGrid1. T extMatrix(0, 2) = "Age"MSFlexGrid1. T extMatrix(0, 2) = "Age"MSFlexGrid1. T extMatrix(1, 0) = "1"MSFlexGrid1. T extMatrix(1, 0) = "1"MSFlexGrid1. T extMatrix(1, 1) = "Dwipen"MSFlexGrid1. T extMatrix(1, 1) = "Dwipen"MSFlexGrid1. T extMatrix(1, 2) = 29MSFlexGrid1. T extMatrix(1, 2) = 29MSFlexGrid1. T extMatrix(2, 0) = "2"MSFlexGrid1. T extMatrix(2, 0) = "2"MSFlexGrid1. T extMatrix(2, 1) = "S Adarsh"MSFlexGrid1. T extMatrix(2, 1) = "S Adarsh"MSFlexGrid1. T extMatrix(2, 2) = 27MSFlexGrid1. T extMatrix(2, 2) = 27MSFlexGrid1. T extMatrix(3, 0) = "3"MSFlexGrid1. T extMatrix(3, 0) = "3"MSFlexGrid1. T extMatrix(3, 1) = "Sachin"MSFlexGrid1. T extMatrix(3, 1) = "Sachin"MSFlexGrid1. T extMatrix(3, 2) = 56MSFlexGrid1. T extMatrix(3, 2) = 56MSFlexGrid1. T extMatrix(9, 0) = " T otal"MSFlexGrid1. T extMatrix(9, 0) = " T otal"MSFlexGrid1. T extMatrix(9, 1) = "Age"MSFlexGrid1. T extMatrix(9, 1) = "Age"

    End SubEnd Sub