VB.Net - Writing to textfile from a textbox - vb.net

Hey guys, just another little problem here! Trying to write a quiz for a college portfolio and having trouble with writing to a .txt textfile. On one form(form4.vb), I have a listbox that picks up the information held within a notepad textfile called "usernames" which contains names of quiz users. When written in manually to this textfile, my listbox picks it up fine, however, on a different form(form3.vb), I have a textbox where a user inputs their name, this is supposed to go to the "usernames.txt" textfile to be picked up by the listbox on the other form but instead, it does not write anything at all and if there is already text on this textfile, it wipes it all out.
I also have to use the application.startup path instead of the usual C:\my documentents\ etc so i would have to begin with something like this: (Note: code is a little mixed up due to messing around with different variations but this is just a example)
'Try
' Dim appPath As String
' Dim fileName As String
' appPath = Application.StartupPath
' fileName = appPath & "\usernames.txt"
' sWriter = New System.IO.StreamWriter(fileName)
' sWriter.Close()
' MessageBox.Show("Writing file to disk")
'Catch ex As Exception
' MessageBox.Show("File Access Error", "Error")
'End Try
'MessageBox.Show("Program terminating")
'Application.Exit()
Hope someone can help! =)

You want something more like this:
Dim appPath As String = Application.StartupPath
Dim fileName As String = IO.Path.Combine(appPath, "usernames.txt")
Try
IO.File.AppendAllText(fileName, TextBox1.Text & Environment.NewLine)
Catch ex As Exception
MessageBox.Show("File Access Error", "Error")
End Try
MessageBox.Show("Program terminating")
Environment.Exit()
Some things worth noting in this code:
Path.Combine() as the correct way to add the separator character
File.AppendAllText() is much easier for simple things than messing with streamreader/writer. Pair it with File.ReadAllText() or File.ReadAllLines() in the other direction.
Environment.Exit() vs Application.Exit()

Where is your dim statement for sWriter (streamWriter)?

Related

vb.net search directory for files containing *.G(num) but NOT *.GP(num)

I'm fairly familiar with bash, but I'm very, ***very**** new to vb.net. I'm searching for an easy way to find files in a folder that end with .G1, .G2, .G3, etc. but NOT .GP1, .GP2, .GP3, etc. Then for each file I need to copy it to another folder using a different file name but the same extension. I've managed to figure this out for the unique files, but there will be an undefined number of these depending on the project and I need to make sure that I get them all. Hard coding is possible, but very, very ugly. Any suggestions?
Here's the remnants of a failed attempt:
Public Sub FindGFiles()
FileList = IO.Directory.GetFiles(searchDir, ".G[1-99]" + , IO.SearchOption.AllDirectories)
For Each foundfile As String In FileList
If foundfile.Contains(".G#") Then
'copy file somehow and retain file extension
Else
MsgBox("No match")
End If
Next
End Sub
The GetFiles-method does only support * and ? wildcard characters.
So you have to get all files with a *.G*-extension first.
In the For Each-loop one can then use the Like-operator to check the desired pattern:
Public Sub CopyGFiles(searchDir As String, destDir As String)
Dim fileList As String() = IO.Directory.GetFiles(searchDir, "*.G*", IO.SearchOption.AllDirectories)
Dim fileName As String
Dim extension As String
For Each foundfile As String In fileList
fileName = IO.Path.GetFileNameWithoutExtension(foundfile)
extension = IO.Path.GetExtension(foundfile)
If extension Like ".G#" OrElse
extension Like ".G##" Then
'copy file to destination, append "_new" to the filename and retain file extension
IO.File.Copy(foundfile, IO.Path.Combine(destDir, fileName & "_new" & extension))
Else
'pattern not matched
End If
Next
End Sub
The method-call would then be as follows:
CopyGFiles("C:\Temp", "C:\Temp\Dest")
This should be done inside a Try/Catch as different exceptions can occur when working with files.
Try
CopyGFiles("C:\Temp", "C:\Temp\Dest")
Catch ex As Exception
MessageBox.Show("An error occured" + vbCrLf + ex.Message)
End Try

Read CSV to series of existing textbox in Vb.net

