Solved

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

Posted on 2010-09-14
7
317 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting multiple values from a dataset ASP.NET(VB) 3 49
VB.Net Report Printing Issue 3 33
vb.net help 4 30
Sending receiving text messages in vb.net 15 35
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It’s quite interesting for me as I worked with Excel using vb.net 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 …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

772 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