Results 1 to 5 of 5

Thread: VB.NET Load Form TextBoxes from CSV file

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    VB.NET Load Form TextBoxes from CSV file

    Hi All - So this is my first attempt at doing something of this nature. I have a form with many textboxes that acts as a real-time hour by hour schedule. A user will fill in portions of the form based on the time of day shown on the form (see attached). I am saving the form contents about every 30 minutes in the event the form closes etc. I want to create an easy method for loading the data back to the form.

    In the CSV file screenshot, all of the green boxes have an associated textbox on the form. The format of the entire CSV is what I am saving out from the form so a Supervisor can view it when needed. The sample code I have here 'seems' like a good start, but I could also be way off on what is actually needed. So how can I load the textboxes efficiently and be able to ignore the empty ones?

    Sample Code:

    Code:
        Public Sub ReadCSVFile()
    
            Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(CStr(fpath & "\PROX CLR.csv"))
                MyReader.TextFieldType = FileIO.FieldType.Delimited
                MyReader.SetDelimiters(",")
                Dim currentRow As String()
                While Not MyReader.EndOfData
                    Try
                        currentRow = MyReader.ReadFields()
                        Dim currentField As String
                        For Each currentField In currentRow
                            MsgBox(currentField)
                        Next
                    Catch ex As Microsoft.VisualBasic.
                        FileIO.MalformedLineException
                        MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
                    End Try
                End While
            End Using
        End Sub
    Here is what I am using now, but does not load textboxes properly if the data is more than one line.

    Code:
        Public Sub LoadDatatoForm()
    
            Dim PickFile As String
    
            Using dialog As New OpenFileDialog
                dialog.InitialDirectory = fpath
                dialog.Filter = "CSV Files|*.csv"
    
                If dialog.ShowDialog() = DialogResult.OK Then
                    PickFile = dialog.FileName
                End If
            End Using
    
            Dim tfp As New TextFieldParser(PickFile)
            tfp.Delimiters = New String() {","}
            tfp.TextFieldType = FieldType.Delimited
            tfp.ReadLine() ' skip header
    
            While tfp.EndOfData = False
    
                Dim fields = tfp.ReadFields()
                CMB4a.Text = fields(4)
                PLHrs4a.Text = fields(5)
                PLHrsTot4a.Text = fields(6)
                ACHrs4a.Text = fields(7)
                ACHrsTot4a.Text = fields(8)
                VarLaborHrly4a.Text = fields(9)
                VarLaborCum4a.Text = fields(10)
                PLUnitsHrly4a.Text = fields(11)
                PLUnitsTot4a.Text = fields(12)
                ActUnitTot4a.Text = fields(13)
                ActUnitRunTot4a.Text = fields(14)
                VarActHrly4a.Text = fields(15)
                VarActCum4a.Text = fields(16)
                Reason4a.Text = fields(17)
    
            End While
    
        End Sub
    Attached Images Attached Images   

Tags for this Thread

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