• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

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

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?
0
cmdolcet
Asked:
cmdolcet
1 Solution
 
golftechCommented:
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.
0
 
ozi_lionCommented:
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)

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

        r.Close()
        fs.Close()

Open in new window

0
 
Shahan AyyubSenior Software Engineer - iOSCommented:
Hi!

>>>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.

-Shahan

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
cmdolcetAuthor Commented:
Shahan_Developer:
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
0
 
cmdolcetAuthor Commented:
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
 :
 BODY
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
        File_Parser()
    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
                Try
                    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.
                        MyReader.SetFieldWidths(errorFormat)
                    Else
                        ' Otherwise parse the fields normally
                        MyReader.SetFieldWidths(stdFormat)
                    End If
                    currentRow = MyReader.ReadFields
                    For Each newstring In currentRow
                        Console.Write(newstring & "|")
                    Next
                    Console.WriteLine()
                Catch ex As FileIO.MalformedLineException
                    MsgBox("Line " & ex.Message & " is invalid.  Skipping")
                End Try
            End While
        End Using
        Console.ReadLine()
    End Sub

Open in new window

0
 
cmdolcetAuthor Commented:
Shahan_Developer: Does the code make sense?
0
 
Shahan AyyubSenior Software Engineer - iOSCommented:
Hi!

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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now