lab programs gui

43

Click here to load reader

Upload: dvadone

Post on 07-Apr-2015

2.456 views

Category:

Documents


9 download

TRANSCRIPT

Page 1: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 1

Program No. 1 Date of Experiment……………….

Aim: Program to input two different integers using the function input box to

compare two numbers

Design the Form:

Procedure:

1. Open Visual Basic 6.0. On the File menu, choose New Project. 2. In the New Project dialog box, choose Standard EXE, and click OK . 3. Select the first Label, and set the Name property to lbldisplay1 and the Caption

property to Clear. 4. Select the Second Label, and set the Name property to lbldisplay2 and the Caption

property to Clear. 5. Select the Third Label, and set the Name property to lbldisplay3 and the Caption

property to Clear. 6. Select the first CommandButton, and set the Name property to CmdEnternumbers and

the Caption property to Enter Numbers. 7. Select the Second CommandButton, and set the Name property to CmdExit and the

Caption property to Exit. 8. Double-click the form to open the Code Editor, and enter the following code.

Option Explicit ' Force explicit declarations

Private Sub cmdEnterNumbers_Click()

Dim num1 As Integer, num2 As Integer

Page 2: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 2

' Clear Labels

lblDisplay1.Caption = ""

lblDisplay2.Caption = ""

lblDisplay3.Caption = ""

lblDisplay4.Caption = ""

' Get values from user

num1 = InputBox("Enter first integer", "Input")

num2 = InputBox("Enter second integer", "Input")

' Test the relationships between the numbers

If num1 = num2 Then

lblDisplay1.Caption = num1 & " is equal to " & num2

End If

If num1 <> num2 Then

lblDisplay1.Caption = num1 & " is not equal to " & num2

End If

If num1 > num2 Then

lblDisplay2.Caption = num1 & " is greater than " & num2

End If

If num1 < num2 Then

lblDisplay2.Caption = num1 & " is less than " & num2

End If

If num1 >= num2 Then

lblDisplay3.Caption = num1 & _

" is greater than or equal to " _

& num2

End If

If num1 <= num2 Then

lblDisplay4.Caption = num1 & _

" is less than or equal to " & num2

End If

End Sub

Private Sub cmdExit_Click()

End

End Sub

9. Press F5 to run the application or Click Run button form the tool bar. 10. First input dialog displedy for user input. Input a value before press ok 11. Second input dialog displayed for user input. Input a value before press ok 12. GUI after second input dialog box is closed as shown in figure 2 13. On the File menu, choose Save Project As. 14. In the Save File As dialog box, save the form as compare.frm and save the project as

comparetwonumbers.vbp.

Page 3: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 3

Result in GUI

Page 4: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 4

Program No. 2 Date of Experiment……………….

Aim: Develop a class average program that will process an arbitrary number of

grades, each time the program is run.

Procedure:

1. Open Visual Basic 6.0. On the File menu, choose New Project. 2. In the New Project dialog box, choose Standard EXE, and click OK . 3. Select the first Label, and set the Name property to lbldisplay1 and the Caption

property to Clear. 4. Select the first CommandButton, and set the Name property to CmdEnterData and the

Caption property to Enter Grades. 5. Select the Second CommandButton, and set the Name property to CmdExit and the

Caption property to Exit. 6. Double-click the form to open the Code Editor, and enter the following code.

' Class average program '

Option Explicit ' General declaration

Private Sub cmdEnterData_Click()

Dim total As Integer ' Sum of all grades input

Dim counter As Integer ' Number of grades input

Dim grade As Integer ' Current grade

Dim average As Single ' Floating-point average

Dim message As String ' Text displayed in Label

' Initialization phase

total = 0

counter = 0

Page 5: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 5

' Processing phase

grade = InputBox("Enter grade: -1 to end", "VBHTP")

' Loop until grade has a -1 value

Do Until grade = -1

total = total + grade ' Add grade to total

counter = counter + 1 ' Increment counter

' Input the next grade. When -1 is assigned,

' the loop continuation condition becomes True

grade = InputBox("Enter grade: -1 to end", "VBHTP")

Loop

' Termination phase

If counter <> 0 Then ' Prevent division by zero

average = total / counter ' Floating-point division

message = "Class average is "

' Format average and concatenate to message

message = message & average

