How to read from a text file into list box in VB .NET ?

Posted By: KP On June 20, 2009 Under Other, Programming Categories

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 here

Next item

Any further doubts ask in comments.



Share with Friends ( Sharing is Caring! )


Subscribe For E-mail Notification Of Article Updates

Enter your email address:

NOTICE:
DO NOT FORGET TO CLICK ON THE VERIFICATION LINK AFTER SUBSCRIBING,THE VERIFICATION MAIL WILL BE SENT TO YOU FROM "FeedBurner Email Subscriptions".
( What is this? Why I need to do this?)

Related Posts

4 responses. Wanna say something?

  1. Devil Boy
    Jun 21, 2009 at 08:59:35
    #1

    Hi Kunal Thank you! for sharing its really very helpful.

    [Reply]

  2. KP
    Jun 21, 2009 at 16:57:00
    #2

    You’re welcome.Keep reading blog for more useful codes.Subscribe to me.

    [Reply]

  3. _VB_
    Feb 27, 2010 at 07:39:28
    #3

    hey i was kindve wondering how to make it save from list box into a .txt? lol

    [Reply]

  4. Mpho
    May 17, 2010 at 18:25:09
    #4

    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]

Post a Comment

OR