Results 1 to 9 of 9

Thread: Read CSV Data into textbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    3

    Unhappy Read CSV Data into textbox

    Hello,

    I want to be able to load & read (and later edit) .csv files into a textbox in Visual Basic 2008, If possible.

    I don't know anything about this at all, but i do know a quite a bit about visual basic..
    You would normally post the code you already have around about now, but i don't have any.. yet

    Any help/code would be really appreciate..

    Thanks!

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Read CSV Data into textbox

    Welcome to VBForums

    Thread moved from 'VB6 and Earlier' forum to 'VB.Net' (VB2002 and later) forum

  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Read CSV Data into textbox

    If you simply want the entire file contents in a textbox, you can do this:

    Code:
            TextBox1.Text = System.IO.File.ReadAllText("C:\Temp\file.csv")
    If you want more control over how the data is processed, then use the TextFieldParser

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Read CSV Data into textbox

    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    3

    Re: Read CSV Data into textbox

    Okay, i have this:

    Code:
            Dim sr As New StreamReader("C:\Users\Luke\Desktop\Test1.csv")
            Dim linesInFile As Integer
            Do Until sr.EndOfStream = True
                linesInFile = linesInFile + 1
                TextBox1.Text = TextBox1.Text + sr.ReadLine & vbNewLine
            Loop
    That brings up Test1.csv into Textbox1... But how would i know separate the data. I know it is using the Split() command but i don't know how to use it..
    So lets say inside the csv i have:

    Bob,Jones,bob@hotmail.com,48

    it is formatted there:
    first name,second name,email,age

    if i have 4 textboxes, how could i get all the information in a different textbox..
    textbox1 would contain firstname
    textbox2 would contain secondname
    etc?

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Read CSV Data into textbox

    It is fairly simple, one line of code to populate the TextBox

    This example populates a TextBox where the data format is shown below the code.

    Code:
            TextBox1.Lines = _
                ( _
                    From line In IO.File.ReadAllLines("States.txt") _
                    Where line.Length > 0 _
                    Let Items = line.Split(","c) _
                    Select SingleState = Items(0) & " - " & Items(1)).ToArray
    Sample content
    Code:
    ALABAMA,AL
    ALASKA,AK
    AMERICAN SAMOA,AS
    ARIZONA,AZ
    ARKANSAS,AR
    CALIFORNIA,CA
    COLORADO,CO
    CONNECTICUT,CT
    DELAWARE,DE
    DISTRICT OF COLUMBIA,DC
    FEDERATED STATES OF MICRONESIA,FM
    A somewhat more detailed example which shows how you might display the data in a DataGridView. Note Select New is used rather than Select.
    Code:
            Dim States = (From line In IO.File.ReadAllLines("States.txt") _
                          Let Items = line.Split(","c) _
                          Select New StateCodes(Items(0), Items(1))).ToDataTable
    
            bsStates.DataSource = States
    
            DataGridView1.DataSource = bsStates
    Extension for the above
    Code:
    Module Extensions
        <System.Runtime.CompilerServices.Extension()> _
        Public Function ToDataTable(Of T) _
        (ByVal value As IEnumerable(Of T)) As DataTable
    
            Dim returnTable As New DataTable
            Dim firstRecord = value.First
    
            For Each pi In firstRecord.GetType.GetProperties
                returnTable.Columns.Add(pi.Name, _
                                        pi.GetValue(firstRecord, Nothing).GetType)
            Next
    
            For Each result In value
                Dim nr = returnTable.NewRow
                For Each pi In result.GetType.GetProperties
                    nr(pi.Name) = pi.GetValue(result, Nothing)
                Next
                returnTable.Rows.Add(nr)
            Next
    
            Return returnTable
    
        End Function
    
    End Module

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    3

    Red face Re: Read CSV Data into textbox

    Thanks guys, i have it reading a text file.
    I have a listbox, and if someone clicks, say, Bob on the listbox, it reads a line from the file, and if myCSVArray(0) is not Bob, then read the next line.. untill it is Bob, then display the data, split at each comma.

    But when it reaches the end of the file, and i search for something at the top of file, it carries on trying to read the next line. Which, of course, is nothing, because it has reached EOF.
    I Have:

    Code:
    If sr.EndOfStream = True Then
    
    
    End If

    How would i get it to start at the top of the file if sr.EndOfStream = true ?

    - Thnks

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Read CSV Data into textbox

    Quote Originally Posted by Lukeshepp View Post
    How would i get it to start at the top of the file if sr.EndOfStream = true ?
    Generally speaking, you wouldn't. Using a StreamReader, you would normally read from the beginning to end of the file and that's that.

    One option you do have is to set the Position of the StreamReader's BaseStream back to zero, which puts you back at the beginning of the file again. I would recommend against that though.

    If you want to access the data more then once then don't read it line by line. Call File.ReadAllLines to read all the data into a String array. You can then loop over that array as many times as you like, or access the data in a completely random manner.

  9. #9
    New Member
    Join Date
    Aug 2016
    Posts
    1

    Re: Read CSV Data into textbox

    Quote Originally Posted by Lukeshepp View Post
    Hello,

    I want to be able to load & read (and later edit) .csv files into a textbox in Visual Basic 2008, If possible.

    I don't know anything about this at all, but i do know a quite a bit about visual basic..
    You would normally post the code you already have around about now, but i don't have any.. yet

    Any help/code would be really appreciate..

    Thanks!



    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    OpenFileDialog1.Title = "Please Select a File"
    OpenFileDialog1.InitialDirectory = "C:\"
    OpenFileDialog1.ShowDialog()
    End Sub

    Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
    TextBox2.Text = OpenFileDialog1.FileName.ToString()
    TextBox1.Text = System.IO.File.ReadAllText(TextBox2.Text)



    You open your csv file and it will display in textbox 1 , remember to activate multiline on textbox 1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width