lblAverage.Caption = message

Else ' counter is 0

lblAverage.Caption = "No grades were entered."

End If

End Sub

Private Sub cmdExit_Click()

End

End Sub

7. Press F5 to run the application or Click Run button form the tool bar. 8. Input dialog displayed for user input. Input a value before press ok 9. Again Input dialog displayed for user input. Input a value before press ok and to exit

from Input dialog box enter value -1. 10. GUI after enter -1 and press ok , input dialog box is closed as shown in figure 2 11. On the File menu, choose Save Project As. 12. In the Save File As dialog box, save the form as classavg.frm and save the project as

classaverage.vbp.

Page 6: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 6

Page 7: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 7

Program No. 3 Date of Experiment……………….

Aim: Program to find the minimum of three numbers using sub procedures

Procedure:

1. Open Visual Basic 6.0. On the File menu, choose New Project. 2. In the New Project dialog box, choose Standard EXE, and click OK . 3. Select the first Label, and set the Name property to lbldisplay1 and the Caption

property to Enter first value:. And Select next to label TextBox, and set the Name property to Txtone and the Text property to Clear.

4. Select the Second Label, and set the Name property to lbldisplay2 and the Caption property to Enter Second value:. And Select next to label TextBox, and set the Name property to Txttwo and the Text property to clear.

5. Select the Third Label, and set the Name property to lbldisplay3 and the Text property to Enter third value:. And Select next to label TextBox, and set the Name property to Txtthree and the Text property to clear.

6. Select the first CommandButton, and set the Name property to cmdsmallest and the Caption property to Enter Numbers.

7. Select the Second CommandButton, and set the Name property to CmdExit and the Caption property to Exit.

8. Double-click the form to open the Code Editor, and enter the following code.

' Program finds the minimum of three numbers input Option Explicit ' General declaration Private Sub cmdSmallest_Click()

Page 8: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 8

Dim value1 As Long, value2 As Long, value3 As Long value1 = txtOne.Text value2 = txtTwo.Text value3 = txtThree.Text Call Minimum(value1, value2, value3) End Sub Private Sub Minimum(min As Long, y As Long, z As Long) If y < min Then min = y End If If z < min Then min = z End If lblSmallest.Caption = "Smallest value is " & min End Sub Private Sub CmdExit_Click() End End Sub

9. Press F5 to run the application or Click Run button form the tool bar. 10. Enter a number in first textbox. 11. Enter a number in Second Textbox 12. Enter a number in Third Textbox and Click on command button Smallest and see the

output on GUI as shown in Figure. 13. On the File menu, choose Save Project As. 14. In the Save File As dialog box, save the form as smallest.frm and save the project as

smallestno.vbp.

RESULT IN GUI:

Page 9: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 9

Program No. 4 Date of Experiment……………….

Procedure:

1. Open Visual Basic 6.0. On the File menu, choose New Project. 2. In the New Project dialog box, choose Standard EXE, and click OK . 3. Select the first Label, and set the Name property to lbldisplay and the Caption

property to Enter a Number. 4. Select the first CommandButton, and set the Name property to cmdrun and the Caption

property to Rum 5. Select the first CommandButton, and set the Name property to cmdclear and the

Caption property to Clear 6. Select the Second CommandButton, and set the Name property to CmdExit and the

Caption property to Exit. 7. Double-click the form to open the Code Editor, and enter the following code.

Option Explicit Private Sub cmdClear_Click() Cls Lbldisplay.Caption = "Enter a number" End Sub Private Sub cmdExit_Click()

Page 10: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 10

End End Sub Private Sub cmdrun_Click() Dim intNum As Integer intNum = Val(InputBox("Enter a number:", "IsOdd Test")) Lbldisplay.Caption = "" If IsOdd(intNumIn) Then Print "The number that you entered is odd." Else Print "The number that you entered is not odd." End If End Sub Private Function IsOdd(pintNum As Integer) As Boolean If (Num Mod 2) = 0 Then IsOdd = False Else IsOdd = True End If End Function

8. Press F5 to run the application or Click Run button form the tool bar. 9. Click on command button Run. A InputBox dialogue appear on the screen to input a

number. 10. Enter a number and press Ok to accept the number. 11. See the result in GUI as shown in Figure. 12. On the File menu, choose Save Project As. 13. In the Save File As dialog box, save the form as funproc.frm and save the project as

oddnotest.vbp.

GUI Output:

Page 11: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 11

Program No.5 Date of

Experiment……………….

Aim: Program to demonstrate ctrl array

Procedure:

1. Open Visual Basic 6.0. On the File menu, choose New Project. 2. In the New Project dialog box, choose Standard EXE, and click OK . 3. Select the first CommandButton, and set the Name property to cmdTest and the

Caption property to First 4. Copy the first CommandButton, and paste it on the form; the Caption property to

Second 5. Copy the first CommandButton, and paste it on the form; the Caption property to

Third 6. Copy the first CommandButton, and paste it on the form; the Caption property to

Fourth 7. Double-click the form to open the Code Editor, and enter the following code.

Private Sub CmdTest_Click(Index As Integer) Print " You have Clicked Command Button " & CmdTest(Index).Caption End Sub

8. Press F5 to run the application or Click Run button from the tool bar. 9. See the result in GUI as shown in Figure. 10. On the File menu, choose Save Project As.

Page 12: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 12

11. In the Save File As dialog box, save the form as controlarray.frm and save the project as crltarray.vbp.

GUI Output:

Page 13: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 13

Program No.6 Date of

Experiment……………….

Aim: Program to demonstrate dynamic array Form Design

Procedure:

1. Open Visual Basic 6.0. On the File menu, choose New Project. 2. In the New Project dialog box, choose Standard EXE, and click OK . 3. Select the first Label, and set the Name property to lbldisplay1 and the Caption

property to DEMONSTRATION OF DYNAMIC ARRAY. 4. Select the ListBox and set the Name property to Listarray 5. Select the CommandButton, and set the Name property to Cmdrun and the Caption

property to RUN. 6. Select the CommandButton, and set the Name property to CmdErase and the Caption

property to ERASE 7. Select the CommandButton, and set the Name property to CmdExit and the Caption

property to EXIT 8. Double-click the form to open the Code Editor, and enter the following code.

Option Explicit ----------------------------------------------------------------------------------------------------- Dim myArray() As String Private Sub CmdErase_Click() Erase myArray

Page 14: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 14

Call Listarray.Clear CmdErase.Enabled = False End Sub ------------------------------------------------------------------------------------------------------ Private Sub Cmdrun_Click() Dim values As Integer Dim range As Integer Dim x As Integer values = InputBox("How many elements?") range = values - 1 ReDim myArray(range) For x = 0 To range myArray(x) = InputBox("Enter Element") Listarray.AddItem myArray(x) Next x CmdErase.Enabled = True End Sub -------------------------------------------------------------------------------------------------------- Private Sub Exit_Click() End End Sub -------------------------------------------------------------------------------------------------------

12. Press F5 to run the application or Click Run button from the tool bar. 13. See the result in GUI as shown in Figure. 14. On the File menu, choose Save Project As. In the Save File As dialog box, save the form

as with suitable name.

GUI Output:

Page 15: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 15

Program No.7 Date of

Experiment……………….

Aim: Program to demonstrate param array Form Design:

Procedure:

1. Open Visual Basic 6.0. On the File menu, choose New Project. 2. In the New Project dialog box, choose Standard EXE, and click OK .. 3. Select the first CommandButton, and set the Name property to Cmddisp and the

Caption property to Display. 4. Double-click the form to open the Code Editor, and enter the following code.

Option Explicit --------------------------------------------------------------------------------------------- Private Sub Cmddisp_Click() Call parray Call parray(1) Call parray(2, 3) Call parray(4, 5, 6) Call parray(7, 8, 9, 10, 11, 12) End Sub ------------------------------------------------------------------------------------------------ Private Sub parray(ParamArray x() As Variant) Dim y As Integer

Page 16: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 16

Print "Procedure AnyNumberArguments received " For y = LBound(x) To UBound(x) Print x(y) & Space$(4); Next y Print End Sub ----------------------------------------------------------------------------------------------------

5. Press F5 to run the application or Click Run button from the tool bar. 6. See the result in GUI as shown in Figure. 7. On the File menu, choose Save Project As. In the Save File As dialog box, save the form

as with suitable name.

GUI Output:

Page 17: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 17

Program No.8 Date of

