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

时间:2015-01-30 12:45:45

标签: vb.net

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

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

[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 

谢谢!

1 个答案:

答案 0 :(得分:0)

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

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开始并以For结尾的Ubound(NumModule.GetNumbers(StringPath:="YourStringPathHere",1)循环将您的数字添加到组合框中。如下:

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

我的最终结果:

enter image description here