【问题标题】:How to auto fill a textbox from a comobobox using a textfile如何使用文本文件从组合框自动填充文本框
【发布时间】:2017-06-13 17:41:31
【问题描述】:

我目前正在 VB 中创建一个表单,该表单使用文本文件收集组合框的信息,然后自动使用文本文件中以逗号分隔的下一个单词来自动填充文本框。 但是我写的当前代码显示了组合框的列表,但总是用第二行的第二个单词自动填充文本框,并且在我从组合框中选择另一个选项后不会更改文本框有人可以帮忙吗?

抱歉,如果这不是很清楚。

我的文本文件是这种格式:

罗伯特,贝尔维尤路 5 号

马丁,贝尔维尤路 6 号

等等……

我的代码如下:

将 LineString 变暗为字符串

Dim FieldString As String()

    Try

        Dim ContactInfoStreamReader As StreamReader = New StreamReader("C:\temp\test1.txt")

        Do Until ContactInfoStreamReader.Peek = -1

            LineString = ContactInfoStreamReader.ReadLine()
            FieldString = LineString.Split(CChar(","))
            LineString = FieldString(0)
            ComboBox1.Items.Add(LineString)
            Loop
        RichTextBox2.Text = FieldString(1)
        ContactInfoStreamReader.Close()
    Catch ex As Exception
        MsgBox("""Customers Name & Address.txt"" file was not found")
    End Try

【问题讨论】:

  • 为什么不使用:File.ReadAllLines(),然后只使用For Each Line
  • 当comboBox值改变时,你想对文本框做什么?是否要将组合框的下一个值加载到文本框?
  • 我希望它采用逗号后的值,例如,如果我在组合框中选择 Robert,我希望文本框有 5 BellView Road 但如果我选择 martin,我希望它显示 6 BellView路。

标签: vb.net combobox


【解决方案1】:

好的,我知道你已经提供了代码,但是我会这样做。

首先我们创建一个Person Class 并创建一个Person List 来存储每个Person

之后,我们将文本文件读取/加载到string array 中,然后我们使用Linq 将逗号所在的文本文件行拆分,并将名称和地址分类为两个键,一个是NAME,另一个是是 ADDRESS

完成后,我们循环遍历每个项目并创建一个新的 Person,其值 value.NAMEValue.Address 用于人员姓名和地址,并添加人的名字变成combobox

最后我们添加一个ComboBox Event,将Textbox's文本更改为与List of People匹配的选定ComboBox's Index,该List of People用于存储每个Person

Public Class Form1
Dim People As New List(Of Person)

Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
    Try
        TextBox1.Text = People(ComboBox1.SelectedIndex).address
    Catch
        ''this will prevent errors if there is a name with no address
        TextBox1.Text = ""
    End Try
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ''This is the location I used, feel free to change it here.
    Dim location As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
    Dim fileName As String = "test1.txt"
    Dim path As String = System.IO.Path.Combine(location, fileName)

    Dim query = From line In System.IO.File.ReadAllLines(path)
                Let val = line.Split(",")
                Select New With {Key .NAME = val(0), Key .ADDRESS = val(1)}

    For Each value In query
        Dim p = New Person(value.NAME, value.ADDRESS)
        People.Add(p)
        ComboBox1.Items.Add(value.NAME)
    Next

    ''ADD OTHER COMBOBOX ITEMS HERE IF NEED BE, SO EVERYTHING IS IN ORDER
    ComboBox1.Items.Add("ANDREW")
End Sub
End Class

Class Person
Public Property name() As String
Public Property address() As String
Public Sub New(name As String, address As String)
    Me.name = name
    Me.address = address
End Sub
End Class

编辑:
这就是我格式化文本文件的方式。
Robert,5 BellView Road
Martin,6 BellView Road

【讨论】:

  • 嗨,我刚刚尝试过,但我仍然遇到问题,上面对组合框进行了正确排序,但是当我选择名称时,它会在尝试写入文本框时引发错误。 (参数超出范围异常)它在代码的这个阶段执行此操作。 Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged TextBox1.Text = People(ComboBox1.SelectedIndex).address End Sub
  • 你有一行一行的文本文件信息吗?
  • 是的,格式如下: Line 1: Robert,5 BellView Road Line 2: Martin,6 BellView Road
  • 组合框中还有其他名字吗?
  • 不是 ATM,但当我能正常工作时,我会加到 50 个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
相关资源
最近更新 更多