Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 3rd, 2006, 10:42 AM
Registered User
 
Join Date: Dec 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to georginho
Default Inserting text file lines into an array?

Hello I am a student at University and need to create a program in VB 2005 that writes text from two text boxes into two seperate text files. One storing names and one Phone Numbers entered. The names are then read from the names file into a listbox. When the name is selected in the listbox, the phone number the user entered is to be displayed in a label underneath.
I have to use an Array to store the numbers

What I want to know is how do you store these phone numbers into the array so they can be displayed in the label?

Thanks in advance :)

 
Old December 4th, 2006, 12:12 PM
Registered User
 
Join Date: Dec 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to georginho
Default

Some help guys, cheers

 
Old December 8th, 2006, 08:47 AM
Authorized User
 
Join Date: Nov 2006
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ef1196
Default

Here is your example.
Since you are a student am I doing the homework? (Kidding...)

    Private Sub subSaveToFile(ByVal strName As String, ByVal strPhone As String)
        '* Pass your the data from your textboxes as parameters.

        '* Create StreamWriter objects that will open and write to text files.
        '* One for each file...
        Dim streamWriterNames As System.IO.StreamWriter = System.IO.File.AppendText("C:\Names.txt")
        Dim streamWriterPhones As System.IO.StreamWriter = System.IO.File.AppendText("C:\Phones.txt")

        '* Write the Name to a text file and then close.
        streamWriterNames.WriteLine(strName)
        streamWriterNames.Close()

        '* Write the Phone Number to a text file and then close.
        streamWriterPhones.WriteLine(strPhone)
        streamWriterPhones.Close()

    End Sub

    Private Sub subReadFiles()

        '* This will read from each text file and populate
        '* the listboxes.

        '* Here are your Arrays
        Dim arrNames As New ArrayList
        Dim arrPhones As New ArrayList

        '* Create StreamReader objects that will open and read from your text files.
        '* One for each file...
        Dim streamReaderNames As System.IO.StreamReader = System.IO.File.OpenText("C:\Names.txt")
        Dim streamReaderPhones As System.IO.StreamReader = System.IO.File.OpenText("C:\Phones.txt")

        '* Your list boxes for this example are:
        '* lstName and lstPhone

        '* Clear the contents of the lstName listbox
        lstName.Items.Clear()

        '* Read the Names.txt file
        With streamReaderNames
            While Not .EndOfStream
                arrNames.Add(.ReadLine)
            End While
            '* Close the file.
            .Close()
        End With

        '* Add the contents of the Name array to the name listbox
        For intX As Integer = 0 To arrNames.Count - 1
            lstName.Items.Add(arrNames.Item(intX))
        Next

        '* Highlight the first name
        If lstName.Items.Count > 0 Then lstName.SelectedIndex = 0

        '* Clear the contents of the lstPhone listbox
        lstPhone.Items.Clear()

        '* Read the Phones.txt file
        With streamReaderPhones
            While Not .EndOfStream
                arrPhones.Add(.ReadLine)
            End While
            '* Close the file.
            .Close()
        End With

        '* Add the contents of the Phone array to the Phone listbox
        For intX As Integer = 0 To arrPhones.Count - 1
            lstPhone.Items.Add(arrPhones.Item(intX))
        Next

        '* Highlight the first phone number
        If lstPhone.Items.Count > 0 Then lstPhone.SelectedIndex = 0

    End Sub




Best Regards,
Earl Francis
 
Old December 8th, 2006, 07:08 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

In as much ss you are a student, a tip: Separate, not Seperate...
 
Old December 9th, 2006, 11:23 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

While the example above might do something, you might want to consider using an actual array rather than an array list if the assignment is asking for you to use an array.

Also, you will want to declare that array at the class level, rather than as a local varaiable inside the sub, since you have to be able to access it whenever a user selects an item in the person list to display the phone number in a label.

The main concern of placing the values from the phone number file into the array is that your array is of a sufficient size to hold the list Exactly how you extract the phone numbers from the file will depend on how you put them into the file. Obviously the ordinal position of the phones has to be the same as the ordinal position of the people in the other file so that you have some way to correlate the items. There are better ways to do that - but I am just pointing out that you need to consider this. One way is to store the data as a delimited string. This could be read into your project and then use the split method of the string type. This would work with relatively small lists. For example:

Data stored in phone file:
123-1234;321-3211;434-2342;543-6543;987-5433

After reading file into a string named strPhoneNumbers you get the array thus:

dim arrPhoneNumbers() as string
arrPhoneNumbers() = strPhoneNumbers.Split(';')

Make sense?

One note: This type of assignment is usually asking you to exercise something you have just learned, like looping through the lines in a file to extract the values into an array - in which case my suggestion is incorrect, as your teacher will see you didn't learn the right thing and start yelling at you.

