Question How to display specifics lines from .txt document on form?

Marie-Andree

Member
Joined
Sep 9, 2015
Messages
8
Programming Experience
Beginner
Hello,
My name is Marie and I am new to VB.Net. I am trying to work on this school assignment and I am struggling a bit. I need to display in a combo box names coming from a .txt documents. For example the text document goes as follow:
Would it be possible to guide me in the right direction?


Dave
781-294-8544
723-295-8533
735-296-8511
450-297-8523
Dave@gmail.com
Marie
780-888-1234
123-123-1234
888-777-5555
444-222-8888
Marie@gmail.com

Once the names are displayed in the combo box, I need to select one to display the relates information in text boxes (phone, cell...)

Here is what I have so far:
Imports System.IO

Public Class BlackBookLookupForm

    Dim BlackBookStreamReader As StreamReader
    Dim lineCounter As Integer = 0
    Dim Line As String

    'Friend Structure peopleInfo
    '    Dim NameString As String
    '    Dim PhoneString As String
    '    Dim PagerString As String
    '    Dim CellString As String
    '    Dim VoiceMailString As String
    '    Dim EmailString As String
    'End Structure

    Dim xIndexInteger As Integer

    'Dim objPeopleInfo(1000) As peopleInfo
    'objPeopleInfo = BlackBookStreamReader

    'Private Sub DisplayPeopleInfo(ByVal people As peopleInfo)
    '    TextBoxPhone.Text = BlackBookStreamReader.ReadLine()
    '    TextBoxPager.Text = BlackBookStreamReader.ReadLine()
    '    TextBoxCell.Text = BlackBookStreamReader.ReadLine()
    '    TextBoxVoiceMail.Text = BlackBookStreamReader.ReadLine()
    '    TextBoxEmail.Text = BlackBookStreamReader.ReadLine()
    'End Sub

    Private Sub OpenToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Dim ResponseDialogResult As DialogResult

        With OpenFileDialog1
            .InitialDirectory = Directory.GetCurrentDirectory()
            .FileName = "*.txt"
            .Title = "Select File or Directory for file."
            ResponseDialogResult = .ShowDialog()
        End With

        If ResponseDialogResult <> DialogResult.Cancel Then
            BlackBookStreamReader = New StreamReader(OpenFileDialog1.FileName)
        End If

        Do Until BlackBookStreamReader.Peek = -1
            'Line = BlackBookStreamReader.ReadLine()
            ComboBoxChooseFile.Text = BlackBookStreamReader.ReadLine
            'ComboBoxChooseFile.Text = BlackBookStreamReader.ReadLine()

            'How to have everithing 
            'ComboBoxChooseFile.Items.Add(BlackBookStreamReader.ReadLine)

            lineCounter = lineCounter + 1
        Loop
    End Sub

    Private Sub DisplayPeopleInfo()

    End Sub

    Private Sub ComboBoxChooseFile_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBoxChooseFile.SelectedIndexChanged
        If BlackBookStreamReader.Peek <> -1 Then
            DisplayPeopleInfo()
        Else
            MessageBox.Show("No record to display")
        End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        If BlackBookStreamReader IsNot Nothing Then
            BlackBookStreamReader.Close()
        End If
        Me.Close()
    End Sub

End Class
 
Last edited by a moderator:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,561
Location
Norway
Programming Experience
10+
In sense of learning it would seem logical to read all the persons into a collection of PeopleInfo objects (by the way that should be a class, not a structure). Then you can bind the collection as data source to combobox and choose Name (and that should be a property, not field) as display member.

You can also take that a bit further and use a BindingSource which you bind data collection to, and that you bind all controls to. Then navigation of combobox will propagate all other fields to other bound textboxes.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,561
Location
Norway
Programming Experience
10+
A quick mockup so you can see the principle of binding in action, you need a Combobox and a TextBox in form for this:
    Friend Class PeopleInfo
        Public Property Name As String
        Public Property Phone As String
        Public Property Pager As String
        Public Property Cell As String
        Public Property VoiceMail As String
        Public Property Email As String
    End Class

        Dim l As New List(Of PeopleInfo)
        l.Add(New PeopleInfo With {.Name = "A", .Phone = "1"})
        l.Add(New PeopleInfo With {.Name = "B", .Phone = "2"})
        Dim bs As New BindingSource()
        bs.DataSource = l
        Me.PeopleComboBox.DataSource = bs
        Me.PeopleComboBox.DisplayMember = "Name"
        Me.PhoneTextBox.DataBindings.Add(New Binding("Text", bs, "Phone"))

If your .Net version really is 2.0 you can't declare properties so simple, and not initialize "New PeopleInfo With" like that, but I think it must a be a mistake in your forum profile. Would be weird if any school would use such old version today. .Net 2.0 and its corresponding VS 2005 is a decade old, and there's no reason to use either.
 
Top Bottom