VB6.0

Post on 21-Nov-2014

241 views 0 download

Tags:

transcript

1

Visual Basic 6.0Visual Basic 6.0

Presented by-Presented by-

Sri Jahnab Kr. Sri Jahnab Kr. DekaDeka

2

How to open a Project?How to open a Project?

3

How to open a Project-2How to open a Project-2

4

How to write codes in an How to write codes in an eventevent

5

Comments line in VBComments line in VB How to write comments in VBHow to write comments in VB

' This is a comment' This is a comment Some important data types in VBSome important data types in VB

StringString DoubleDouble IntegerInteger DateDate

How to declare variablesHow to declare variables ImplicitImplicit

Total = Total + 10Total = Total + 10

ExplicitExplicit Dim Total as doubleDim Total as double

Total= Total + 10Total= Total + 10

6

Scope of a VariableScope of a Variable The term The term ScopeScope refers to whether the variable is available outside the refers to whether the variable is available outside the

procedure in which it appears. The scope is procedure in which it appears. The scope is procedure-levelprocedure-level or or module-module-levellevel..

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

Option ExplicitOption Explicit                    Dim Total2 As Integer                    Dim Total2 As Integer

                     Private Sub Command1_Click ()Private Sub Command1_Click ()                    Dim Total1 As Integer                    Dim Total1 As Integer                    Static Total3 As Integer                    Static Total3 As Integer                    Total1 = Total1 + 1                    Total1 = Total1 + 1                    Total2 = Total2 + 1                    Total2 = Total2 + 1                    Total3 = Total3 + 1                    Total3 = Total3 + 1                     End SubEnd Sub

                     Private Sub Command2_Click ()Private Sub Command2_Click ()                    Dim Total1 As Integer                    Dim Total1 As Integer                    Total1 = Total1 + 1                    Total1 = Total1 + 1                    Total2 = Total2 + 1                    Total2 = Total2 + 1                    Total3 = Total3 + 1                    Total3 = Total3 + 1                     End SubEnd Sub

7

What are different What are different properties of a Text Boxproperties of a Text Box

8

Different controls of a Different controls of a Text BoxText Box

Name prefix is as Name prefix is as txt, txt, e. g. e. g. txtNametxtName

E.g.E.g. Text1.BackColor = vbRedText1.BackColor = vbRed Text1.FontSize = 20Text1.FontSize = 20 Text1.Alignment = 2Text1.Alignment = 2 Text1.ForeColor = vbYellowText1.ForeColor = vbYellow Text1.BorderStyle = 0Text1.BorderStyle = 0 Text1.FontBold = TrueText1.FontBold = True Text1.tooltiptext=“enter Text1.tooltiptext=“enter

something”something” Text1.text=Text1Text1.text=Text1

9

Multi line property of Multi line property of Text boxesText boxes

Select the multi line Select the multi line property as one of property as one of the following:the following: TrueTrue falsefalse

Select the scroll bar Select the scroll bar optionoption HorizontalHorizontal VerticalVertical NoneNone BothBoth

10

Different controls of a Different controls of a label boxlabel box

Name prefix is as Name prefix is as lbl, lbl, e. g. e. g. lblNamelblName

E.g.E.g. Label1.BackColor = vbRedLabel1.BackColor = vbRed Label1.FontSize = 20Label1.FontSize = 20 Label1.Alignment = 2Label1.Alignment = 2 Label1.ForeColor = vbYellowLabel1.ForeColor = vbYellow Label1.BorderStyle = 0Label1.BorderStyle = 0 Label1.FontBold = TrueLabel1.FontBold = True Label1.ToolTipText = "enter Label1.ToolTipText = "enter

Something"Something" Label1.Caption = "HI"Label1.Caption = "HI"

11

Frame ControlFrame Control

Frame1.BackColor = Frame1.BackColor = vbRedvbRed

Frame1.FontSize = 20Frame1.FontSize = 20 Frame1.FontBold = TrueFrame1.FontBold = True Frame1.BorderStyle = 1Frame1.BorderStyle = 1 Frame1.Caption = "Hello"Frame1.Caption = "Hello" Frame1.ToolTipText = Frame1.ToolTipText =

"enter Something""enter Something"

12

Command button ControlCommand button ControlPrivate Sub Command1_Click()Private Sub Command1_Click()

MsgBox "You have Clicked MsgBox "You have Clicked Submit button", vbInformationSubmit button", vbInformation

End SubEnd Sub

