简体   繁体   中英

Read Text from file and put the in the TextBox in vb.net

I got the data from a text file as follows:

KILL
MAD
JOG
JUG

From the above data I want to put the text "Kill" in Textbox 1, the text "MAD" in textbox 2, the text "JOG" in the textbox 3, and "JUG" in the textbox 4
The following screenshot
Click Here

So, anyone can help me ?

Dim lines = File.ReadAllLines(path)
TextBox1.Text = lines.ElementAtOrDefault(0)
TextBox2.Text = lines.ElementAtOrDefault(1)
TextBox3.Text = lines.ElementAtOrDefault(2)
TextBox4.Text = lines.ElementAtOrDefault(3)

You could also use the indexer( fe lines(0) ) of the array instead of ElementAtOrDefault . But the latter has the advantage that it doesn't throw an exception if there are fewer elements.

However, i would use a ListBox instead or a single TextBox with Multiline=true :

TextBox1.Lines = File.ReadAllLines(path)

try to put a seprator between values stored in file, then you can read it as below:

Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt")

Dim strArray() as String = fileReader .Split("|") 'change the | by your separator

TextBox1.Text = strArray(0)
TextBox2.Text = strArray(1)
TextBox3.Text = strArray(2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM