vb 如何从文本中获取一些信息到组合框

How to get some info from text to combobox in vb

这是我的第一个问题,我不知道该说什么,但我会尽力解释。

现在我有一个包含以下内容的文本文件:

[2000] name : any thing job : doctor age : 50
[2002] name : anything else job : anything age : 60
[2003] name : anything else job : anything else age : 55

如何在组合框中获取此 [ ] 中的所有内容,以便组合框项目应为 2000、2002、2003?顺便说一句,我的文本文件有很多行,所以我不能用行来做。

我的第二个问题,例如组合框中是否有数字

64080 
65090 
62055

什么代码可以删除组合框中每个项目的最后一个数字:

6408
6509
6205 

谢谢!

我希望这个模块能提供一个好的起点:

Imports System.IO.File
Module NumModule

    Function GetNumbers(ByVal PathString As String) As Integer()

        Dim Line As String
        Dim IntTable() As Integer
        'Determines line count
        Dim LineCount = System.IO.File.ReadAllLines(PathString).Length - 1
        ReDim IntTable(LineCount)
        'Initiating counter 
        Dim Counter As Integer = 0
        'Copying numbers into Intable
        Dim stream As New IO.StreamReader(PathString)
        For Counter = 0 To LineCount
            Line = stream.ReadLine
            Dim PosS As Integer
            Dim PosE As Integer
            PosE = Line.IndexOf("]") - 1
            PosS = Line.IndexOf("[") + 1
            Dim Number As Integer
            Number = CInt(Line.Substring(PosS, PosE))
            IntTable(Counter) = Number
        Next Counter
        stream.Close()
        GetNumbers = IntTable
    End Function
End Module

稍后,将组合框添加到您的表单(我们将其称为 Form1)并双击它以转到 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

从那里,您现在可以使用从 0 开始并以 Ubound(NumModule.GetNumbers(StringPath:="YourStringPathHere",1) 结束的 For 循环将您的数字添加到您的组合框中。喜欢关注:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim counter As Integer = UBound(NumModule.GetNumbers(PathString:="D:\Test.txt"), 1)
        For i As Integer = 0 To counter

            ComboBox1.Items.Add(NumModule.GetNumbers(PathString:="D:\Test.txt")(i).ToString)

        Next
    End Sub

要回答您的第二个问题,请将 Form1_load 的内容替换为以下内容:

 Dim counter As Integer = UBound(NumModule.GetNumbers(PathString:="D:\Test.txt"), 1)
    For i As Integer = 0 To counter
        Dim TrimmedString As String = NumModule.GetNumbers(PathString:="D:\Test.txt")(i).ToString
        If Len(TrimmedString) = 5 Then
            TrimmedString = TrimmedString.Remove(TrimmedString.Length - 1)
        End If
        ComboBox1.Items.Add(TrimmedString)

    Next

我的end-result: