We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

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

Last post 05-01-2008 7:23 AM by AllenSmith. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 04-29-2008 8:45 PM

    • guivarch
    • Not Ranked
    • Joined on 04-29-2008
    • United Kingdom
    • New Member
    • Points 20

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

    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

    • Post Points: 10
  • 04-30-2008 4:58 AM In reply to

    • AllenSmith
    • Not Ranked
    • Joined on 04-08-2008
    • India
    • Junior Member
    • Points 135

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

     

    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

    • Post Points: 10
  • 04-30-2008 1:55 PM In reply to

    • guivarch
    • Not Ranked
    • Joined on 04-29-2008
    • United Kingdom
    • New Member
    • Points 20

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

    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!

    • Post Points: 10
  • 05-01-2008 7:23 AM In reply to

    • AllenSmith
    • Not Ranked
    • Joined on 04-08-2008
    • India
    • Junior Member
    • Points 135

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

     

    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 Points: 5
Page 1 of 1 (4 items)