Results 1 to 8 of 8

Thread: [2005] look up button (reading text file)

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    9

    [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

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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:
    1. Dim myExamples As New List(of String)
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    9

    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
    Last edited by rollntider; Apr 4th, 2007 at 03:54 PM.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    9

    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    9

    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.
    Attached Files Attached Files

  6. #6
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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:
    1. 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:
    1. Dim j As Integer = 0
    2.  
    3.         Dim SR As New IO.StreamReader("C:\Temp\structurearray.txt")
    4.         Dim temp As New List(Of String)
    5.  
    6.         Do Until SR.Peek = -1
    7.             temp.Add(SR.ReadLine)
    8.         Loop
    9.  
    10.         For i As Integer = 0 To temp.Count - 1 Step 2   'Every 2nd item
    11.             USstate(j).abbrString = temp(i)         'start at 0 index in List
    12.             USstate(j).stateNameString = temp(i + 1)      'start at 1 index in List
    13.             j += 1
    14.         Next

    edited: corrected the order items read into.
    Last edited by stimbo; Apr 5th, 2007 at 10:06 AM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    9

    Re: [2005] look up button (reading text file)

    I shall try this thank you

  8. #8
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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:
    1. 'just change around the i + 1 bit.
    2. USstate(j).stateNameString = temp(i + 1)      'start at 1 index in List
    3. USstate(j).abbrString = temp(i)         'start at 0 index in List
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width