title: VB Populating ComboBox with specific field from comma-delimited txt file
body: <p>Need to populate NameComboBox from a comma-delimited txt file. Want user to be able to select just the name from NameComboBox dropdown and the rest of the textboxes to fill in. </p>
  
  <p>Right now the entire record is populating the ComboBox.</p>
  
  <pre><code>Imports System.IO
  
  Public Class LookupForm
  
  Private Sub LookupForm_Load(sender As Object, e As System.EventArgs) Handles Me.Load
      ' Load the items into the NameComboBox list.
      Dim ResponseDialogResult As DialogResult
      Dim NameString As String
  
      Try
          ' Open the file.
          Dim ContactInfoStreamReader As StreamReader = New StreamReader("TextFile.txt")
          ' Read all the elements into the list.
          Do Until ContactInfoStreamReader.Peek = -1
              NameString = ContactInfoStreamReader.ReadLine()
              NameComboBox.Items.Add(NameString)
          Loop
          ' Close the file.
          ContactInfoStreamReader.Close()
      Catch ex As Exception
          ' File missing.
          ResponseDialogResult = MessageBox.Show("Create a new file?", "File Not Found",
              MessageBoxButtons.YesNo, MessageBoxIcon.Question)
          If ResponseDialogResult = Windows.Forms.DialogResult.No Then
              ' Exit the program.
              Me.Close()
          End If
      End Try
  End Sub
  </code></pre>
  
  <p>****Update 11/2/15: 
  The names are appearing in the NamesComboBox as they should but when one is selected the info doesn't show in the other text boxes. Also as soon as form loads its already putting info in the other textboxes (from the 1st record in the array) before any name is selected from NamesComboBox.</p>
  
  <p>Heres my streamwrite form</p>
  
  <pre><code>Imports System.IO
  
  Public Class ContactInfoForm
  
  ' Declare module-level variable.
  Private ContactInfoStreamWriter As StreamWriter
  
  Private Sub SaveButton_Click(sender As System.Object, e As System.EventArgs) Handles SaveButton.Click
      ' Save the users contact information to the end of the file.
  
      ' Make sure name field and at least 1 number field is not empty.
      If NameTextBox.Text &lt;&gt; "" And PhoneNumberTextBox.Text &lt;&gt; "" Or PagerNumberTextBox.Text &lt;&gt; "" Or
          CellPhoneNumberTextBox.Text &lt;&gt; "" Or VoiceMailNumberTextBox.Text &lt;&gt; "" Then
  
          If ContactInfoStreamWriter IsNot Nothing Then ' Check if the file is open
              Dim info As String = ""
              info = String.Format("{0},{1},{2},{3},{4},{5}", _
              NameTextBox.Text, PhoneNumberTextBox.Text, PagerNumberTextBox.Text,
              CellPhoneNumberTextBox.Text, VoiceMailNumberTextBox.Text, EmailAddressTextBox.Text)
              ContactInfoStreamWriter.WriteLine(info)
  
              With NameTextBox
                  .Clear()
                  .Focus()
              End With
              PhoneNumberTextBox.Clear()
              PagerNumberTextBox.Clear()
              CellPhoneNumberTextBox.Clear()
              VoiceMailNumberTextBox.Clear()
              EmailAddressTextBox.Clear()
          Else        ' File is not open
              MessageBox.Show("You must open the file before you can save your contact information.", "File is Not Open",
                  MessageBoxButtons.OK, MessageBoxIcon.Information)
              ' Display the File Open dialog box.
              OpenToolStripMenuItem_Click(sender, e)
          End If
  
      Else
          MessageBox.Show("Please enter your name and at least 1 number where you can be reached.", "Data Entry Error",
              MessageBoxButtons.OK)
          NameTextBox.Focus()
      End If
  
  End Sub
  </code></pre>
  
  <p>Heres my Streamread section on the form to lookup in combo box.</p>
  
  <pre><code>Imports System.IO
  
  Public Class LookupForm
  
  Private Sub LookupForm_Load(sender As Object, e As System.EventArgs) Handles Me.Load
      ' Load the items into the NameComboBox list.
      Dim ResponseDialogResult As DialogResult
      Dim LineString As String
      Dim FieldString As String()
  
      Try
          ' Open the file.
          Dim ContactInfoStreamReader As StreamReader = New StreamReader("C:\Users\Cherokee\Documents\Cherokees Files\School Stuff\Visual Basic\Week 8 Data Files\pg465Ex11.5\pg465Ex11.5\pg465Ex11.5\bin\Debug\TextFile.txt")
          ' Read all the elements into the list.
          Do Until ContactInfoStreamReader.Peek = -1
              LineString = ContactInfoStreamReader.ReadLine()
              FieldString = LineString.Split(CChar(","))
              LineString = FieldString(0) 'Take First Field
              NameComboBox.Items.Add(LineString)
  
              'Set Textboxes based on position in line. 
              PhoneNumberTextBox.Text = FieldString(1)
              PagerNumberTextBox.Text = FieldString(2)
              CellPhoneNumberTextBox.Text = FieldString(3)
              VoiceMailNumberTextBox.Text = FieldString(4)
              EmailAddressTextBox.Text = FieldString(5)
  
          Loop
  
          ' Close the file.
          ContactInfoStreamReader.Close()
      Catch ex As Exception
          ' File missing.
          ResponseDialogResult = MessageBox.Show("Create a new file?", "File Not Found",
              MessageBoxButtons.YesNo, MessageBoxIcon.Question)
          If ResponseDialogResult = Windows.Forms.DialogResult.No Then
              ' Exit the program.
              Me.Close()
          End If
      End Try
  End Sub
  </code></pre>