How can I populate a Combobox from reading in a test file.

Posted on 2010-09-14
Last Modified: 2012-06-21
I have a text file and I would like to read in information associated with it. If I were to use the readline file method and place the different components into a string array. How would I know which string array index would be placed in my combo box.

What are some alternative approaches?
Question by:cmdolcet
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 33675302
Using .NET:

You could have the input file in XML format and read it into a dataset, then use the dataset to populate the combobox. That would be easy, and clean.

If using XML, you could also use XMLReader to read the values one at a time and add them to the Combobox.

Expert Comment

ID: 33675709
Here is the code i am using to read and list the lines in combobox.

Dim file As String
        Dim tmp As String

        file = "idoklist.txt"
        Dim fs As FileStream = New FileStream (file, FileMode.Open)
        Dim r As StreamReader = New StreamReader (fs)

        While Not r.EndOfStream
            tmp = r.ReadLine
            ComboBox1.Items.Add (tmp)
        End While


Open in new window

LVL 19

Expert Comment

by:Shahan Ayyub
ID: 33685533

>>>How would I know which string array index would be placed in my combo box.

It depends on your file from which you reading, i.e., how the   information is saved and which part you are going to add in combobox. the 'Readline' statement will just read the whole line and add the line in the combobox(so what is meant by array index ???).

I think you should elaborate more along with the data storage pattern.


Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.


Author Comment

ID: 33685627
Its a simple flat file that im trying to read in.......The file looks like this:

G4249A562      Header - Rear Gap
G4249A562      D      -0.01      0      0.7      -0.7      
F4249A562      Header - Rear - Flush
F4249A562      D      0.01      0      0.7      -0.7      
G4249A560      Header - Middle Gap
G4249A560      D      -0.01      0      0.7      -0.7      
F4249A560      Header - Middle - Flush
F4249A560      D      0.01      0      0.7      -0.7      
G4249A558      Header - Front Gap
G4249A558      D      -0.01      0      0.7      -0.7      
F4249A558      Header - Front - Flush
F4249A558      D      0.01      0      0.7      -0.7      
G4249A494      Fender Line - Top Gap
G4249A494      D      -0.01      0      0.7      -0.7      
F4249A494      Fender Line - Top Flush
F4249A494      D      0.01      0      0.7      -0.7      
G4249A498      Fender Line - Bottom Gap
G4249A498      D      -0.01      0      0.7      -0.7      
F4249A498      Fender Line - Bottom Flush
F4249A498      D      0.01      0      0.7      -0.7      
G4249A644      Bottom - Front Gap
G4249A644      D      -0.01      0      0.7      -0.7      
F4249A644      Bottom - Front - Flush
F4249A644      D      0.01      0      0.7      -0.7

Author Comment

ID: 33692441
Ok I am using the following to Parse out my file....The one thing I noticed in the Parser is that doesn;t always parse out the same way. In the example below I have a simple text file setup as such:

####****#### Release Organization : BODY
 ####****#### Site : TEMP
 ####****#### Division : TEMP

When I use the code that parses out the information the first line will.
#### and then the next loop will be ****#### R and the next loop will be elease Orga

Why does this do it and how can I fix it so that I get:
Release Organization
Private Sub mnu_Import_File_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_Import_File.Click
        If Me.dlgOpenPartfile.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
            Dim FileProps As FileInfo = New FileInfo(dlgOpenPartfile.FileName)
            Me.lblFileName.Text = FileProps.FullName
            Me.lblFileLast_Accessed.Text = FileProps.LastAccessTime
        End If
    End Sub

    'Parses any File sent into the DTU software Program.
    Public Sub File_Parser()
        Dim stdFormat As Integer() = {5, 10, 11, -1}
        Dim errorFormat As Integer() = {5, 5, -1}
        Dim newstring As String
        Using MyReader As New FileIO.TextFieldParser(dlgOpenPartfile.FileName)
            MyReader.TextFieldType = FileIO.FieldType.FixedWidth
            MyReader.FieldWidths = stdFormat
            Dim currentRow As String()
            While Not MyReader.EndOfData
                    Dim rowType = MyReader.PeekChars(3)
                    If String.Compare(rowType, "Err") = 0 Then
                        ' If this line describes an error, the format of the row will be different.
                        ' Otherwise parse the fields normally
                    End If
                    currentRow = MyReader.ReadFields
                    For Each newstring In currentRow
                        Console.Write(newstring & "|")
                Catch ex As FileIO.MalformedLineException
                    MsgBox("Line " & ex.Message & " is invalid.  Skipping")
                End Try
            End While
        End Using
    End Sub

Open in new window


Author Comment

ID: 33702446
Shahan_Developer: Does the code make sense?
LVL 19

Accepted Solution

Shahan Ayyub earned 125 total points
ID: 33707542

The problem could be this variable 'stdFormat' you did not set the correct values.

See this article written by EMoreau

read this heading specifically: Processing a fixed-width file

You can read the whole article as well.

As mention in this line of YOUR code:
Dim stdFormat As Integer() = {5, 10, 11, -1}

This reads first 5 characters then 10 characters then 11 and variable length characters.

I believe correction in this line can help you.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question