If you want to read a text file in VB .Net and want to take each line of notepad file as one new item of list box.This method is useful.To read from notepad file in VB .Net we will simply use single line code.
You will need to add one listbox in your form.Add below code in which event you want to load the text file in list box.For example on form load,on button click etc.
Dim abc As String = My.Computer.FileSystem.ReadAllText(“itemInfo.txt”)
Dim pqr As String() = abc.Split(vbNewLine)ListBox1.Items.AddRange(pqr)
You may also interested in running loop for each item of list box,In this code for VB .Net I have used FOR loop to do a task for each item in list box.
For Each item In ListBox1.Items
‘put your codes hereNext item
Any further doubts ask in comments.
Jun 21, 2009 at 08:59:35
Hi Kunal Thank you! for sharing its really very helpful.
[Reply]
Jun 21, 2009 at 16:57:00
You’re welcome.Keep reading blog for more useful codes.Subscribe to me.
[Reply]
Feb 27, 2010 at 07:39:28
hey i was kindve wondering how to make it save from list box into a .txt? lol
[Reply]
May 17, 2010 at 18:25:09
Hi Kunal, I’ve been looking for a simple way of reading data from a text file to a listbox and you seem to know the easier way to do that, but the “a” just before the split function is confusing me. What is it declared as?
[Reply]