Experiment……………….

Aim: Program to demonstrate function array Design Form:

Procedure:

1. Open Visual Basic 6.0. On the File menu, choose New Project. 2. In the New Project dialog box, choose Standard EXE, and click OK .. 3. Select the first CommandButton, and set the Name property to Cmddisp and the

Caption property to Display. 4. Double-click the form to open the Code Editor, and enter the following code.

Option Explicit --------------------------------------------------------------------------------------------- Private Sub cmdPrint_Click() Dim v As Variant, x As Integer v = Array(7, 5, 6, 9, 3, 0) Print "Variant array values are: "; For x = LBound(v) To UBound(v) Print v(x) & Space$(2); Next x

Page 18: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 18

Print v = Array("hello", "bye", "hi") Print "Variant array values are: "; For x = LBound(v) To UBound(v) Print v(x) & Space$(2); Next x Print v = Array(1.1, 2.2, 3.3, 4.4) Print "Variant array values are: "; For x = LBound(v) To UBound(v) Print v(x) & Space$(2); Next x cmdPrint.Enabled = False End Sub --------------------------------------------------------------------------------------------------------

5. Press F5 to run the application or Click Run button from the tool bar. 6. See the result in GUI as shown in Figure. 7. On the File menu, choose Save Project As. In the Save File As dialog box, save the form

as with suitable name.

GUI Output:

Page 19: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 19

Program No.9 Date of

Experiment……………….

Aim: Program to demonstrate string functions

Design Form

Procedure:

1. Open Visual Basic 6.0. On the File menu, choose New Project. 2. In the New Project dialog box, choose Standard EXE, and click OK . 3. Select the first CommandButton, and set the Name property to Cmddisp and the

Caption property to Display. 4. Double-click the form to open the Code Editor, and enter the following code.

Option Explicit ------------------------------------------------------ Dim stringl As String, string2 As String Dim answer As String Dim result As Integer Private Sub Cmddisp_Click() stringl = "It was the best of times," string2 = "best of times" result = Len(string2)

Page 20: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 20

Print result Print answer = Left$(stringl, 6) Print answer; Print answer = Right$(stringl, 6) Print answer; Print answer = Mid$(stringl, 8, 8) Print answer; Print result = InStr(stringl, string2) Print result Print answer = UCase$(string2) Print answer Print End Sub ----------------------------------------------------------------------------------------------------

8. Press F5 to run the application or Click Run button from the tool bar. 9. See the result in GUI as shown in Figure. 10. On the File menu, choose Save Project As. In the Save File As dialog box, save the form

as with suitable name.

GUI Output:

Page 21: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 21

Program No.10 Date of Experiment……………….

Aim: Design an application to validate the user name & password & display

appropriate msg using msg box ctrl

Design Form:

Procedure:

1. Open Visual Basic 6.0. On the File menu, choose New Project. 2. In the New Project dialog box, choose Standard EXE, and click OK . 3. Select TextBox and set the Name Property to Text1 4. Select TextBox and set the Name Property to Text2 and set PasswordChar to * 5. Select the first CommandButton, and set the Name property to Cmdlogin and the

Caption property to Login 6. Select the Second CommandButton, and set the Name property to Cmdcancel and the

Caption property to Cancel. 7. Double-click the form to open the Code Editor, and enter the following code.

Option Explicit -------------------------------------------------------------------------------------------------------------- Dim a, b As Integer Private Sub Cmdcancel_Click() End End Sub --------------------------------------------------------------------------------------------------------------

Page 22: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 22

Private Sub Cmdlogin_Click() If Text1.Text = "AAA" And Text2.Text = "BBB" Then a = MsgBox("sucessful Login", vbDefaultButton1) Else b = MsgBox("Unsucessful Login", vbDefaultButton1) End If End Sub

8. Press F5 to run the application or Click Run button from the tool bar. 9. See the result in GUI as shown in Figure.

On the File menu, choose Save Project As. In the Save File As dialog box, save the form as with suitable name GUI Output:

Page 23: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 23

Program No.11 Date of Experiment……………….

Aim: Program to demonstrate date and time functions DEGIGN FORM:

Procedure:

1. Open Visual Basic 6.0. On the File menu, choose New Project. 2. In the New Project dialog box, choose Standard EXE, and click OK . 3. Select the first Label, and set the Name property to lbldisplay1 and the Caption

