【问题标题】:Adding items to listbox from a struture file in vb with an if statement?使用if语句将项目从vb中的结构文件添加到列表框?
【发布时间】:2015-12-12 16:08:50
【问题描述】:

PS:我有visual studio 2012版

嘿,我有一个文件,我根据用户以单独的形式提供给我的信息将结构“personne”保存在 .txt 文件中,现在我需要搜索最终包含几个人信息的文件(姓名,最后姓名、性别、城市等)适用于男性。

目标是有一个列表框显示文件中所有男性的姓名和姓氏

当我删除“if p.sexe ="homme"” 部分时,代码运行良好,但随后它会列出表单中所有人的姓名,而不仅仅是男性。 当我保持条件并且发生不明错误时,Visual Studio 甚至没有告诉我它是什么。它只是冻结,直到我关闭应用程序。

我对 vb 比较陌生,不明白这个问题。有什么建议吗?

Public Class Form4
Private Sub cmdAfFiltre_Click(sender As Object, e As EventArgs) Handles cmdAfFiltre.Click
    Dim a, i As Integer
    a = FreeFile()
    Dim p As Personne, L As Long
    Dim path As String
    path = "d:\miniprojet.txt"
    L = Len(p)
    FileOpen(a, path, OpenMode.Random, , , L)
    i = 0
    While Not EOF(a)

        FileGet(a, p, i + 1)
        If rdnHomme.Checked Then
           'the goal is to only add to the list when the "person"'s sexe is male 
           'homme means man
        If p.sexe = homme Then

                lstPersonne.Items.Add(p.nom & p.prenom)
                i = i + 1
            End If
        End If
    End While
    FileClose(a)
End Sub

【问题讨论】:

  • 摆脱那些遗留的 VB 文件功能并使用 NET 版本会好得多。您可以用 2-3 行序列化整个 List(of Person) 并读回整个列表,而不是将该文件用作随机访问。然后只需管理底层列表。

标签: vb.net file if-statement visual-studio-2012 listbox


【解决方案1】:
 If p.sexe = homme Then
    lstPersonne.Items.Add(p.nom & p.prenom)
    i = i + 1
 End If

应该是

 If p.sexe = "homme" Then
    lstPersonne.Items.Add(p.nom & p.prenom)
    i = i + 1
 End If

我同意其他 cmets,您应该远离随机文件访问并使用 https://support.microsoft.com/en-us/kb/316730 之类的东西

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-11
    • 2021-11-06
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多