Wednesday, May 8, 2013

How to fill ComboBox with Text File

How to fill ComboBox with Text File

Fill a ComboBox quickly by importing data from a Text File


ComboBox SampleText files are the simplest way to store data. It requires minimum knowledge to use them. Knowing how to read them make it possible to help you fill data in a control like in a ComboBox. Here is a very simple example. It uses an OpenFileDialog to get a Text File the user is requesting. The program validates the file and if everything is fair, it loads each line and put them in the ComboBox1. When the cursor reaches the end of the file, the property EndOfStream will be true. This will make your program quit the loop and safely close the function. Remember to close the Stream File after you finish.
Download the project sample here: ComboBoxSample.zip

ComboBox Sample

ComboBox Sample

SQL Server Developer Edition 2012 and Microsoft Visual Studio Pro 2012 are very cheap on Amazon ! Free Delivery. Check here:
 






    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Dim oStream As IO.StreamReader
        Dim oOpenFileDialog As OpenFileDialog


        oOpenFileDialog = New OpenFileDialog
        oOpenFileDialog.SupportMultiDottedExtensions = False

        If oOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            If IO.File.Exists(oOpenFileDialog.FileName) Then
                oStream = New IO.StreamReader(oOpenFileDialog.FileName)

                'Read all lines and add them into the ComboBox1
                Do Until oStream.EndOfStream = True
                    ComboBox1.Items.Add(oStream.ReadLine)
                Loop
                oStream.Close() 'very important to close the file
            End If
        End If


    End Sub




No comments:

Post a Comment