property to Clear. 4. Select the first CommandButton, and set the Name property to CmdEnterData and the

Caption property to Enter Grades. 5. Select the Second CommandButton, and set the Name property to CmdExit and the

Caption property to Exit. 6. Select the Second CommandButton, and set the Name property to CmdExit and the

Caption property to Exit 7. Double-click the form to open the Code Editor, and enter the following code.

Option Explicit Private Sub Form_Load() Call lstOutput.AddItem("Current date and time: " & Now)

Page 24: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 24

Call lstOutput.AddItem("Date: " & Date) Call lstOutput.AddItem("Day: " & Day(Date)) Call lstOutput.AddItem("Weekday: " & Weekday(Date)) Call lstOutput.AddItem("WeekdayName: " & WeekdayName(Weekday(Date))) Call lstOutput.AddItem("Month: " & Month(Date)) Call lstOutput.AddItem("MonthName: " & MonthName(Month(Date))) Call lstOutput.AddItem("Year: " & Year(Date)) End Sub

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

8. Press F5 to run the application or Click Run button from the tool bar. 9. See the result in GUI as shown in Figure. 10. On the File menu, choose Save Project As. In the Save File As dialog box, save the form

as with suitable name

GUI Output:

Page 25: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 25

Program No.12 Date of Experiment……………….

Aim: Program to demonstrate adding and subtracting dates with date add and date diff Form Design:

Procedure:

1. Open Visual Basic 6.0. On the File menu, choose New Project. 2. In the New Project dialog box, choose Standard EXE, and click OK . 3. Select the Listbox, and set the Name property to Lstoutput 4. Double-click the form to open the Code Editor, and enter the following code.

Option Explicit ---------------------------------------------------------------------------------------------------------------- Private Sub Form_Load() Call lstOutput.AddItem("Now: " & Now) Call lstOutput.AddItem("Now + 3 years: " & DateAdd("yyyy", 3, Now)) Call lstOutput.AddItem("Days between 1/1/98 and " & "12/31/98: " & DateDiff("d", "1/1/98", "12/31/98")) End Sub

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

5. Press F5 to run the application or Click Run button from the tool bar. 6. See the result in GUI as shown in Figure. 7. On the File menu, choose Save Project As. In the Save File As dialog box, save the form

as with suitable name

GUI Output:

Page 26: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 26

Program No.13 Date of Experiment……………….

Aim: Program to demonstrate function filter & split

Procedure:

1. Open Visual Basic 6.0. On the File menu, choose New Project. 2. In the New Project dialog box, choose Standard EXE, and click OK . 3. Select the first CommandButton, and set the Name property to Cmdfilter and the

Caption property to Enter Grades. 4. Select the Second CommandButton, and set the Name property to Cmdsplit and the

Caption property to Exit. 5. Double-click the form to open the Code Editor, and enter the following code.

Option Explicit ----------------------------------------------------------------------------------------------------------- Private Sub Cmdfilter_Click() Dim a(9) As String, b() As String a(0) = "Java" a(1) = "Visual Basic" a(2) = "C" a(3) = "C++" a(4) = "Visual Basic" a(5) = "HTML" a(6) = "Visual Basic" a(7) = "Visual C++ " a(8) = "Visual J++ " a(9) = "Visual Basic" Call Cls Print "Filtering for the string: " & "Visual Basic 6"

Page 27: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 27

b = Filter(a, "Visual Basic") Call PrintStrings(b) Print Erase b Print "Filtering for strings other than: " & "Visual Basic 6" b = Filter(a, "Visual Basic", False) Call PrintStrings(b) End Sub

--------------------------------------------------------------------------------------------------------------------- Private Sub PrintStrings(c() As String) Dim z As Integer For z = 0 To UBound(c) Print c(z) Next z End Sub ------------------------------------------------------------------------------------------------------------- Private Sub Cmdsplit_Click() Dim x() As String, y As String Dim z As Integer Call Cls y = "This is a sentence with 7 words" x = Split(y) For z = LBound(x) To UBound(x) Print x(z) Next z End Sub

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

6. Press F5 to run the application or Click Run button from the tool bar. 7. See the result in GUI as shown in Figure. 8. On the File menu, choose Save Project As. In the Save File As dialog box, save the form

