Vb.net VB使用逗号分隔的txt文件中的特定字段填充组合框
需要从逗号分隔的txt文件填充NameComboBox。希望用户能够从NameComboBox下拉列表中仅选择名称和其他要填写的文本框 现在,整个记录正在填充组合框Vb.net VB使用逗号分隔的txt文件中的特定字段填充组合框,vb.net,visual-studio-2010,combobox,Vb.net,Visual Studio 2010,Combobox,需要从逗号分隔的txt文件填充NameComboBox。希望用户能够从NameComboBox下拉列表中仅选择名称和其他要填写的文本框 现在,整个记录正在填充组合框 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 NameComb
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
****2015年11月2日更新:
名称会以应有的方式显示在名称组合框中,但当选择其中一个时,信息不会显示在其他文本框中。此外,只要表单加载了它已经在其他文本框中放置的信息(从数组中的第一条记录),就可以从NamesComboBox中选择任何名称
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
这是我的表格
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
您可以拆分读取的行并选择所需的列位置,例如,如果第一列是名称,则代码如下所示:
' 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()
您可以拆分读取的行并选择所需的列位置,例如,如果第一列是名称,则代码如下所示:
' 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()
现在我的名字显示在combobox中,但是我如何让文本框填充所选名字的其余字段呢?谢谢你的帮助!同样的程序也适用于文本框,例如txtAge.text=fields(1)我现在有这个,但我应该把它放在哪里?我在NameComboBox.Items.Add(Namestring)后面尝试过,但它只是在文本框中加载第一条记录的信息
PhoneNumberTextBox.Text=FieldString(1)PagerNumberTextBox.Text=FieldString(2)CellPhoneNumberTextBox.Text=FieldString(3)VoiceMailNumberTextBox.Text=FieldString(4)EmailAddressTextBox.Text=FieldString(5)
6个字段/每条记录(姓名、电话号码、寻呼机号码、手机号码、语音信箱号码、电子邮件地址).Name在正在工作的ComboBox中读取。现在,当用户从ComboBox中选择Name时,我正在尝试获取列表框中显示的其余字段。使用此信息和表单结构更新问题。现在,我在ComboBox中显示了名称,但如何获取文本框以填充所选名称的其余字段?谢谢您的帮助!同样的过程也适用于文本框,例如txtAge.text=fields(1)我现在有这个,但我把它放在哪里?我在NameComboBox.Items.Add(Namestring)后面尝试过,但它只加载文本框中第一条记录的信息。PhoneNumberTextBox.text=FieldString(1)PagerNumberTextBox.Text=FieldString(2)CellPhoneNumberTextBox.Text=FieldString(3)VoiceMailNumberTextBox.Text=FieldString(4)EmailAddressTextBox.Text=FieldString(5)
6个字段(姓名、电话号码、寻呼机号码、手机号码、语音信箱号码、电子邮件地址).Name在正在工作的ComboBox中读取。现在,当用户从ComboBox中选择Name时,我正在尝试让列表框中显示其余字段。使用此信息和表单结构更新问题。