Solved

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

Posted on 2010-09-14
7
302 Views
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?
0
Comment
Question by:cmdolcet
7 Comments
 
LVL 1

Expert Comment

by:golftech
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.
0
 
LVL 2

Expert Comment

by:ozi_lion
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)

        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
 
LVL 19

Expert Comment

by:Shahan Ayyub
ID: 33685533
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:cmdolcet
ID: 33685627
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
 

Author Comment

by:cmdolcet
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
 :
 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
 

Author Comment

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

Accepted Solution

by:
Shahan Ayyub earned 125 total points
ID: 33707542
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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now