Reading txt(ASCII) file by row index and assign them to controls in vb.net

Hakan
Hakan used Ask the Experts™
on
I've text file like below and i'd like to read them by row number and assign their values automatically to Windos Form textbox control.

How is it possible to read specific rows (in my file desired values' row index is 2,5,8,11...) and assign their value to my application's textbox.

######## Log File for RBE2 elements (Be careful to have write permission to folder, file names are not important)
C:\Users\USER1\Desktop\Sample_BIF\condensRun_13042019\test4.log

######## Log File copy for RBE2 elements(Be careful to have write permission to folder, file names are not important)
C:\Users\USER1\Desktop\Sample_BIF\condensRun_13042019\test4_copy.log

######## Excel file to get set id to delete
C:\Users\USER1\Desktop\Sample_BIF\condensRun_13042019\input_condensationPrep.xlsx

######## Excel file sheet number
1

######## Column for Delete?
3

######## Column for Set ID
4

######## First Row of SetIDs (Source)
2

######## Last row of SetIDs (Source)
12

######## Sleep Time
10000

######## Column for Leaf Spring Property ID
6

######## Column for Reference Nodes
7

######## Column for Interface Nodes
8

######## BUSHING STARTS - Changed node number start. This number will be the start point while renumbering
12001

######## Bush properties K1
150000

######## Bush properties K2
200000

######## Bush properties K3
300000

######## Bush properties K4
400000

######## Bush properties K5
500000

######## Bush properties K6
600000

######## Bush property ID
120

######## Bush element ID start
30001

######## Fig file to open
C:\Users\USER1\Desktop\Sample_BIF\condensRun_13042019\test.fig

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
I must ask: What is your use-case?

Is this a kind of configuration file? Then you should use XML or better the app.config of your program.

Otherwise its something like:

Dim reader as StreamReader = My.Computer.FileSystem.OpenTextFileReader(f"iletoimport")
Dim lineContent as String
Dim lineCount as Integer

Do
  lineCount = lineCount + 1
  lineContent = reader.ReadLine   
  Select Case lineCount
  Case Is = 2
    txtApplicationTextBox.Value = txtApplicationTextBox.Value + lineContent
  Case Is = 5
    txtApplicationTextBox.Value = txtApplicationTextBox.Value + lineContent
  Case Is = 8
    txtApplicationTextBox.Value = txtApplicationTextBox.Value + lineContent
  Case Is = 11  
    txtApplicationTextBox.Value = txtApplicationTextBox.Value + lineContent
  End Select
Loop Until lineContent Is Nothing

reader.Close()

Open in new window

Author

Commented:
Yes you're right but this settings file not belongs to my application it's for editing 3rd part tool's settings file. I'll try your code and give feedback. Thanks.
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
I would just use System.IO.File.ReadAllLines(), and access the resulting array:
Dim fileName As String = "c:\some folder\directory\file.txt"
Dim lines() As String = System.IO.File.ReadAllLines(fileName)
' ... now you can access lines directly by index ...
' ... don't forget that arrays are zero based ...

' get line 3:
Dim index As Integer = 3
If index >= 1 AndAlso index <= lines.LengthThen
	Dim line3 As String = lines(index - 1)
	Debug.Print("line3: " & line3)
End If

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial