|
-
Jun 13th, 2023, 11:32 AM
#1
Thread Starter
Addicted Member
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
-
Jun 17th, 2023, 01:56 PM
#2
Re: VB.NET Load Form TextBoxes from CSV file
When you say you're saving the form to a CSV every 30 minutes, you're saving whatever might be in the textboxes and whatever is in the grid below as well? So, when a text file is selected to restore the form, you need to fill these areas in?
I would traverse the controls on the form and if I encounter a textbox, save the name of the textbox and the contents (if it isn't empty) in the top of the file. Then a line-by-line duplicate of the grid in the bottom of the file.
The code to save would look something like this
Code:
Sub SaveFormContents()
Dim bkupFile As New IO.StreamWriter("F:\temp\" & Format(Now, "MMddyyHHmmss_backup") & ".txt")
Dim tb As TextBox
bkupFile.WriteLine("[TEXTBOXES]")
For Each c As Control In Me.Controls
If TypeOf c Is TextBox Then
tb = c
If tb.Text.Trim.Length > 0 Then
' the textbox contains data
bkupFile.WriteLine(tb.Name & "|" & tb.Text)
End If
End If
Next
bkupFile.WriteLine("[GRID]")
For Each c As DataGridViewColumn In DataGridView1.Columns
bkupFile.Write(c.HeaderText & "|")
Next
bkupFile.WriteLine("")
For Each r As DataGridViewRow In DataGridView1.Rows
For Each d As DataGridViewTextBoxCell In r.Cells
bkupFile.Write(d.Value & "|")
Next
bkupFile.WriteLine("")
Next
bkupFile.Close()
End Sub
This is my form and the text file I created. I wanted it to be a bit readable, so I added the headings and used "|" instead of "," in case any text contained a comma in it.
https://imgur.com/SYEIeWX
Then you'd reverse this by reading the text file, finding the textbox (me.Controls(name of control)) and placing the text back in it as well as writing the data back to the grid.
-
Jun 20th, 2023, 09:35 AM
#3
Thread Starter
Addicted Member
Re: VB.NET Load Form TextBoxes from CSV file
Thanks jdelano... I will play around with this and see how it works. I appreciate your time and effort on this.
-
Jun 21st, 2023, 10:02 AM
#4
Re: VB.NET Load Form TextBoxes from CSV file
You're welcome, I hope it helps you out.
-
Jun 21st, 2023, 01:25 PM
#5
Re: VB.NET Load Form TextBoxes from CSV file
It sounds like you're wanting to persist control values when the form closes. This is a simple enough requirement without using a CSV file, so the question I have is: are you only using a CSV file because this is what you're most familiar with or are you using a CSV file so that a user (or anyone) can edit the values outside of the application?
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|