|
-
Feb 18th, 2013, 07:35 AM
#1
Thread Starter
Member
[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
-
Feb 18th, 2013, 11:55 AM
#2
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?
-
Feb 18th, 2013, 12:02 PM
#3
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
-
Feb 18th, 2013, 12:07 PM
#4
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.
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!
-
Feb 18th, 2013, 12:14 PM
#5
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.
-
Feb 18th, 2013, 02:12 PM
#6
Thread Starter
Member
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
-
Feb 18th, 2013, 02:18 PM
#7
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
-
Feb 18th, 2013, 02:26 PM
#8
Thread Starter
Member
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.
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|