as with suitable name

Page 28: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 28

GUI Output:

Page 29: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 29

Program No.14 Date of Experiment……………….

Aim: Program to demonstrate timer ctrl

Procedure:

1. Open Visual Basic 6.0. On the File menu, choose New Project. 2. In the New Project dialog box, choose Standard EXE, and click OK . 3. Select the first CommandButton, and set the Name property to CmdStart and the

Caption property to Start Timer.. 4. Select the Second CommandButton, and set the Name property to CmdStop and the

Caption property Stop Timer. 5. Select the TimerButton and set the Name property to tmrTest and set the Interval

property to 250 6. Double-click the form to open the Code Editor, and enter the following code.

Option Explicit ------------------------------------------------------------------------------------------------- Private mintCount As Integer Private Sub cmdStart_Click() mintCount = 0 Cls tmrTest.Enabled = True End Sub -------------------------------------------------------------------------------------------------

Page 30: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 30

Private Sub cmdStop_Click() tmrTest.Enabled = False End Sub --------------------------------------------------------------------------------------------------- Private Sub tmrTest_Timer() mintCount = mintCount + 1 Print "Timer fired again. Count = " & mintCount End Sub

7. Press F5 to run the application or Click Run button from the tool bar. 8. See the result in GUI as shown in Figure. 9. On the File menu, choose Save Project As. In the Save File As dialog box, save the form

as with suitable name

GUI Output:

Page 31: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 31

Program No.15 Date of Experiment……………….

Aim: Program to demonstrate combo box ctrl GUI Design:

Procedure:

1. Open Visual Basic 6.0. On the File menu, choose New Project. 2. In the New Project dialog box, choose Standard EXE, and click OK . 3. Select the ComboBox , and set the Name property to Combo1 4. Select the CommandButton, and set the Name property to cmdAdd and the Caption

property to ADD 5. Select theCommandButton and set the Name property to cmdClear and the Caption

Property to Clear 6. Select the ListBox and set the Name property to List1 7. Double-click the form to open the Code Editor, and enter the following code.

Private Sub Cmdadd_Click() List1.AddItem (Combo1.Text) End Sub Private Sub Cmdclear_Click() List1.Clear End Sub Private Sub Form_Load() Combo1.AddItem "Monday"

Page 32: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 32

Combo1.AddItem "Tuesday" Combo1.AddItem "Wednesday" Combo1.AddItem "Thursday" Combo1.AddItem "Friday" Combo1.AddItem "Saturday" Combo1.AddItem "Sunday" End Sub

8. Press F5 to run the application or Click Run button from the tool bar. 9. See the result in GUI as shown in Figure. 10. On the File menu, choose Save Project As. In the Save File As dialog box, save the form

as with suitable name

GUI Output:

Page 33: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 33

Program No.16 Date of Experiment……………….

Aim: Program to demonstrate pop-up menu GUI Design: Procedure 1. Start a new VB project and invoke the Menu Editor by select the Menu Editor option from the Tools

menu. The Menu Editor screen appears, as shown in figure:

2. For "Caption", type &File For "Name", type mnuFile.

3. Click the "right-arrow" button A ellipsis (...) will appear as the next item in the menu list, indicating that this item is a level-two item (below "File").

4. For "Caption", type &New; for "Name", type mnuNew, and for "Shortcut", select Ctrl+N. By

specifying a shortcut

5. For "Caption", type &Open; for "Name", type mnuOpen, and for "Shortcut", select Ctrl+O.

6. For "Caption", type - (a hyphen), and for "Name", type mnuFileBar1.

7. For "Caption", type Save &As ..., and for "Name", type mnuSaveAs.

Page 34: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 34

8. For "Caption", type &Print;for "Name", type mnuPrint; and for "Shortcut", select Ctrl+P.

9. For "Caption", type -; and for "Name", type mnuFileBar3.

10. For "Caption", type E&xit, and for "Name", type mnuExit.

