Computer Programming I

Using Combo Boxes

Contacts Program

Situation: Ever wondered how programs keep up with names, addresses, phone numbers or other data that people may want to store? Most people never give this any thought because the data is always there and can easily be stored and retrieved; but how does all of this work? Develop a program that demonstrates how you can create your very own Contacts program to store, retrieve and update information using Visual Basic and Notepad.

Directions:

1)  First Design your layout for the program using the following example. Command button locations, colors, fonts, etc can be changed in any way the user wants for the initial design.

2)  Next we need to save your form and project to your student folder.

3)  Minimize the Visual Basic screen and open up notepad(Go to Start, Programs, Accessories, Notepad)

4)  Notepad is where we are going to pull our contacts and also store our contacts for the program. Our initial design needs to allow 5 contacts to be entered into Notepad. An example of how the first four names would look is below, please note that the comma is used at the end of each person’s information line. The only time the comma will not be included is after the last person is entered. You can include as much info as you would like on each line.

5)  Save your Notepad document as names.dat and save this in the same location as your visual basic file **Make sure that the project and the names.dat file are in the same folder otherwise problems will exist later on in your program design.

6)  Go back to your Visual Basic program and insert the following code.

Private Sub Command1_Click()

Combo1.AddItem (Text1.Text)

End Sub

Private Sub Command2_Click()

Combo1.RemoveItem Combo1.ListIndex

End Sub

Private Sub Command3_Click()

'number used for the open file where the names are stored

Dim intPersonFileNbr As Integer

'develops the path to find the file that has the names

Dim strBS As String

'Variable to hold the persons name

Dim strPersonName As String

'Develop the file path for the names

strBS = IIf(Right$(App.Path, 1) = "\", "", "\")

'Assigns a number for the open file

intPersonFileNbr = FreeFile

'Opens the path and prepares it for use

Open (App.Path & strBS & "names.DAT") For Input As #intPersonFileNbr

'Clears out the combo box of any other data

Combo1.Clear

'Tells your program to do the following until it reaches the end of data.

Do Until EOF(intPersonFileNbr)

'Using the file file the data and store it for use

Input #intPersonFileNbr, strPersonName

'Take the stored data and place it into the combo box

Combo1.AddItem strPersonName

'End of the Do Until Statement

Loop

'Tells the open file to close

Close #intPersonFileNbr

'Displays the message to the screen

MsgBox "Names have been loaded.", vbInformation, "Combo Box Demo"

End Sub

Private Sub Command4_Click()

'I is used to count the number of files to write

Dim i As Integer

'Open the path to the location where names are stored

Open App.Path & ".\names.dat" For Output As #1

'Continue saving items until you reach the end of your list

For i = 0 To Combo1.ListCount

'Put each item back into the file

Write #1, Combo1.List(i)

Next i

'close your file as no more files will be written to it

Close #1

End Sub

Private Sub Command5_Click()

Dim totalentry As Integer

totalentry = Combo1.ListCount

MsgBox "The total number of contacts entered are:" & totalentry

End Sub

7)  Save your program and then run the program to see how everything works. Here is the checklist rubric for the program.

  1. Y/N Can you program load the contacts you created within the Notepad document? (20 points)
  2. Y/N Can you add a new contact to the combo box list? (20 points)
  3. Y/N Can you remove a contact from the combo box list? (20 points)
  4. Y/N Can your program count how many contacts you have? This will also count the number of blank lines that you might have in your file.

(20 points)

  1. Y/N Can your program save the current contact list that you have within your combo box? (20 points)