Private Sub Form_Load()Private Sub Form_Load()Command1.ToolTipText = Command1.ToolTipText = "Click here""Click here"Command1.Caption = "Submit"Command1.Caption = "Submit"

End SubEnd Sub

13

Controls of Check boxesControls of Check boxesPrivate Sub Check1_Click()Private Sub Check1_Click()

MsgBox "You have Clicked MsgBox "You have Clicked Assam"Assam"

End SubEnd Sub

Private Sub Command1_Click()Private Sub Command1_Click()Check1.BackColor = vbYellowCheck1.BackColor = vbYellowCheck1.Caption = Check1.Caption = "Command1""Command1"Check2.BackColor = vbBlackCheck2.BackColor = vbBlackCheck2.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

14

Controls of Option Controls of Option ButtonButton

Private Sub Command1_Click()Private Sub Command1_Click()

If Option1.Value = True ThenIf Option1.Value = True Then

MsgBox "You have selected the MsgBox "You have selected the first option“first option“

End IfEnd If

If Option2.Value = True ThenIf Option2.Value = True Then

MsgBox "You have selected the MsgBox "You have selected the second option"second option"

End IfEnd If

End SubEnd Sub

15

Combo Box ControlCombo Box ControlPrivate Sub cmdAddNew_Click()Private Sub cmdAddNew_Click()

Combo1.AddItem Text1.TextCombo1.AddItem Text1.TextText1.Text = ""Text1.Text = ""

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 = Combo1.ForeColor = vbWhitevbWhiteCombo1.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 Combo1.List(3) = "Uttar Pradesh"Pradesh"

End SubEnd Sub

16

List Box ControlList Box ControlPrivate Sub cmdAddNew_Click()Private Sub cmdAddNew_Click()

List1.AddItem Text1.TextList1.AddItem Text1.TextText1.Text = ""Text1.Text = ""

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

17

IF-Else ConditionIF-Else ConditionPrivate Sub cmdAdd_Click()Private Sub cmdAdd_Click()

If If txtFirstNumber = “ “txtFirstNumber = “ “ ThenThen MsgBox "Please Enter First Number"MsgBox "Please Enter First Number" txtFirstNumber.SetFocustxtFirstNumber.SetFocus

ElseIf ElseIf txtNumberTwo = “ “txtNumberTwo = “ “ Then Then MsgBox "Please Enter Second Number"MsgBox "Please Enter Second Number" txtNumberTwo.SetFocustxtNumberTwo.SetFocus

ElseIf ElseIf IsNumeric(txtFirstNumber) = False IsNumeric(txtFirstNumber) = False ThenThen

MsgBox "Please Enter Numberic Value"MsgBox "Please Enter Numberic Value" txtFirstNumber.SetFocustxtFirstNumber.SetFocus

ElseIf ElseIf IsNumeric(txtNumberTwo) = False IsNumeric(txtNumberTwo) = False ThenThen

MsgBox "Please Enter Numberic Value"MsgBox "Please Enter Numberic Value" txtNumberTwo.SetFocustxtNumberTwo.SetFocus

ElseElse lblResult = Val(txtFirstNumber) + lblResult = Val(txtFirstNumber) +

Val(txtNumberTwo)Val(txtNumberTwo)End IfEnd If

End SubEnd Sub

Private Sub cmdClear_Click()Private Sub cmdClear_Click()txtNumberTwo = ""txtNumberTwo = ""txtFirstNumber = ""txtFirstNumber = ""

End SubEnd Sub

18

SELECT CASESELECT CASE Can be used as an alternative to the Can be used as an alternative to the

If...Then...ElseIf...Then...Else structure, especially structure, especially when many comparisons are involved.when 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 = lblResult = "Small""Small"

Case 2: Case 2: lblResult = lblResult = "Medium""Medium"

Case 3: Case 3: lblResult = "big"lblResult = "big" Case 4: Case 4: lblResult = lblResult =

"Extra Large""Extra Large" Case ElseCase Else MsgBox "Invalid MsgBox "Invalid

Choice"Choice"End SelectEnd Select

End SubEnd Sub

19

DO…..LOOPDO…..LOOP Used to execute a block of statements an Used to execute a block of statements an

unspecified number of times.unspecified number of times.

Do While conditionDo While condition     statements     statementsLoopLoop

First, the condition is tested; if condition First, the condition is tested; if condition is True, then the statements are is True, then the statements are executed. When it gets to the Loop it executed. When it gets to the Loop it goes back to the Do and tests condition goes back to the Do and tests condition again. If condition is False on the first again. If condition is False on the first pass, the statements are never executed.pass, the statements are never executed.