11. Click the "left-arrow" button (The ellipsis (...) no longer appears, meaning we are back to the top-

level items.

12. For "Caption", type &Help; and for "Name", type mnuHelp.

13. Click the "right-arrow" button to create a level-two item below "Help". For "Caption", type &About;

and for "Name", type mnuAbout.

14. At this point, we are done creating our menu entries, so click the OK button. That will dismiss the

menu editor and return focus to the VB IDE.

15. Back in the VB IDE, your form will now have a menu, based on what we have set up in the Menu

Editor. If we click on a top-level menu item

16. Double-click the form to open the Code Editor, and enter the following code.

Option Explicit ------------------------------------------------------------------------------------------------- Private Sub mnuFileNew_Click() MsgBox "Code for 'New' goes here." End Sub -------------------------------------------------------------------------------------------------- Private Sub mnuFileOpen_Click() MsgBox "Code for 'Open' goes here." End Sub -------------------------------------------------------------------------------------------------- Private Sub mnuFileSave_Click() MsgBox "Code for 'Save' goes here." End Sub

Page 35: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 35

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

Private Sub mnuFileSaveAs_Click() MsgBox "Code for 'Save As' goes here." End Sub ------------------------------------------------------------------------------------------------- Private Sub mnuFilePrint_Click() MsgBox "Code for 'Print' goes here." End Sub -------------------------------------------------------------------------------------------------- Private Sub mnuFileExit_Click() Unload Me End Sub -------------------------------------------------------------------------------------------------- Private Sub mnuHelpAbout_Click() MsgBox "Menu Demo" End Sub --------------------------------------------------------------------------------------------------

Page 36: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 36

Program No.17 Date of Experiment……………….

Aim: Program to demonstrate Pull down menu Procedure: 1. Start a new VB project and place a label on the form. Name the label lblTestText. Set the Caption to

Test Text

2. Open the Menu Editor, and create a top-level item with a Caption value of PopUpFormat and the Name mnuPopuUpFormat. Also importantly uncheck the Visible checkbox (see the circled item in figure). In order for a menu to be a pop-up menu, it must be invisible.

Page 37: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 37

3. Create the following level-two menu items below the PopUpFormat top-level menu. (When creating these level-two items, keep the Visible box checked.)

4.

Caption Name Bold mnuBold Italic mnuItalic Underline mnuUnderline - (hyphen) mnuFormatSep Cancel mnuCancel

5. Click OK to save your changes. Note: When you return to the IDE, we will NOT see this menu on the form (remember it's a pop-up menu, and it will only be visible when invoked through code).

6. Code the lblTestText_MouseDown event as shown below. Note that the Button parameter is tested for vbRightButton as is conventional, we only want to pop up the menu if the user right-clicks on the label. If the user clicks the right mouse button, the PopupMenu statement is executed. It is this statement that makes the pop-up menu app

Option Explicit

---------------------------------------------------------------------------------------------------------------- Private Sub lblTestText_MouseDown(Button As Integer, _ Shift As Integer, _ X As Single, _ Y As Single) If Button = vbRightButton Then PopupMenu mnuPopUpFormat, vbPopupMenuRightButton End If End Sub -------------------------------------------------------------------------------------------------------------- Private Sub mnuBold_Click() If mnuBold.Checked Then lblTestText.FontBold = False mnuBold.Checked = False Else lblTestText.FontBold = True mnuBold.Checked = True End If

Page 38: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 38

End Sub --------------------------------------------------------------------------------------------------------------- Private Sub mnuItalic_Click() If mnuItalic.Checked Then lblTestText.FontItalic = False mnuItalic.Checked = False Else lblTestText.FontItalic = True mnuItalic.Checked = True End If End Sub --------------------------------------------------------------------------------------------------------------- Private Sub mnuUnderline_Click() If mnuUnderline.Checked Then lblTestText.FontUnderline = False mnuUnderline.Checked = False Else lblTestText.FontUnderline = True mnuUnderline.Checked = True End If End Sub ----------------------------------------------------------------------------------------------------------------

GUI Output:

Page 39: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 39

Program No.18 Date of Experiment……………….

Aim: Program to demonstrate linear search GUI Design:

Procedure:

1. Open Visual Basic 6.0. On the File menu, choose New Project. 2. In the New Project dialog box, choose Standard EXE, and click OK . 3. Select the Label and set the Caption to Enter numbers of Elements. 4. Select the Textbox and set the Name Property to Text1. 5. Select the Commandbutton and set the Name Property to CmdEnter and set the

Caption Property to Accept Numbers. 6. Select the Commandbutton and set the Name Property to Cmdsearch and set the

Caption Property to Search. 7. Select the Commandbutton and set the Name Property to Cmdexit and set the

Caption Property to Exit. 8. Double-click the form to open the Code Editor, and enter the following code.

Dim A(10) As Integer Dim N As Integer ----------------------------------------------------------------------------------------------- Private Sub cmdenter_Click() N = Val(Text1.Text) For I = 0 To N - 1 B = InputBox(" Enter the Numbers:") A(I) = B Next I End Sub ------------------------------------------------------------------------------------------------ Private Sub Cmdsearch_Click()

Page 40: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 40

Dim FLAG As Integer FLAG = 0 Key = InputBox("Enter the number to search in a List:") For I = 0 To N - 1 If A(I) = Key Then B = MsgBox("Search Sucessful ……..and elements is found at position" & I + 1) FLAG = 1 End If Next I If FLAG = 0 Then B = MsgBox("Search Sucessful ……..") End If End Sub ------------------------------------------------------------------------------------------------------- Private Sub Command3_Click() End End Sub

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

9. Press F5 to run the application or Click Run button from the tool bar. 10. See the result in GUI as shown in Figure. 11. On the File menu, choose Save Project As. In the Save File As dialog box, save the form

as with suitable name

Page 41: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 41

Program No.19 Date of Experiment……………….

Aim: Design an application which performs the following operations on the database

the

form using ADO a) add b) update c) delete