So... if that is your situation, you can store the phone numbers as lines in the text file and (as in ef's example) loop though those one line at a time and add them to an array one at a time. The main concern will be making sure you have an array of sufficient size to hold all the phone numbers. One way to do this is to start with a very large array - which is probably not what you are being taught. Another is to resize the array occasionally as you add items - start with an initial array size of 100, and then after each 100 items resize the array by increasing its size by 100. You do this using the Redim Preserve keyword. Of course, ArrayLists eliminate this problem, but again - if your assignment is learning about arrays, you should learn about arrays.

Also - Note to Brian: Please check my spelling, just in case. Thanks!



Woody Z
http://www.learntoprogramnow.com
 
Old December 12th, 2006, 08:36 AM
Authorized User
 
Join Date: Nov 2006
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ef1196
Default

George,

This topic is a great example of the usefulness of a forum. Different ways to approach the same topic lead to increased personal skill levels!



Best Regards,
Earl Francis
 
Old December 15th, 2006, 02:37 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Woody,

I would, but you are not as likely to have a teacher take points off in grading. Students can use all the help they can get to ensure they are graded for the class' topic, rather than "ambient noise" they might inadvertently inject.

But since you mentioned it...

In "The main concern of placing the values from the phone number file . . ." "phone number is an adjective modifying "file." Should be "phone-number file." (Plus you left off the period at the end of that sentence...)
"This could be read into your project and then use the split method of the string type." changes from past tense to present tense in mid-sentence.
"This could be read into your project and then the split method of the string type could be used."
or
"This could be read into your project. You could then use the split method of the string type."

" . . . then after each 100 items resize the array by increasing its size by 100." implies that resizing by 100 is the [u]method</u> of increasing by the array's size.

In "You do this using the Redim Preserve keyword." "keyword" should be "keyword[u]s</u>"

I mean, since you asked and all... ;)
 
Old December 15th, 2006, 03:26 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by BrianWren
 Woody,

I would, but you are not as likely to have a teacher take points off in grading. Students can use all the help they can get to ensure they are graded for the class' topic, rather than "ambient noise" they might inadvertently inject.

But since you mentioned it...

In "The main concern of placing the values from the phone number file . . ." "phone number is an adjective modifying "file." Should be "phone-number file." (Plus you left off the period at the end of that sentence...)
"This could be read into your project and then use the split method of the string type." changes from past tense to present tense in mid-sentence.
"This could be read into your project and then the split method of the string type could be used."
or
"This could be read into your project. You could then use the split method of the string type."

" . . . then after each 100 items resize the array by increasing its size by 100." implies that resizing by 100 is the [u]method</u> of increasing by the array's size.

In "You do this using the Redim Preserve keyword." "keyword" should be "keyword[u]s</u>"

I mean, since you asked and all... ;)
Hey - I thought I said check my SPELLING, not ALL my bone-headed mistakes.

Woody Z
http://www.learntoprogramnow.com
 
Old January 15th, 2007, 09:44 AM
Registered User
 
Join Date: Dec 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to georginho
Default

This is the exercise:

Exercise 4
Create a program in which the user inputs a person’s name and phone number. These are then written to two files; one for names, the other for phone numbers. Read the names back into a ListBox and the phone numbers into an array. The rest of the program is similar to Tutorial 6, Exercise 3: When a name is selected in the ListBox, the corresponding phone number is displayed in a label.
[Program name: FilePhoneBook]

Extra Task: Could you achieve the same functionality using just one file? (Do not actually do this, but explain how you think it could be done.)



Now this is my code:
__________________________________________________ ____________________
Imports System.IO

Public Class Form1

    Dim PhoneNumbersArray(50) As Integer
__________________________________________________ ____________________
    Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click

        'stores the Name and Number entered in text boxes'
        Dim swrName As IO.StreamWriter
        Dim swrPhoneNum As IO.StreamWriter

        'opens, writes and closes the individual text files'
        swrName = File.AppendText("PhoneBook.txt")
        swrName.WriteLine(txtName.Text)
        swrName.Close()

        swrPhoneNum = File.AppendText("PhoneBookNumbers.txt")
        swrPhoneNum.WriteLine(txtNumber.Text)
        swrPhoneNum.Close()
    End Sub
__________________________________________________ ____________________
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click

        Dim sreReadName As IO.StreamReader
        sreReadName = File.OpenText("PhoneBook.txt")

        Do While sreReadName.Peek <> -1
            lstNames.Items.Add(sreReadName.ReadLine)
        Loop

        sreReadName.Close()
    End Sub
__________________________________________________ ____________________
    Private Sub lstNames_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstNames.SelectedIndexChanged

        lblPhoneNumber.Text = lstNames.SelectedIndex 'Displays Phone Number of Name Selected in Label'
    End Sub

__________________________________________________ ____________________
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close() 'Closes Program'
    End Sub
End Class


I know it is the read from part and the selectedindex part that is wrong but i am a relative amatuer to VB as you can see.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting an array into a database... cpk Classic ASP Databases 1 June 1st, 2006 10:15 AM
read url file as array or text stream w_g_day Excel VBA 0 May 19th, 2005 05:49 AM
Text File to Array chadlupkes Javascript How-To 4 September 19th, 2003 09:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.