Total Pageviews

Saturday, September 17, 2016

Open Text File in ListBox control in Visual Basic 2015

You can open text file in ListBox control in Visual Basic 215 on many ways. In this post I share the way of opening text file line by line.

Test text file

First you have to add the following code!

Imports System.IO

After add the code to the button Open file.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim OpenFileDialog1 As New OpenFileDialog

        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            Dim lines = File.ReadAllLines(OpenFileDialog1.FileName)
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(lines)
        End If
    End Sub

When you open the file it looks like the image bellow.

Open text file in ListBox
Open text file in ListBox

You can check my other posts about ListBox:
- Insert data from TextBox and InputBox into ListBox
- Delete Selected Item from ListBox in Visual Basic 2015

No comments:

Post a Comment