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)
Related
I need help. I want to check if user exists by entering their ic number and I want to display another rest of their data by using file in visual basic. Unfortunately, an error occurs while doing that. I need help. If the user exists, then It will display automatically name, email, address and so on but if a user doesn't exist, then it shows message box. Here I attached the image of the display screen and the code. Please help me. Thank you.
Public Class Form1
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim userFile As String = "C:\Users\HP\Desktop\userdata.txt"
Dim inputFile As String
If System.IO.File.Exists(userFile) = True Then
Dim objReader As New System.IO.StreamReader(userFile)
Dim intIc As Integer
Dim intCount As Integer = 0
Dim strName As String
Dim strEmail As String
Dim intPhoneNum As String
Dim strAdd1 As String
Dim strAdd2 As String
Dim intPostcode As String
Dim strState As String
Do While objReader.Peek() <> -1
intIc(intCount) = Convert.ToInt64(objReader.ReadLine())
If (intIc(intCount).Convert.ToInt64(objReader.ReadLine())) Then
strName(intCount) = objReader.ReadLine()
strEmail(intCount) = objReader.ReadLine()
intPhoneNum(intCount) = Convert.ToInt32(objReader.ReadLine())
strAdd1(intCount) = objReader.ReadLine()
strAdd2(intCount) = objReader.ReadLine()
intPostcode(intCount) = Convert.ToInt32(objReader.ReadLine())
strState(intCount) = objReader.ReadLine()
lblName.Text = strName
lblEmail.Text = strEmail
lblNum.Text = intPhoneNum
lblAdd1.Text = strAdd1
lblAdd2.Text = strAdd2
lblPostcode.Text = intPostcode
lblState.Text = strState
objReader.Close()
Else
MessageBox.Show("User Does Not Exist")
End If
intCount = intCount + 1
Loop
Else
MessageBox.Show("File Does Not Exist")
End If
End Sub
End Class
Your task, the easy way:
make a new project
add a DataSet to this new project
open the DataSet, in the properties call it something sensible
Right click the surface, add a new datatable, name it Person
Right click the datatable, add a column, name it IC. Right click, add column, name it Name. Keep going until you added all the fields you want to track(email,phone,address1 etc)
save the DataSet
open the form
show the datasources window (view menu.. other windows)
expand the nodes til you can see Person
click the drop down next to Person, switch from DataGridview to Details
drag Person onto the form. Text boxes, labels etc appear. In the tray at the bottom more things appear
add a textbox to the form and call it searchTextBox
add a search button to the form, double click it, add this line of code to the click handler:
personBindingSource.Filter = '[ic] LIKE '" & searchTextBox.Text & "'"
If personBindingSource.Count = 0 Then MessageBox.Show("No records")
double click the form background to add a form load event handler, put this line of code:
If IO.File.Exists("data.xml") Then .ReadXml("data.xml")
switch back to designer, single click the form background and switch to event properties of the form, add a handler to the form closing event:
.WriteXml("data.xml")
That's it, you now have a program that will open, read and fill the DataSet with data from the data.xml file, it will search it when you type something in the ic box, the text boxes use databinding to show values automatically, and when you close the program it will save updates data. The only task now is to load the xml file with data.
When the textboxes were added to the form you should also have seen a bar appear across the top with some left/right controls in and a green plus. Click the green plus, type some data in, click it again, type more data. Navigating back, if you're adding new data, will commit the data. If you're looking at existing data, editing it then navigating will commit it
After you added some data, you can search for existing data using the search box. When you've searched for a single value it should be the only thing shown and the nav will show "1 of 1". To get back to the mode where all data is showing, put a single asterisk in the search box and hit search; it should show the number records in the top bar and you can scroll them with the arrows.
If you already have lots of data in a file, like you use in your question, you can read it in a loop (like you do in your question, except don't use that code exactly cos it has loads of errors) as a one time thing and assign it into the datatable, or you can manipulate it directly into being XML in a text editor. This is easy to do if you have a capable text editor but I'll not offer any particular advice on it in case you don't have a large amount of existing data. Ask a new question if you do
all
I am working on a project with vb.net and MySQL database.
Now for taking information I have added few checkboxes, and in case if user selects a checkbox named other I want a window to appear and it should have a text box and when user enters the text at that box, the details should be stored in db.
As you haven't provided much information I may can't answer exactly your question but check if this can help.
First Add LINQ to SQL (.dbml File) - Let's name it as XYZ and Dataset (.xsd File) - let's name this as XYZ too, to your project and then drag and drop your database table in both the files and save all.
Now going into your form which contains the check box you mentioned.
Add this code to you checkbox click_event.
If checkbox1.checked = True Then
Dim insertValue As String = ""
insertValue = InputBox("Enter text to insert", YourTitle, "")
If inserValue <> "" Then
Dim db as New XYZDataContext
Dim NewRec As New YourTableName With {.ColumnName = insertValue}
db.YourTableName.InsertOnSubmit(NewRec)
db.SubmitChanges()
Msgbox("Value added!")
End If
checkbox1.checked = False
End If
create your window in the designer and give it a name like someWindow. then in your code open the window from the click event of your checkbox. when you close the window don't dispose it. just hide it me.hide so when you window closes you can retrieve the data from your textbox.
Sub Test ()
Dim wd_SomeInfo as new someWindow
wd_SomeInfo.showdialog()
Dim result As String = wd_SomeInfo.txt_sometextbox.text
If result = "" Then cancel....
End Sub
...Hopefully this question will be readable...
I have 27 textboxes.
The controlname and the text in the textboxes are written to a textfile like this:
System.DateTime.Now.ToString("yyyyMMdd") & "_Names_Config.txt"
Dim objWriter As New System.IO.StreamWriter(configfile, True)
'Textboxes
objwriter.Writeline("tbmax1") 'Control name
objwriter.Writeline("tbmax1.text) 'The text in the textbox
objWriter.WriteLine("tbname1") 'Control name
objWriter.WriteLine(tbname1.Text) 'The text in the textbox
objWriter.WriteLine("tbext1") 'Control name
objWriter.WriteLine(tbext1.Text) 'The text in the textbox
'And so on for all the the controls
This goes on for all the textboxes and controls, so 54 lines in total.
Works great. The textfile looks like this:
Alright, now the issue. There will be a load button that should search the textfile -> find the control matching the form's control -> use the line below and fill that spesific line in the control's textbox -> then find next control and do the same.
Load button: This is #1 attempt;
'Openfiledialog, then:
Using reader As New StreamReader(OpenFileDialog1.FileName.ToString)
Dim currentTextBox As TextBox = Nothing
While reader.Peek > -1
Dim line As String = reader.ReadLine()
If Not String.IsNullOrEmpty(line) Then
Dim tmpTextbox = Controls.Find(line, True) 'Try to find text according to line
If tmpTextbox.Any() Then 'It´s a textbox name
currentTextBox = DirectCast(tmpTextbox(0), TextBox)
Else
'?
End If
End If
End While
End Using
Here comes what I don't understand at all. See before and after picture of what happens to the 27 textboxes after I load the textfile.
What it SHOULD look like:
What it will look like:
"Edit channels" is actually the title of the form itself. I'm speechless. Because I totally don't understand why this happens and the load code, I moved on to another attempt.
#2 attempt:
Using reader As New StreamReader(OpenFileDialog1.FileName)
Dim Line As String = reader.ReadLine()
Dim Current As Integer = 0
Dim TB As TextBox = Nothing
While Not IsNothing(Line) 'It will be Nothing when file is over
'__________________________________________________________________1
If Line.StartsWith("tbext1") Then
'We will increment CurrentChannel, as we changed the section
Current += 1
For onetonine = 1 To 9
tbext1.Text = Line
Next
End If
If Line.StartsWith("tbname1") Then
'We will increment CurrentChannel, as we changed the section
Current += 1
For onetonine = 1 To 9
tbname1.Text = Line
Next
End If
If Line.StartsWith("tbmax1") Then
'We will increment CurrentChannel, as we changed the section
Current += 1
For onetonine = 1 To 9
tbmax1.Text = Line
Next
End If
'__________________________________________________________________2
'Then I guess this would go on for all the 27 textboxes (probably a really bad attempt)
End While
End Using
However, this just goes into break mode.
Your first approach works already if you fill the Else part:
If tmpTextbox.Any() Then
currentTextBox = DirectCast(tmpTextbox(0), TextBox)
ElseIf currentTextBox IsNot Nothing Then
currentTextBox.Text = line
End If
But you should not use it in production code:
Control-names are a very bad key for a database record or file entry:
They can change in future and you won't notice it
They are GUI related and not supposed to be identifiers for properties
is not fail-safe because there could be multiple controls with the same name
don't store key-value pairs on different lines, that makes it difficult, less readable and more error-prone to put them together afterwards
If you want to use one line you need a delimiter that a user never enters, it can be a combination of multiple characters like |::| or something similar. Then you can later use String.Split({"|::|"}, StringSplitOptions.None) to get both tokens back.
However, this is still not a good approach and also not 100% safe. So better approaches were
serialize/deserialize a List(Of String).
If you want to store it in a way that a human can read you should prefer XML.
Here's an example how you can write/read xml easily:
' write all TextBoxes to a file '
Dim allTextBoxes = TextBoxPanel.Controls.OfType(Of TextBox)()
Dim doc As New XDocument(New XElement("Channels"))
For Each txt In allTextBoxes
doc.Root.Add(New XElement(txt.Name, txt.Text.Trim()))
Next
doc.Save(OpenFileDialog1.FileName)
' later read it ... '
Dim xml As XDocument = XDocument.Load(OpenFileDialog1.FileName)
For Each element As XElement In xml.Descendants("Channels").Descendants()
Dim txt = allTextBoxes.FirstOrDefault(function(t) t.Name = element.Name)
If txt IsNot nothing
txt.Text = element.Value
End If
Next
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
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)?