Procedure:

Creating an database using MS Acces

1. Start – Program – Ms office – click Ms Access. 2. create a table as shown in figure 3. Save the table as table1. and save the database as db1.mdb

Creating an ADO Data Control

1. Add the Microsoft ADO DataControl 6.0 (OLEDB) from the Project, Components menu dialog box, as in Figure 8.13. The ADO Data Control icon should now appear in the VB toolbox.

2. Place an instance of the ADO Data Control on the form

3. Change the control's Name and Caption from their default values. Set the ConnectionString property using steps 5–9.

4. Click the ellipsis next to the ConnectionString property in the ADO Data Control's Properties window to bring up the Property Page dialog box for this property ConnectionString property.

5. As Source of Connection, choose one of the following three options:

Page 42: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 42

• Use Data Link File. If you choose this option, you will be able to click the Browse On the Provider tab of the Data Link Properties tabbed dialog box, choose an OLE DB data provider, such as Microsoft Jet 4.0 OLE DB

6. Click OK to accept the ConnectionString options you have built. 7. Still in the ADO Data Control's Properties window, navigate to the RecordSource

property and click the ellipsis button. 8. On the RecordSource tab (see Figure 8.18) of the resulting Property Page dialog box,

choose the adCmdTable. • If you chose adCmdTable fill in the appropriate table or stored procedure name in the Table or Stored Procedure Name drop-down list.

9. Click OK to end the RecordSource dialog box. 10. 10 Place the textbox in form set the name property to text1, set the datasource to adodc1

and set the datafield to slno. 11. Place the textbox in form set the name property to text2, set the datasource to adodc1 and

set the datafield to name 12. Place the textbox in form set the name property to text3, set the datasource to adodc1 and

set the datafield to phoneno. 13. Place the command button, set the name property to cmdadd, and set the Caption

property to ADD. 14. Place the command button, set the name property to cmdupdate, and set the Caption

property to UPDATE. 15. Place the command button, set the name property to cmdelete, and set the Caption

property to Delete.

Private Sub Cmdadd_Click() Adodc1.Recordset.AddNew End Sub -------------------------------------------------------------------------- Private Sub Command1_Click() Adodc1.Recordset(0) = Val(Text1.Text) Adodc1.Recordset(1) = Text2.Text Adodc1.Recordset(2) = Val(Text3.Text) Adodc1.Recordset.Update End Sub ------------------------------------------------------------------------- Private Sub Command2_Click() Adodc1.Recordset.Delete End Sub

16. Press F5 to run the application or Click Run button from the tool bar. 17. See the result in GUI as shown in Figure.

Page 43: Lab Programs GUI

Acharya Polytechnic, Department of Computer Science

G.U.I LAB MANUAL

Page 43

18. On the File menu, choose Save Project As. In the Save File As dialog box, save the form as with suitable name