Question TextChanged change Label from a text file.

klturi421

Member
Joined
May 5, 2011
Messages
5
Programming Experience
Beginner
Basically I want to have a textbox (TextBox1) that when you enter an area code (ex. 512) that it will return the state (TEXAS) in the label (Label1). One way that I had been trying before was to reference from a text file the array that I am using which happens to be in the format of 512, TEXAS.

I would like to use this for multiple area codes and have been unsuccessful in getting it to work. Any help with that? Any ideas how I could get it to work?
 
By multiple area codes, what do you mean?
 
By multiple area codes I mean I have a text file with area codes listed in this format:

512, TEXAS
330, OHIO
773, IL
229, Georgia

Etc...(over 300 lines of area codes)

What I want to have happen is if someone were to type in 512 in a textbox, the label would return TEXAS.
 
Try something like this:

VB.NET:
Imports System.IO

VB.NET:
Private Sub txtAreaCode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAreaCode.TextChanged
    Dim strLines() As String = File.ReadAllLines("C:\zip_codes.txt")
    For i = 0 To strLines.Length - 1
        If Not strLines(i) = "" Then
           Dim strSplit() As String = strLines(i).Split(",")
            If strSplit(0) = txtAreaCode.Text Then
                lblZip.Text = strSplit(1)
            End If
        End If
    Next
End Sub

Hope this helps
-Josh
 
Right, sorry about that.
 
No problem, Thanks!

Also I just want to make sure I entered it right as well, the
VB.NET:
Imports System.IO

goes below the Form1 tags right? Otherwise seems like it is working quite well for me, Thanks!
 
No, that would go above the Form1 tags, so the top of your code should look something like this:

VB.NET:
Imports System.IO
Public Class Form1
 
I tested the code, created the .txt and placed it in C:\ but it is not doing anything. When I debug the label does not change. Any help? Im not getting any errors from the code or anything.
 
Try changing this line:

If strSplit(0) = txtAreaCode.Text Then


To this:

If strSplit(0).ToLower.Trim = txtAreaCode.Text.ToLower.Trim Then
 
You can also try this

Dim rFile() As String = File.ReadAllLines("C:\textfile.txt")
Dim eachLine As String
If String.IsNullOrWhiteSpace(TextBox1.Text) = False Then
    For Each eachLine In rFile
        Dim word As String() = eachLine.Split(CChar(","))
        If String.Compare(word(0).Trim, TextBox1.Text.Trim, True) = 0 Then
            Label1.Text = word(1)
        End If
    Next
End If
 
Back
Top