Results 1 to 8 of 8

Thread: displaying a text file into combo box

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    18

    Unhappy displaying a text file into combo box

    i've been trying to display a .txt file in a combo box, but all i get is one long line of all the iitems, here's the code :

    Do Until EOF(1)
    LineOfProduct = LineInput(1)
    AllProducts = AllProducts & vbCrLf & LineOfProduct
    cobProduct.Items.Add(AllProducts)
    Loop

    txtBox.Text = AllProducts
    i also included the line cobProduct.Items.Add(AllProducts) within the loop to all items , it displays the correct number of items, but again all the items in one line and repeats it til line 21 (number of items is 21)...so please help

    also how do sperate a string into two ,then display the 2nd part into a sperate listBox (i managed to display the whole string in listBox) ...again help...and thank u in advance...

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Use the classes in the framework. It will be much easier. How is the file formated? Is it comma separated or is line by line?
    Dont gain the world and lose your soul

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    18
    i'm a newbie ..so u gotta be more specifc

    and the file is line by line with commas as well ,because i want to sperate the line into 2 and store them in an array ,but i've not got a clue how it is done??...so it like this

    utguugsdgyiugh, $556
    hgihgegiygwiefyi, $568
    .
    .
    .
    EOF

    thanks

  4. #4
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    I coulnt come up with anything better than this....

    Code:
    Dim st As Stream = File.OpenRead("D:\Test.txt")
    Dim sr As New StreamReader(st)
    Dim a(1, 21) As String
    Dim temp As String
    Dim c As Integer = 0
    sr.BaseStream.Seek(0, SeekOrigin.Begin)
    While (sr.Peek() > -1)
        temp = sr.ReadLine()
        a(0, c) = temp.Substring(0, temp.IndexOf(","c))
        a(1, c) = temp.Substring(temp.IndexOf(","c) + 2)
        c += 1
    End While
    Theres probably a smother way to do it but I'm more into beers than VB at the moment.

    I'll get back if I find something beter in the morning.

    /Leyan

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    This is in C#, but the concepts are pretty much the same. I use this to read in a file and add each line of the file to a listbox.

    Code:
    try
    {
    	string sTemp;
    	string sTemp1;
    
    	// Create the path to the file.
    	sTemp = Application.StartupPath;
    	sTemp += @"\selected.typ";
    
    	if(File.Exists(sTemp))
    	{
    		// Clear the list box
    		lbSearchForTypes.Items.Clear();
    
    		// Create a fileinfo object and open it into a stream.
    		FileInfo fiSource = new FileInfo(sTemp);
    		StreamReader stream = fiSource.OpenText();
    					
    		// Read the first line of the file.
    		sTemp1 = stream.ReadLine();
    		do
    		{
    			// Add the file to the list.
    			lbSearchForTypes.Items.Add(sTemp1);
    
    			// Read the next line in.
    			sTemp1 = stream.ReadLine();
    
    			// Check to see if we are at end of file.  If not, loop.
    		}while (sTemp1 != null);
    	}
    }
    catch (System.Exception e)
    {
    	// This is here for debug purposes.  May be left in the app for release though.
    	MessageBox.Show(e.Message.ToString());
    }

  6. #6
    New Member
    Join Date
    Nov 2002
    Posts
    5

    List control population

    I have written two funtions to populater list controls from both databases and text files. Hope these help :

    Public Function PopulateListControlFromFile(ByVal ListControl As Object, _
    ByVal FullPath As String, Optional ByVal Delimiter As String = vbCrLf) As Boolean

    Dim objFso As New Scripting.FileSystemObject()

    Dim objTextStream As Scripting.TextStream
    Dim strContents As String
    Dim arrContents() As String
    Dim lCtr As Long, lCount As Long

    Try
    'Ensure file exists
    If Dir(FullPath) = "" Then
    MsgBox("The file " & FullPath & " does not exist", MsgBoxStyle.OKOnly, "Load contract lists")
    Exit Function
    End If

    'Get file contents
    objTextStream = objFso.OpenTextFile(FullPath, Scripting.IOMode.ForReading)
    strContents = objTextStream.ReadAll
    objTextStream.Close()

    'Split file contents based on delimiter
    arrContents = Split(strContents, Delimiter)
    lCount = UBound(arrContents)

    'Clear List Control.
    ListControl.Items.Clear()

    'Populate list control
    ListControl.Items.AddRange(arrContents)

    PopulateListControlFromFile = True

    Catch ex As Exception
    objTextStream = Nothing
    objFso = Nothing
    MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "Load contract lists")
    End Try

    End Function

    and to do the same from a database:

    Public Function PopulateListControlFromDb(ByVal ListControl As Object, _
    ByVal Database As String, ByVal DataTable As String, ByVal DataColumn As String) As Boolean

    'connection stuff
    Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Database)
    Dim cm As New OleDb.OleDbCommand("SELECT * FROM" & " " & DataTable, con)
    Dim dr As OleDb.OleDbDataReader

    'empty ListControl first
    ListControl.Items.Clear()

    'read table and fill ListControl
    Try
    con.Open()
    dr = cm.ExecuteReader(CommandBehavior.CloseConnection)
    While dr.Read()
    ListControl.Items.Add(dr(DataColumn))
    End While
    Catch ex As Exception
    MessageBox.Show(ex.Source & ": " & ex.Message)
    Finally
    cm.Dispose()
    cm = Nothing
    con.Dispose()
    con = Nothing
    dr = Nothing
    End Try
    End Function

  7. #7
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question

    I think rgreen is missing something - what imports he uses.
    ~Peter


  8. #8
    New Member
    Join Date
    Nov 2002
    Posts
    5
    Imports ADODB
    Imports System.Data
    Imports System.Data.OleDb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width