[2005] look up button (reading text file)
I am trying to make it read from the text file. the items from the text should be in an array. Then when the user types in the Abbreviation text box 2 letters it should try to find the state. Same for the State box to find the abbreviation. When I compile it, it tells me I need to declare "NEW" in my if statement
Code:
If .stateText.Text.ToUpper.Trim = USstate(indexInteger).stateNameString.ToUpper.Trim Then
full sub below
Thanks for any comment or suggestions in advance
Code:
Private Sub LookUpButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LookUpButton.Click
Dim indexInteger As Integer
Dim foundBoolean As Boolean
With Me
If .AbbreviationRadioButton.Checked = True Then
Do Until foundBoolean Or indexInteger > 55
If .stateText.Text.ToUpper.Trim = USstate(indexInteger).stateNameString.ToUpper.Trim Then
AbText.Text = USstate(indexInteger).abbrString
foundBoolean = True
Else
indexInteger += 1
End If
Loop
If Not foundBoolean Then
MessageBox.Show("the state could not be found", "No match", MessageBoxButtons.OK, MessageBoxIcon.Information)
AbText.SelectAll()
AbText.Focus()
End If
End If
If .NameRadioButton.Checked = True Then
Do Until foundBoolean Or indexInteger > 55
If .AbText.Text.ToUpper.Trim = USstate(indexInteger).abbrString.ToUpper.Trim Then
Me.stateText.Text = USstate(indexInteger).stateNameString
foundBoolean = True
Else
indexInteger += 1
End If
Loop
If Not foundBoolean Then
MessageBox.Show("the state could not be found", "No match", MessageBoxButtons.OK, MessageBoxIcon.Information)
stateText.SelectAll()
stateText.Focus()
End If
End If
End With
End Sub
End Class
Re: [2005] look up button (reading text file)
What type of variable is USstate?
Where is it declared. That's most likely the problem. Lists for example have to be declared with a New Keyword.
vb Code:
Dim myExamples As New List(of String)
Re: [2005] look up button (reading text file)
Code:
Imports System.IO
Public Class Form1
Structure state
Dim stateNameString As String
Dim abbrString As String
End Structure
Dim USstate() As state
Dim stateStreamReader As New StreamReader("structurearray.txt")
Private Sub selectedRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NameRadioButton.CheckedChanged, AbbreviationRadioButton.CheckedChanged
Dim selectedRadiobutton As RadioButton
selectedRadiobutton = CType(sender, RadioButton)
If AbbreviationRadioButton.Checked Then
AbbreviationRadioButton = selectedRadiobutton
stateText.Clear()
stateText.ReadOnly = True
AbText.ReadOnly = False
AbText.Focus()
Else
NameRadioButton = selectedRadiobutton
AbText.Clear()
AbText.ReadOnly = True
stateText.Focus()
stateText.ReadOnly = False
End If
End Sub
Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
AbText.Clear()
stateText.Clear()
stateText.ReadOnly = False
AbText.ReadOnly = False
AbText.Focus()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub LookUpButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LookUpButton.Click
Dim indexInteger As Integer
Dim foundBoolean As Boolean
With Me
If .AbbreviationRadioButton.Checked Then
Do Until foundBoolean Or indexInteger > 130
If .stateText.Text.Trim = USstate(indexInteger).stateNameString.ToUpper.Trim Then
AbText.Text = stateStreamReader.ReadLine
foundBoolean = True
Else
indexInteger += 1
End If
Loop
If Not foundBoolean Then
MessageBox.Show("the state could not be found", "No match", MessageBoxButtons.OK, MessageBoxIcon.Information)
AbText.SelectAll()
AbText.Focus()
End If
End If
If .NameRadioButton.Checked Then
Do Until foundBoolean Or indexInteger > 55
If .AbText.Text.ToUpper.Trim = USstate(indexInteger).abbrString.ToUpper.Trim Then
Me.stateText.Text = USstate(indexInteger).stateNameString
foundBoolean = True
Else
indexInteger += 1
End If
Loop
If Not foundBoolean Then
MessageBox.Show("the state could not be found", "No match", MessageBoxButtons.OK, MessageBoxIcon.Information)
stateText.SelectAll()
stateText.Focus()
End If
End If
End With
End Sub
End Class
Edited with the full code
Re: [2005] look up button (reading text file)
I think my issue is that it is reading the text file , but not loading the array with the info from the text file. let me see if I can stumble onto that :D
1 Attachment(s)
Re: [2005] look up button (reading text file)
I figured out what I am trying to accomplish. I just cant figure out how to get it to occur.
I am trying to get the contents of the text file into an array.
The first line is the state abbreviation
the second line is the state full name
each odd line is the abbreviation and each even line is the full name.
it would reverse if i select name. it would display the abbreviation if i spell the state
in the above code it is loading the file with my streamreader but i can't figure out how to get it into the array.
http://img19.imageshack.us/img19/4742/1111vz9.jpg
Re: [2005] look up button (reading text file)
First of all I think the problem you were having was because of the way you declared it.
There's a difference between saying USstate() and USstate(55)
So, declare your variable:
vb Code:
Private USstate(55) As State
There are loads of ways to do this. Here's just one to load it into an array.
vb Code:
Dim j As Integer = 0
Dim SR As New IO.StreamReader("C:\Temp\structurearray.txt")
Dim temp As New List(Of String)
Do Until SR.Peek = -1
temp.Add(SR.ReadLine)
Loop
For i As Integer = 0 To temp.Count - 1 Step 2 'Every 2nd item
USstate(j).abbrString = temp(i) 'start at 0 index in List
USstate(j).stateNameString = temp(i + 1) 'start at 1 index in List
j += 1
Next
edited: corrected the order items read into.
Re: [2005] look up button (reading text file)
I shall try this thank you
Re: [2005] look up button (reading text file)
I just realised that in the text file, the abbreviation comes first. So it should be:
vb Code:
'just change around the i + 1 bit.
USstate(j).stateNameString = temp(i + 1) 'start at 1 index in List
USstate(j).abbrString = temp(i) 'start at 0 index in List