I have been searching all over for a way to open a text file that was created using vb.net on one form and open that same text file and place the contents into a list view. The text file will have a description, a part number, and a quantity, this is for parts of a copier. This file will have multiple multiple parts (example fuser, drum, developer) each has their own part number, description, and quantities.

Here is the code that I used to make and save the file, I think I might need to change the code so I can have all three items on one line.

 Dim txt As String
        txt = TextBox1.Text
        My.Computer.FileSystem.CreateDirectory("C:\Copiers\" & txt & "\")

        Dim sFilename As String
        sFilename = ("C:\Copiers\" & txt & "\") & TextBox2.Text & ".txt"

        Dim i As Integer
        Dim aryText(3) As String

        aryText(0) = TextBox3.Text
        aryText(1) = TextBox4.Text
        aryText(2) = TextBox5.Text


        Dim objWriter As New System.IO.StreamWriter(sFilename, True)

        For i = 0 To 2
            objWriter.WriteLine(aryText(i))
        Next

        objWriter.Close()
        MsgBox("Text Appended To the File")

        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()

        If CheckBox1.Checked Then
            Me.Close()
        End If


    End Sub

But when I go to the first form I have two comboxes, one is to open the folder and the other displays the files in that folder. I need help to pull the information from the file and place the items in their respective columns. Everything I found is based on account and password and it is not working for me. I just need to open the file read the line and place them where they need to go.

Recommended Answers

All 4 Replies

Hello,

I use streamwriter/streamreader for writing and reading text files. To read files you need something like

s1="path and filename"

Dim rf As New StreamReader(s1)
linne=rf.readline
.
.
rf.close

For writing to files you need something like

Using outp As New StreamWriter(data_path$ & Convert.ToString("weight.dat"), True)

outp.writeline (var1,var2,.....)

outp.close

Don't forget you need an Imports System.IO at start of module.

Denis

Well in your file you will need a separator that will separates the fields so that you will know how to identify fields(columns) in your text file. I will make an example of this. Since you said your text file will have 3 columns(Description, Part Number, Quantity) you need a way of separating this like this: sample|123|3 now in this example I used | as a separator and my code would be like this to read this text file, remember that you will read the fields into array.

 If My.Computer.FileSystem.FileExist("c:\test.txt") Then
 Dim sFile As String = IO.File.ReadAllText("C:\test.txt")
 ' The above will help to read all lines but will not be able to read or split each line, at least that what happened to me because of ReadAllText.
 Dim f As New System.IO.StreamReader("c:\test.txt")
 Dim lines() As String = Split(sFile, vbCrLf)
 For Each line As string In lines
 Dim x As String = f.ReadLine
 ' Now this is for giving the fields
 If x <> String.Empty Then
 Dim myArray(3) As String
 ' This is the array that will contain the fields
 Dim xz As String = x.Split("|")
 ' Now we split or form a field on this identifier |
 myArray = xz
 ' Now you can call or assign or do what ever you want with the data read. Suppose you had 3 text boxs.
 txtDescr.Text = myArray(0)
 txtPartNum.Text = myArray(1)
 txtQuant.Text = myArray(2)
 End If
 Next
 x.Close

That a sample of how to read the data and split it.

I appreciate the help but I was able to locate something that would help me. I do have another problem that I am really having a hard time with. I have a textbox in my form that I am trying to add into autocomplete so when I get back into that form I can just type the first letter and have it populate.

Dim MySource As New AutoCompleteStringCollection()
        Dim add As String
        add = TextBox1.Text
        MySource.Add(add)


        TextBox1.AutoCompleteCustomSource = MySource
        TextBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource

when i close the form and go back in the textbox does not autocomplete.

Since this is a different question, you should mark this solved and start a new question. This way your new question is more liable to get the attention you want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.