[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
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?
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
Re: 2 forms - 2nd pulls data from a text file in the 1st
Er ... logic?
vb.net Code:
Dim fields() As String = IO.File.ReadAllLines("Myfile.txt") ' creates an array of lines
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.
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 ...
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
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
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
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