Dim i, j As IntegerDim i, j As Integer

Private Sub Command1_Click()Private Sub Command1_Click()

i = InputBox("Enter the limit", "DO-WHILE-i = InputBox("Enter the limit", "DO-WHILE-LOOP", 5)LOOP", 5)

j = 1j = 1

Do While j <= iDo While j <= i

Form1.Print j;Form1.Print j;

j = j + 1j = j + 1

LoopLoop

End SubEnd Sub

20

WHILE….WENDWHILE….WEND Used to execute a block of statements Used to execute a block of statements

an unspecified number of times.an unspecified number of times.

WhileWhile condition condition     statements     statementsWendWendFirst, the condition is tested; if First, the condition is tested; if condition is True, then the statements condition is True, then the statements are executed. When it gets to the Loop are executed. When it gets to the Loop it goes back to the Do and tests it goes back to the Do and tests condition again. If condition is False on condition again. If condition is False on the first pass, the statements are never the first pass, the statements are never executed.executed.

Dim i, j As IntegerDim i, j As Integer

Private Sub Command1_Click()Private Sub Command1_Click()

i = InputBox ("Enter the limit", "DO-WHILE-i = InputBox ("Enter the limit", "DO-WHILE-LOOP", 5)LOOP", 5)

j = 1j = 1

While j <= iWhile j <= i

Form1.Print j;Form1.Print j;

j = j + 1j = j + 1

WendWend

End SubEnd Sub

21

Private Sub Form_Load() 'display the numbers from 0 to 9 For i = 0 To 9

Text1.Text = i & ","

Next i End Sub

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

Let us take a closer look 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

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

Description: ' The for loop will loop from the given start walue to the given stop value. ' The amount of loops will then be 10 (first loop i=0).

FOR….NETFOR….NET

22

Function procedure and Function procedure and Sub Procedure in VBSub Procedure in VB

Difference between Function & Procedure isDifference between Function & Procedure isA procedure is a set of code that does the work but does not return a A procedure is a set of code that does the work but does not return a value whereas a function accepts parameters and does the calculation value whereas a function accepts parameters and does the calculation and does return a value back. and does return a value back.

How to write Function Procedure and Sub Procedure in Visual BasicHow 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 = TimeofDay()stamp = TimeofDay()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 TimeofDay()Private Function TimeofDay()TimeofDay = TimeTimeofDay = Time

End FunctionEnd Function

23

How to Pass Array in a How to Pass Array in a FunctionFunctionPrivate Sub Command1_Click()Private Sub Command1_Click()

Dim x(3) As IntegerDim x(3) As Integer 'Declare a Static Integer Array of 4 elements 'Declare a Static Integer Array of 4 elements x(0) = 10x(0) = 10 x(1) = 20x(1) = 20 x(2) = 30x(2) = 30 x(3) = 40x(3) = 40

Call AcceptArray(x) Call AcceptArray(x) 'Call the procedure and pass the Array'Call the procedure and pass the ArrayEnd SubEnd Sub

Private Sub AcceptArray(intArray() As Integer)Private Sub AcceptArray(intArray() As Integer) Dim obj As VariantDim obj As Variant For Each obj In intArrayFor Each obj In intArray Form1.Print objForm1.Print obj NextNextEnd SubEnd Sub

Private Function ReturnArray() As VariantPrivate Function ReturnArray() As Variant Dim x(3) As Integer Dim x(3) As Integer 'Declare a Static Integer Array of 4 elements'Declare a Static Integer Array of 4 elements x(0) = 1x(0) = 1 x(1) = 2x(1) = 2 x(2) = 3x(2) = 3 x(3) = 4x(3) = 4 ReturnArray = x 'Pass the array back as a return valueReturnArray = x 'Pass the array back as a return valueEnd FunctionEnd FunctionPrivate Sub Command2_Click()Private Sub Command2_Click() Dim retval As VariantDim retval As Variant Dim objDim obj retval = ReturnArray 'Assign the return value to a Variant Variableretval = ReturnArray 'Assign the return value to a Variant Variable For Each obj In retvalFor Each obj In retval Form1.Print objForm1.Print obj NextNextEnd SubEnd Sub

24

How to use Message BoxHow to use Message Box

Used for displaying Used for displaying messagesmessages PromptPrompt SymbolSymbol ButtonButton titletitle

25

How to use Input Box?How to use Input Box?

