Community discussion forum

Adding each line of a text file as an item to a combobox

  • 7 months ago

    Very new to VB and have hit a brick wall with an applicaiton

    I need my combobox contorl to add each line of a text file as an item to the combobox when the application starts so it is populated for the user. Have been searching the webfor solutions and this is the first time i have turnt to a forum. Dont really know the best approach to take.

    Any help would be greatly appreciated. Thanks

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 7 months ago

     

    Hello,

    You may try the following Code. The code checks for the existence of a file when the application starts and then reads

    each line into the Combo box.

    BEGIN CODE

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    If File.Exists("c:\source.txt") Then

    Dim str As StreamReader

    str = New StreamReader("c:\source.txt")

    While Not str.EndOfStream

    Me.ComboBox1.Items.Add(str.ReadLine)

    End While

    Else

    MsgBox("File does not exists")

    End If

    End Sub

    END CODE

    You will also have to import the namespace 'IO' on the top using

    Imports System.IO

    I believe this should help.

    Regards,

    Allen Smith
    ComponentOne LLC

    www.componentone.com

  • 7 months ago

    That worked perfectly, i cant thank you enough!

    Next step is to display a default from the list in the combobox when the application runs for the first time!

    Thanks again!

  • 7 months ago

     

    Hello,

    I am glad that my response was helpful.

    As far as your latest query is concerned, you can use the following code to set a default selection in the Combo List.

    Me.ComboBox1.SelectedIndex = 1

    Index for first item is 0 and so forth.

    Regards,

    Allen Smith

Post a reply

Enter your message below

Sign in or Join us (it's free).