VB使用逗号分隔的txt文件中的特定字段填充ComboBox

时间:2015-10-26 02:53:47

标签: vb.net visual-studio-2010 combobox

需要从逗号分隔的txt文件填充NameComboBox。希望用户能够从NameComboBox下拉列表和其他文本框中选择要填写的名称。

现在整个记录正在填充ComboBox。

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

****更新11/2/15: 名称出现在NamesComboBox中,但是当选择一个时,信息不会显示在其他文本框中。一旦表单从NamesComboBox中选择任何名称之前,表单就会将其已放入的信息加载到其他文本框中(来自数组中的第一条记录)。

继承我的streamwrite表格

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 <> "" And PhoneNumberTextBox.Text <> "" Or PagerNumberTextBox.Text <> "" Or
        CellPhoneNumberTextBox.Text <> "" Or VoiceMailNumberTextBox.Text <> "" 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

继承我在表单上的Streamread部分,在组合框中查找。

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

1 个答案:

答案 0 :(得分:0)

您可以拆分读取行并获取所需的列位置,例如,如果第一列是名称,则代码如下所示:

        ' Open the file.
        Dim ContactInfoStreamReader As StreamReader = New StreamReader("TextFile.txt")
        ' Read all the elements into the list.
        Do Until ContactInfoStreamReader.Peek = -1
            String lineRead = ContactInfoStreamReader.ReadLine()
            Dim fields as String() = lineRead.Split(",")
            NameString = fields(0) 'Take First Field
            NameComboBox.Items.Add(NameString)

            'Set Textboxes based on position in line. 
            'E.g. if Age is column 2 then
            AgeTextBox.Text = fields(1)
        Loop
        ' Close the file.
        ContactInfoStreamReader.Close()