Results 1 to 8 of 8

Thread: [RESOLVED] 2 forms - 2nd pulls data from a text file in the 1st

  1. #1

    Thread Starter
    Member MrCrow's Avatar
    Join Date
    Feb 2013
    Posts
    41

    Resolved [RESOLVED] 2 forms - 2nd pulls data from a text file in the 1st

    I have 2 forms: The first one loads a text file and populates a listbox with the first field of each line. The second form then takes what is currently selected in the listbox on form 1 and reads the rest of the fields and populates it in textboxes and a radio button on form 2.

    Here is what I have for the button click on form 2, but it is not doing anything. My minimal understanding of VB tells me the logic is right, but obviously my execution is not. Any pointers?
    Code:
    Private Sub btnRecord_Click(sender As System.Object, e As System.EventArgs) Handles btnRecord.Click
            Dim fields() As String = IO.File.ReadAllLines("Myfile.txt")
            fields = Split(","c)
            Dim first_info As String = frmONE.lstOutput.SelectedItem
            If fields(0) = first_info Then
                txtFirst.Text = fields(0)
                txtSecond.Text = fields(1)
                txtThird.Text = fields(3)
                txtFourth.Text = fields(4)
                If fields(2) = "T" Then
                    radT.Checked = True
                ElseIf fields(2) = "F" Then
                    radF.Checked = True
                End If
            End If
        End Sub

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: 2 forms - 2nd pulls data from a text file in the 1st

    Are you getting it to load the text file but afterwards it doesn't populate anything?
    Are you getting it to load the text file and populate the listbox, but nothing happens when you try to read the rest of the fields?
    Is absolutely nothing happening anywhere?

    Basically, what does work and what does not work?

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: 2 forms - 2nd pulls data from a text file in the 1st

    A little help...
    Code:
            Dim lns() As String = IO.File.ReadAllLines("Myfile.txt")
            For Each l As String In lns
                Dim fields() As String = l.Split(New Char() {","c})
                '
            Next
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: 2 forms - 2nd pulls data from a text file in the 1st

    Er ... logic?

    vb.net Code:
    1. Dim fields() As String = IO.File.ReadAllLines("Myfile.txt") ' creates an array of lines
    2.         fields = Split(","c) ' sets the array to nothing

    The array of lines needs to be looped through and so left intact. Split works on a defined string (eg. stringvar.Split(","c)), it doesn't magically gather up strings that happen to be floating around somewhere in an array.

    Your If needs some work too. What happens if neither fields(0) = firstinfo nor fields(2) = "F"? Well, nothing, obviously, but that can't be the intention.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: 2 forms - 2nd pulls data from a text file in the 1st

    This is a problem
    Code:
    Dim fields() As String = IO.File.ReadAllLines("Myfile.txt")
            fields = Split(","c)
    Here you are populating an array named fields with the lines from a file
    but then you are overwriting the content of that array with nothing on the next line

    Not quite sure what you are trying to do but you need to change it a bit.
    Code:
    Dim Records() As String = IO.File.ReadAllLines("Myfile.txt")
    Dim Fields() as string=Records(0).Split(","c)
    Though you really should be using a loop the above is just an example of how to get the fields from the first line of the file

    A For Each loop would be the likely choice here.

    Edit: I see two others picked out the same thing while I was typing ...
    Last edited by DataMiser; Feb 18th, 2013 at 12:19 PM.

  6. #6

    Thread Starter
    Member MrCrow's Avatar
    Join Date
    Feb 2013
    Posts
    41

    Re: 2 forms - 2nd pulls data from a text file in the 1st

    Thanks for all of the replies. However, I am still completely lost. I have only been using VB for about 1 month now and would prefer examples to point me in the right direction. I'm an old guy that is not quick to get all this, so all help is appreciated!

    Here's my updated code (but still not working) Go easy on me, because I am just trying to understand how/why/what everything does.
    Code:
    Private Sub btnRecord_Click(sender As System.Object, e As System.EventArgs) Handles btnRecord.Click
            Dim fields() As String
            Dim i As Integer
            Dim arraytext() As String
            fields = IO.File.ReadAllLines("Myfile.txt")
            arraytext = fields.Split(",") 'THIS LINE IS INCORRECT, BUT DO NOT KNOW WHAT IT SHOULD BE
            For i = 0 To UBound(arraytext)
                txtFirst.Text = fields(0)
                txtSecond.Text = fields(1)
                txtThird.Text = fields(3)
                txtFourth.Text = fields(4)
                If fields(2) = "T" Then
                    radT.Checked = True
                Else
                    radF.Checked = True
                End If
            Next i
        End Sub

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: 2 forms - 2nd pulls data from a text file in the 1st

    Try this as is

    Code:
            Dim lns() As String = IO.File.ReadAllLines("Myfile.txt")
    
            'hover over lns.  look at each line.  press F5
            Stop
    
            For Each l As String In lns
                Dim fields() As String = l.Split(New Char() {","c})
    
                'hover over l and fields.    press F5
                Stop
    
                Dim first_info As String = frmONE.lstOutput.SelectedItem
                If fields(0) = first_info Then
                    txtFirst.Text = fields(0)
                    txtSecond.Text = fields(1)
                    txtThird.Text = fields(3)
                    txtFourth.Text = fields(4)
                    If fields(2) = "T" Then
                        radT.Checked = True
                    ElseIf fields(2) = "F" Then
                        radF.Checked = True
                    End If
                End If
            Next
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    Member MrCrow's Avatar
    Join Date
    Feb 2013
    Posts
    41

    Re: 2 forms - 2nd pulls data from a text file in the 1st

    Well, it will take me a while to understand it all, but it works. Now I am just trying to look at how/why. Anyway, thank you, dbasnett.

    Quote Originally Posted by dbasnett View Post
    Try this as is

    Code:
            Dim lns() As String = IO.File.ReadAllLines("Myfile.txt")
    
            'hover over lns.  look at each line.  press F5
            Stop
    
            For Each l As String In lns
                Dim fields() As String = l.Split(New Char() {","c})
    
                'hover over l and fields.    press F5
                Stop
    
                Dim first_info As String = frmONE.lstOutput.SelectedItem
                If fields(0) = first_info Then
                    txtFirst.Text = fields(0)
                    txtSecond.Text = fields(1)
                    txtThird.Text = fields(3)
                    txtFourth.Text = fields(4)
                    If fields(2) = "T" Then
                        radT.Checked = True
                    ElseIf fields(2) = "F" Then
                        radF.Checked = True
                    End If
                End If
            Next

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