【问题标题】:vb.net - Comparing two text files and retrieving a specific valuevb.net - 比较两个文本文件并检索特定值
【发布时间】:2018-02-14 18:32:54
【问题描述】:

这个问题与我之前的问题有关

我目前正在制作一个注册表单,其中学生的所有详细信息都保存在一个文本文件中。

我使用文本文件填充了组合框。

这些值的格式例如:(code~school name) SCH001~Saint 托马斯学院

这就是我将详细信息保存在文本文件中的方式:

Dim firstname, lastname, email, mobile, level, currentschool, currenttrack, institution1, institution2, institution3, institution4, institution5, institution6, courses1, courses2, courses3, macaddress, eventcode, idseq, filename, logfiledirectory, targetdirectory, log, configdir, printschool As String
    firstname = txtFName.Text
    lastname = txtLName.Text
    email = txtEmail.Text
    mobile = txtMobile.Text
    level = cmbLevel.Text
    currenttrack = cmbCurrentTrack.Text
    printschool = cmbCurrentSchool.Text
    currentschool = cmbCurrentSchool.SelectedValue
    institution1 = cmbInstitution1.SelectedValue
    institution2 = cmbInstitution2.SelectedValue
    institution3 = cmbInstitution3.SelectedValue
    institution4 = cmbInstitution4.SelectedValue
    institution5 = cmbInstitution5.SelectedValue
    institution6 = cmbInstitution6.SelectedValue
    courses1 = cmbCourse1.SelectedValue
    courses2 = cmbCourse2.SelectedValue
    courses3 = cmbCourse3.SelectedValue

    If mobile = "" Then
        mobile = "09000000000"
    End If

    If firstname = "" Or lastname = "" Or email = "" Or level = "" Or currentschool = "" Or currenttrack = "" Then
        MessageBox.Show("Please enter the required fields!", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

我有一个数据网格视图,其中显示了文本文件中所有保存的详细信息。 这是我的数据网格视图的屏幕截图:

我想做什么:我想使用源文本文件(我用来填充组合框的那个)中的当前学校代码(参考图片)检索学校名称。我希望学校名称显示在 datagridview 中,而不是学校代码。 有没有可能的方法?

我希望你们明白我想说什么。 我是新来的。

【问题讨论】:

  • 就是这么简单。在文本文件内容中搜索学校代码。得到线后,在'~'上分割线。现在我真的很想知道,你自己尝试了多少。正如我为前两个问题提供的答案一样。你现在可能已经学会了!

标签: vb.net visual-studio datagrid


【解决方案1】:

如果你想产生与Nobody's answer相同的输出但不循环,你可以使用LINQ,这也使用更少的代码行但实现相同的输出

Dim query = filecontents.Where(Function(x) x.Contains(schoolcode)).Select(Function(x) x.Split("~")(1)).FirstOrDefault
Return query.ToString()

【讨论】:

  • 赞成你的答案,因为我比我更喜欢它:)
  • @Nobody 谢谢,你完成了大部分工作,我只是提供了循环的替代方法:)
  • 我的 Linq 知识有点不靠谱。所以使用 foreach 版本:] 尽管如此,我还是学到了一些东西。非常感谢;]
【解决方案2】:

就像我上次回答的那样,使用这个函数:

Function FetchSchoolName(schoolcode As String, filepath As String) As String
    Dim filecontents = File.ReadAllLines(filepath)
    For Each line As String In filecontents
        If (line.Contains(schoolcode.Trim())) Then
            Return line.Split("~"c)(1)
        End If
    Next
    Return String.Empty
End Function 

文本输入示例:

sch001~abcinstitute
sch002~myacademy
sch003~someschoolname

然后在最顶层的函数中传递学校代码(您从 datagridview 获得)和文件路径

MessageBox.Show(FetchSchoolName("sch002", "C:\Users\nobody\Desktop\somefile.txt"))

输出:

我的学院

现在对于您的DataGridView,无论您在哪里填充(源绑定),它都使用上述方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多