I have a code to import csv to auto generated text box which was a part of my previous app. However I had to re do the whole script which involves importing csv to multiple existing textbox.
Below is my old code which worked like a charm but in this code my textbox were getting auto generated based on the numbers of value present in my csv.
Dim T(100) As TextBox
Using ofd As New OpenFileDialog()
If ofd.ShowDialog() = DialogResult.OK Then
TextBox1.Text = (ofd.FileName)
End If
Using MyReader As New Microsoft.VisualBasic.
FileIO.TextFieldParser(TextBox1.Text)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim numer As Integer
Dim currentRow As String()
numer = 1
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
If (currentField IsNot "") Then
Dim myTB As New TextBox
T(numer) = myTB
myTB.Text = currentField
myTB.Visible = True
myTB.Location = New Point(550, 92 + (numer * 28))
myTB.Name = "ADBox" + numer.ToString
myTB.ReadOnly = True
Me.Controls.Add(myTB)
numer += 1
End If
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 Using
But that created a lot of issue in my app hence I had to load the values on an existing textboxes(Multiple) but I am somehow not able to.
Edit1:
*** The code above creates a textbox and adds my csv values to it and what I am looking for is inject csv to existing textbox which I have created and not automatically generated text box.
For Example my code creates text box called ADUser1,2,3,4 and enters all the value but my following code which will create a textfile by fetching the values from the text box is not working because when I declare
My.Computer.FileSystem.WriteAllText(aduser1, ADBox1.Text, True)
it says it doesn't exists because when a form loads it never created such textboxes. This is the challenge I am facing
Any help will be a great value
Thanks
I agree that this is a very awkward design and should be redone, but for the purpose of answering your question...
The reason your code here: My.Computer.FileSystem.WriteAllText(aduser1, ADBox1.Text, True) doesn't find ADBox1 is that it is not created and referenced like an object you drag on to the form. It could be, by the way, but that is more work than dragging and naming 100+ text boxes on your form. Nuts. Creating the textboxes in code is better.
If you "manually" add one textbox to a form, then examine the designer-generated code for the form you will see that it created a textbox for you. You would find something similar to Friend WithEvents ADBox1 As System.Windows.Forms.TextBox. This is the reason you can reference the textbox in your form code. There is no magic here and you are technically doing the same thing in your code, here:
Dim T(100) As TextBox
...
Dim myTB As New TextBox
T(numer) = myTB
You can use the reference T(n) to refer to any of your textboxes. It is not clear where the WriteAllText function is but you may need to be sure Dim T(100) As TextBox is a form global, then change the WriteAllText line like this to get at ADBox1:
My.Computer.FileSystem.WriteAllText(aduser1, T(1).Text, True)

Using an array to search a file VB

