Home > other >  Reading text file and searching for a number
Reading text file and searching for a number

Time:03-02

I did search a lot about this but found nothing. So, I simply have 1 textbox, 1 label called "Invalid", 1 button, and finally a text file.

The text file has numbers in lines or more likely zip codes written in this way:

10000
23251
30021
51931
and so on...
  • textbox = type the zip code number
  • button = to search if the number which I did type in the textbox is inside the text file or not. If yes, then label1.text = "Valid"

and that's pretty much all of it.

I tried the following but I know it's totally wrong it doesn't work it will take so long time to type each and every single number in it.

Can someone provide a method and a code to use and apply it directly not to mention understanding it as I'm new regarding VB.NET stuff.

I tried File.ReadAllLines, was searching about some documentations but I only did find C# not VB.NET

Someone told me to used File.ReadAllLines as shown:

If linesFromFile.Any(Function(l) l = myTextBox.Text) Then
    label1.Text = "Valid"
End If

Tried to apply it but no good.

Dim File As String = "Georgia.txt"
Dim Georgia As String = IO.File.ReadAllLines(File)
If Georgia.Any(Function(l) l = TextBox1.Text) Then
Label1.Text = "Valid"
End If

CodePudding user response:

Here is a process to find a match. There are many ways to do this. Create a form called frmTest add two buttons btnFind and btnLoad then a Textbox called tbFind.Also you will need a ListBox named ListBox1.
In my example I load the data into a String Array called strData.
It needs to be declared top level like this Dim strData(12) As String
Now that the data is in the String Array load the strData in to the ListBox.
Enter the value you want to find into the tbFind
Now click the btnFind.

Dim strData(12) As String

Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
    LoadLB()
End Sub

Public Sub LoadLB()
    strData = {"", "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"}
    Dim allMo As String
    For i As Integer = 1 To 12
        allMo = i & " - " & strData(i)
        ListBox1.Items.Add(allMo)
    Next
End Sub
Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click

    For x As Integer = 1 To 12
        If tbFind.Text = strData(x) Then
            MsgBox("Match " & strData(x))
        End If
    Next

End Sub
  • Related