Used for taking Used for taking inputs from usersinputs from users PromptPrompt TitleTitle DefaultDefault X-positionX-position Y-positionY-position

26

Horizontal/ Vertical Horizontal/ Vertical Scroll BarScroll Bar

Private Sub Form_Load()Private Sub Form_Load()HScroll1.Value = 0HScroll1.Value = 0HScroll1.Min = 0HScroll1.Min = 0HScroll1.Max = 100HScroll1.Max = 100VScroll1.Value = 0VScroll1.Value = 0VScroll1.Max = 100VScroll1.Max = 100VScroll1.Min = 0VScroll1.Min = 0Label1.Caption = HScroll1.ValueLabel1.Caption = HScroll1.Value

End SubEnd Sub

Private Sub HScroll1_Change()Private Sub HScroll1_Change()Label1 = ""Label1 = ""Label1 = HScroll1.ValueLabel1 = HScroll1.Value

End SubEnd Sub

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

End SubEnd Sub

27

How to include add ins How to include add ins and Librariesand Libraries

28

How to use the Timer?How to use the Timer?Private Sub Command1_Click()Private Sub Command1_Click()

Timer1.Enabled = TrueTimer1.Enabled = True

End SubEnd Sub

Private Sub Timer1_Timer()Private Sub Timer1_Timer()

Text1.Text = TimeText1.Text = Time

End SubEnd Sub

29

How to use Month How to use Month ViewerViewer

Private Sub Form_Load()MonthView1.BackColor =

vbGreenMonthView1.Appearance =

ccFlatMonthView1.ForeColor = vbRed

End Sub

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

End Sub

30

How to use Date PickerHow to use Date Picker

31

How to Create ManusHow to Create Manus

32

How to use ManusHow to use ManusPrivate Sub mnDisplay_Click()Private Sub mnDisplay_Click()

MsgBox "Hello, You have Clicked MsgBox "Hello, You have Clicked Display"Display"

End SubEnd Sub

33

How to use Common How to use Common Dialog ControlDialog Control

Private Sub mnDisplay_Click()Private Sub mnDisplay_Click()On Error GoTo err:On Error GoTo err:CommonDialog1.Action = 3 CommonDialog1.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

34

How to Use MS-Flex GridHow to Use MS-Flex GridPrivate Sub Command1_Click()Private Sub Command1_Click()

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

End SubEnd Sub

Private Sub Form_Load()Private Sub Form_Load()MSFlexGrid1.TextMatrix(9, 2) = 0MSFlexGrid1.TextMatrix(9, 2) = 0MSFlexGrid1.TextMatrix(0, 0) = "S. No"MSFlexGrid1.TextMatrix(0, 0) = "S. No"MSFlexGrid1.TextMatrix(0, 1) = "Name"MSFlexGrid1.TextMatrix(0, 1) = "Name"MSFlexGrid1.TextMatrix(0, 2) = "Age"MSFlexGrid1.TextMatrix(0, 2) = "Age"MSFlexGrid1.TextMatrix(1, 0) = "1"MSFlexGrid1.TextMatrix(1, 0) = "1"MSFlexGrid1.TextMatrix(1, 1) = "Dwipen"MSFlexGrid1.TextMatrix(1, 1) = "Dwipen"MSFlexGrid1.TextMatrix(1, 2) = 29MSFlexGrid1.TextMatrix(1, 2) = 29MSFlexGrid1.TextMatrix(2, 0) = "2"MSFlexGrid1.TextMatrix(2, 0) = "2"MSFlexGrid1.TextMatrix(2, 1) = "S Adarsh"MSFlexGrid1.TextMatrix(2, 1) = "S Adarsh"MSFlexGrid1.TextMatrix(2, 2) = 27MSFlexGrid1.TextMatrix(2, 2) = 27MSFlexGrid1.TextMatrix(3, 0) = "3"MSFlexGrid1.TextMatrix(3, 0) = "3"MSFlexGrid1.TextMatrix(3, 1) = "Sachin"MSFlexGrid1.TextMatrix(3, 1) = "Sachin"MSFlexGrid1.TextMatrix(3, 2) = 56MSFlexGrid1.TextMatrix(3, 2) = 56MSFlexGrid1.TextMatrix(9, 0) = "Total"MSFlexGrid1.TextMatrix(9, 0) = "Total"MSFlexGrid1.TextMatrix(9, 1) = "Age"MSFlexGrid1.TextMatrix(9, 1) = "Age"

End SubEnd Sub