I have a program that needs to look through a text file line by line, the lines look like this:
10-19-2015 Brett Reinhard All Bike Yoga Run the Studio Design Your Own Strength
These are separated by tabs in the text file.
What I want to do is look at the second value, in this case "Brett Reinhard" and move the full line to another textfile called "Brett Reinhard"
I was thinking of using an array to check to see if the second 'column' in the line matched any value within a given array, if it does I want to perform a specific action.
The way I am thinking of doing this is with a For/next statement, now while it will work it will be a laborious process for the computer that I will be using it on.
The code I am thinking of using looks like this:
For intCounter=0 to Whatever Number is the last number of the array
If currentfield.contains(array(intCounter)) Then
Open StreamWriter(File directory & array(intcounter) & ".txt")
Streamwriter.Writeline(currentfield)
End IF
Is there a better way of doing this, such as referencing the second 'column' in the line, similar to the syntax used in VBA for excel.
Name=Cells(1,2).Value
If you can guarantee that a line will only use the tab characters as field separators, you can do something along this:
Open the stream for reading text
Open a stream for writing text
Read a line of text
Use the Split method to break the incoming line into an array of fields
If the second element in the array is your sentinel value, write the original line to the writer
Repeat yourself until you have reached the end of file (ReadLine will return Nothing, or null for those c# folk).
Close and dispose of your stream objects.
If you aren't sure of the format, you will want to take the hit and use the TextFieldParser as mentioned in an earlier comment.
So while its not using an array to search a file, what I ended up doing works just as well. I ended up using the split method thanks to #Martin Soles.
Here is what I came up with:
Sub Main()
Dim intCount As Integer = 1
Dim words As String
Dim split As String()
Using MyReader As New Microsoft.VisualBasic.
FileIO.TextFieldParser(
"I:\Games, Events, & Promotions\FRP\Back End\Approved.txt")
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
words = currentField
split = words.Split(New [Char]() {CChar(vbTab)})
For Each s As String In split
If intCount = 2 Then
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("I:\Games, Events, & Promotions\FRP\Back End\" & s & ".txt", True)
file.WriteLine(currentField)
file.Close()
End If
intCount = intCount + 1
Next s
intCount = 1
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 'Main
Thank you guys for the suggestions.
For right now the split method will work for what is needed.

Try catch if selected excel file is blank

Hello I have a program that imports an excel file using the oledb in vb.net. This program imports the excel spreadsheet into a datagridview. On opening the program will ask the user to choose an excel file to open, however if the user inputs nothing or presses the cancel button the program will crash. I'm trying to find a way to prevent a user from canceling or leaving the excel filename blank. I'm hoping this will be done using a try catch block but I'm not exactly familiar with try catch in vb.net. If anyone has any suggestions or solutions for this I would greatly appreciate it. This is what I found on MSDN.
If System.IO.File.Exists(filePath) = False Then
Console.Write("File Not Found: " & filePath)
Else
' Open the text file and display its contents.
Dim sr As System.IO.StreamReader =
System.IO.File.OpenText(filePath)
Console.Write(sr.ReadToEnd)
sr.Close()
End If
You could try a Try....Catch loop. Check this site out, should give you what you are looking for.
You could possibly use the Try....Catch loop and on the catch have an error message saying they must fill in the blank textbox.
I would suggest to take care of this at the source, when opening the file, and catching errors after the fact should be a last resort.
If filePath <> String.Empty And System.IO.File.Exists(filePath) Then
Try
'handle the opening of the file
Catch ex As Exception
Console.Write(ex.Message)
End Try
ElseIf filePath = String.Empty Then
Console.Write("Nothing Entered")
Else
Console.Write("File Not Found: " & filePath)
End If

Alternate ways of read, write data from multiple forms using vb.net

I am working on an application for work. We use an excel file to create reports. I am rusty in coding and really have not faced such a large request. The way the application works is there is standard toolbar. When the user opens the application he is presented with blank form. They must first create a new file and save it to their folder of choice. I have used the stream reader/writer but the problem is there is large amounts of data that user has to fillout so my code is horrible and monster like and I was wondering is there an easier way?
ToolStripLabel1.Text = FileDataStorage.OpenFileTextBox1.Text
If ToolStripLabel1.Text = ("C:\Temp\New QCA.qca") Then
MsgBox("Must Save New file first ACCESS DENIED!")
End If
Dim FILE_NAME As String = FileDataStorage.OpenFileTextBox1.Text
Try
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
objWriter.WriteLine(General_Data.GTextBox1.Text)
objWriter.WriteLine(General_Data.GTextBox2.Text)
objWriter.WriteLine(General_Data.GTextBox3.Text)
objWriter.WriteLine(General_Data.GTextBox4.Text)
objWriter.WriteLine(General_Data.GTextBox5.Text)
objWriter.WriteLine(General_Data.GTextBox6.Text)
objWriter.WriteLine(General_Data.GTextBox7.Text)
objWriter.WriteLine(General_Data.GTextBox8.Text)
objWriter.WriteLine(General_Data.GTextBox9.Text)
objWriter.WriteLine(General_Data.GTextBox10.Text)
objWriter.WriteLine(General_Data.GTextBox11.Text)
objWriter.WriteLine(Inventory.ComboBox1.Text)
objWriter.WriteLine(Inventory.ComboBox2.Text)
objWriter.WriteLine(Inventory.ComboBox3.Text)
objWriter.WriteLine(Inventory.ComboBox4.Text)
objWriter.WriteLine(Inventory.ComboBox5.Text)
objWriter.WriteLine(Inventory.ComboBox6.Text)
objWriter.WriteLine(Inventory.ComboBox7.Text)
objWriter.WriteLine(Inventory.ComboBox8.Text)
objWriter.WriteLine(Inventory.ComboBox9.Text)
objWriter.WriteLine(Inventory.ComboBox10.Text)
objWriter.WriteLine(Inventory.ComboBox11.Text)
objWriter.WriteLine(Inventory.ComboBox12.Text)
objWriter.WriteLine(Inventory.ComboBox13.Text)
objWriter.WriteLine(Inventory.ComboBox14.Text)
objWriter.WriteLine(Inventory.ComboBox15.Text)
objWriter.WriteLine(Inventory.ComboBox16.Text)
objWriter.WriteLine(Inventory.ComboBox17.Text)
objWriter.WriteLine(Inventory.ComboBox18.Text)
objWriter.WriteLine(Inventory.ComboBox19.Text)
objWriter.WriteLine(Inventory.ComboBox20.Text)
objWriter.WriteLine(Inventory.TextBox1.Text)
objWriter.WriteLine(Inventory.TextBox2.Text)
objWriter.WriteLine(Inventory.TextBox3.Text)
objWriter.WriteLine(Inventory.TextBox4.Text)
objWriter.WriteLine(Inventory.TextBox5.Text)
objWriter.WriteLine(Inventory.TextBox6.Text)
objWriter.WriteLine(Inventory.TextBox7.Text)
objWriter.WriteLine(Inventory.TextBox8.Text)
objWriter.WriteLine(Inventory.TextBox9.Text)
objWriter.WriteLine(Inventory.TextBox10.Text)
objWriter.WriteLine(Inventory.TextBox11.Text)
objWriter.WriteLine(Inventory.TextBox12.Text)
objWriter.WriteLine(Inventory.TextBox13.Text)
objWriter.WriteLine(Inventory.TextBox14.Text)
objWriter.WriteLine(Inventory.TextBox15.Text)
objWriter.WriteLine(Inventory.TextBox16.Text)
objWriter.WriteLine(Inventory.TextBox17.Text)
objWriter.WriteLine(Inventory.TextBox18.Text)
objWriter.WriteLine(Inventory.TextBox19.Text)
objWriter.WriteLine(Inventory.TextBox20.Text)
objWriter.WriteLine(Inventory.TextBox21.Text)
objWriter.WriteLine(Inventory.TextBox22.Text)
objWriter.WriteLine(Inventory.TextBox23.Text)
objWriter.WriteLine(Inventory.TextBox24.Text)
objWriter.WriteLine(Inventory.TextBox25.Text)
objWriter.WriteLine(Inventory.TextBox26.Text)
objWriter.WriteLine(Inventory.TextBox27.Text)
objWriter.WriteLine(Inventory.TextBox28.Text)
objWriter.WriteLine(Inventory.TextBox29.Text)
objWriter.WriteLine(Inventory.TextBox30.Text)
objWriter.WriteLine(Inventory.TextBox31.Text)
objWriter.WriteLine(Inventory.TextBox32.Text)
objWriter.WriteLine(Inventory.TextBox33.Text)
objWriter.WriteLine(Inventory.TextBox34.Text)
objWriter.WriteLine(Inventory.TextBox35.Text)
objWriter.WriteLine(Inventory.TextBox36.Text)
objWriter.WriteLine(Inventory.TextBox37.Text)
objWriter.WriteLine(Inventory.TextBox38.Text)
objWriter.WriteLine(Inventory.TextBox39.Text)
objWriter.WriteLine(Inventory.TextBox40.Text)
objWriter.WriteLine(Inventory.TextBox41.Text)
objWriter.WriteLine(Inventory.TextBox42.Text)
objWriter.WriteLine(Inventory.TextBox43.Text)
objWriter.WriteLine(Inventory.TextBox44.Text)
objWriter.WriteLine(Inventory.TextBox45.Text)
objWriter.WriteLine(Inventory.TextBox46.Text)
objWriter.WriteLine(Inventory.TextBox47.Text)
objWriter.WriteLine(Inventory.TextBox48.Text)
objWriter.WriteLine(Inventory.TextBox49.Text)
objWriter.WriteLine(Inventory.TextBox50.Text)
objWriter.WriteLine(Inventory.TextBox51.Text)
objWriter.WriteLine(Inventory.TextBox52.Text)
objWriter.WriteLine(Inventory.TextBox53.Text)
objWriter.WriteLine(Inventory.TextBox54.Text)
objWriter.WriteLine(Inventory.TextBox55.Text)
objWriter.WriteLine(Inventory.TextBox56.Text)
objWriter.WriteLine(Inventory.TextBox57.Text)
objWriter.WriteLine(Inventory.TextBox58.Text)
objWriter.WriteLine(Inventory.TextBox59.Text)
objWriter.WriteLine(Inventory.TextBox60.Text)
objWriter.WriteLine(Inventory.TextBox61.Text)
objWriter.WriteLine(Inventory.TextBox62.Text)
objWriter.WriteLine(Inventory.TextBox63.Text)
objWriter.WriteLine(Inventory.TextBox64.Text)
objWriter.WriteLine(Inventory.TextBox65.Text)
objWriter.WriteLine(Inventory.TextBox66.Text)
objWriter.WriteLine(Inventory.TextBox67.Text)
objWriter.WriteLine(Inventory.TextBox68.Text)
objWriter.WriteLine(Inventory.TextBox69.Text)
objWriter.WriteLine(Inventory.TextBox70.Text)
objWriter.WriteLine(Inventory.TextBox71.Text)
objWriter.WriteLine(Inventory.TextBox72.Text)
objWriter.WriteLine(Inventory.TextBox73.Text)
objWriter.WriteLine(Inventory.TextBox74.Text)
objWriter.WriteLine(Inventory.TextBox75.Text)
objWriter.WriteLine(Inventory.TextBox76.Text)
objWriter.WriteLine(Inventory.TextBox77.Text)
objWriter.WriteLine(Inventory.TextBox78.Text)
objWriter.WriteLine(Inventory.TextBox79.Text)
objWriter.WriteLine(Inventory.TextBox80.Text)
objWriter.WriteLine(Water_QC.ComboBox1.Text)
objWriter.WriteLine(Water_QC.TextBox1.Text)
objWriter.WriteLine(Water_QC.TextBox2.Text)
objWriter.WriteLine(Water_QC.TextBox3.Text)
objWriter.WriteLine(Water_QC.TextBox4.Text)
objWriter.WriteLine(Water_QC.TextBox5.Text)
objWriter.WriteLine(Water_QC.ComboBox2.Text)
objWriter.WriteLine(Water_QC.TextBox6.Text)
objWriter.WriteLine(Water_QC.TextBox7.Text)
objWriter.WriteLine(Water_QC.TextBox8.Text)
objWriter.WriteLine(Water_QC.TextBox9.Text)
objWriter.WriteLine(Water_QC.TextBox10.Text)
objWriter.WriteLine(Water_QC.TextBox11.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox1.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox2.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox3.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox4.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox5.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox6.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox7.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox8.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox9.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox10.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox11.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox12.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox13.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox14.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox15.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox16.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox17.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox18.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox19.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox20.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox21.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox22.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox23.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox24.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox25.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox26.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox27.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox28.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox29.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox30.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox31.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox32.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox33.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox34.Text)
objWriter.WriteLine(Base_Gel_QC.TextBox35.Text)
objWriter.WriteLine(Acid_QC.TextBox1.Text)
objWriter.WriteLine(Acid_QC.TextBox2.Text)
objWriter.WriteLine(Acid_QC.TextBox3.Text)
objWriter.WriteLine(Acid_QC.TextBox4.Text)
objWriter.WriteLine(Acid_QC.TextBox5.Text)
objWriter.WriteLine(Acid_QC.TextBox6.Text)
objWriter.WriteLine(Acid_QC.TextBox7.Text)
objWriter.WriteLine(Acid_QC.TextBox8.Text)
objWriter.WriteLine(Acid_QC.TextBox9.Text)
objWriter.WriteLine(Acid_QC.TextBox10.Text)
objWriter.WriteLine(Acid_QC.TextBox11.Text)
objWriter.WriteLine(Acid_QC.TextBox12.Text)
objWriter.WriteLine(Acid_QC.TextBox13.Text)
objWriter.WriteLine(Acid_QC.TextBox14.Text)
objWriter.WriteLine(Acid_QC.TextBox15.Text)
objWriter.WriteLine(Acid_QC.TextBox16.Text)
objWriter.WriteLine(Acid_QC.TextBox17.Text)
objWriter.WriteLine(Acid_QC.TextBox18.Text)
objWriter.WriteLine(Acid_QC.TextBox19.Text)
objWriter.WriteLine(Acid_QC.TextBox20.Text)
objWriter.WriteLine(Acid_QC.TextBox21.Text)
objWriter.WriteLine(Acid_QC.TextBox22.Text)
objWriter.WriteLine(Acid_QC.TextBox23.Text)
objWriter.WriteLine(Acid_QC.TextBox24.Text)
objWriter.WriteLine(Acid_QC.TextBox25.Text)
objWriter.WriteLine(Acid_QC.TextBox26.Text)
objWriter.WriteLine(Acid_QC.TextBox27.Text)
objWriter.WriteLine(Acid_QC.TextBox28.Text)
objWriter.WriteLine(Acid_QC.TextBox29.Text)
objWriter.WriteLine(Acid_QC.TextBox30.Text)
objWriter.WriteLine(Acid_QC.TextBox31.Text)
objWriter.WriteLine(Acid_QC.TextBox32.Text)
objWriter.WriteLine(Acid_QC.TextBox33.Text)
objWriter.WriteLine(Acid_QC.TextBox34.Text)
objWriter.WriteLine(Acid_QC.TextBox35.Text)
objWriter.WriteLine(Acid_QC.TextBox36.Text)
objWriter.WriteLine(Acid_QC.TextBox37.Text)
objWriter.WriteLine(Acid_QC.TextBox38.Text)
objWriter.WriteLine(Acid_QC.TextBox39.Text)
objWriter.WriteLine(Acid_QC.TextBox40.Text)
objWriter.WriteLine(Acid_QC.TextBox41.Text)
objWriter.WriteLine(Acid_QC.TextBox42.Text)
objWriter.WriteLine(Acid_QC.TextBox43.Text)
objWriter.WriteLine(Acid_QC.TextBox44.Text)
objWriter.WriteLine(Acid_QC.TextBox45.Text)
objWriter.WriteLine(Acid_QC.TextBox46.Text)
objWriter.WriteLine(Acid_QC.TextBox47.Text)
objWriter.WriteLine(Acid_QC.TextBox48.Text)
objWriter.WriteLine(Acid_QC.TextBox49.Text)
objWriter.WriteLine(Acid_QC.TextBox50.Text)
objWriter.WriteLine(Acid_QC.TextBox51.Text)
objWriter.WriteLine(Acid_QC.TextBox52.Text)
objWriter.WriteLine(Acid_QC.TextBox53.Text)
objWriter.WriteLine(Acid_QC.TextBox54.Text)
objWriter.WriteLine(Acid_QC.TextBox55.Text)
objWriter.WriteLine(Acid_QC.TextBox56.Text)
objWriter.WriteLine(Acid_QC.TextBox57.Text)
objWriter.WriteLine(Acid_QC.TextBox58.Text)
objWriter.WriteLine(Acid_QC.TextBox59.Text)
objWriter.WriteLine(Acid_QC.TextBox60.Text)
objWriter.WriteLine(Acid_QC.TextBox61.Text)
objWriter.WriteLine(Acid_QC.TextBox62.Text)
objWriter.WriteLine(Acid_QC.TextBox63.Text)
objWriter.WriteLine(Acid_QC.TextBox64.Text)
objWriter.WriteLine(Acid_QC.TextBox65.Text)
objWriter.Close()
MsgBox("Data written to file")
Else
'Do Nothing
End If
Catch ex As Exception
End Try
End Sub
If you mean making the code more elegant? You could do something like the following.
ToolStripLabel1.Text = FileDataStorage.OpenFileTextBox1.Text
If ToolStripLabel1.Text = ("C:\Temp\New QCA.qca") Then
MsgBox("Must Save New file first ACCESS DENIED!")
End If
Dim FILE_NAME As String = FileDataStorage.OpenFileTextBox1.Text
Try
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
For Each c As Control In Form.Controls
If TypeOf c Is TextBox Or TypeOf c Is ComboBox Then
objWriter.WriteLine(c.Text)
End If
Next
objWriter.Close()
MsgBox("Data written to file")
Else
'Do Nothing
End If
Catch ex As Exception
Throw
End Try
In keeping with what you have already, an external function similar to this may work for you.
It goes over the form passed to it, and then finds the objects of a certain type, and then prints them out to the writer that you've created there. You'll have to make it work with what you have already, but something like this would save so many lines.
What it does is gets the number of controls on the form, and then goes across each one to get its type. If the type is correct, then it just writes its contents to the file.
Private sub writeToFile(formFrom as Form)
Dim controls As Control
For Each controls In formFrom.Controls
If TypeOf (controls) Is TextBox Then
objWriter.WriteLine(formFrom.controls.text)
elseif TypeOf (controls) is ComboBox then
objWriter.WriteLine(formFrom.controls.text)
End If
Next
end sub