Commands on Form Load - vb.net

I have a command in a form which activates on Form Load which is as follows
Private Sub frm_15_IssueWarning_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lbl_incidentid.Text = frm_6_UpdateIncident.lbl_incidentid.Text
'checks to see if a containment already exists for the Incident, if it does, hide the save containment button
Dim SqlString As String = "select [containmentid],[incidentid],[containmentdate],[containment] from [containment] WHERE [incidentid] = #incidentid"
Using conn As New OleDbConnection(ConnString)
Using command As New OleDbCommand(SqlString, conn)
command.Parameters.AddWithValue("#incidentid", lbl_incidentid.Text)
Using adapter As New OleDbDataAdapter(command)
conn.Open()
Dim reader As OleDbDataReader = command.ExecuteReader()
Try
If reader.Read() Then
Button1.Visible = False
Else
Button1.Visible = True
End If
Finally
reader.Close()
End Try
End Using
End Using
End Using
And all this works great, however the first command
lbl_incidentid.Text = frm_6_UpdateIncident.lbl_incidentid.Text
If I remove this line and place it in form 6 so that when pressing the button on form 6 to open form 15 it includes the line
lbl_incidentid.Text = frm_15_IssueWarning.lbl_incidentid.Text
The rest of the command on form load no longer works, previously this has'nt been a problem because you could only access form 15 from one location but now this is changing so the form can be opened from various locations. Is there a command I can write here so that the form knows what form was open/pressed in order to make it here and act accordingly or can anyone suggest another way round this issue? I apologise if this doesnt make much sense, its a bit of a ramble.

In the end I changed the forms a little, instead of opening the form and closing the previous form straight away I inserted an if statement to see what previous form was open before closing it so the new form could act accordingly, please see example below
'checks to see which form is open to see where to get incident if from
If frm_6_UpdateIncident.Visible = True Then
Me.lbl_incidentid.Text = frm_6_UpdateIncident.lbl_incidentid.Text
frm_6_UpdateIncident.close()
End If
If frm_17_IncidentView.Visible = True Then
Me.lbl_incidentid.Text = frm_17_IncidentView.lbl_incidentid.Text
frm_17_IncidentView.close()
End If
It was actually really simple, no idea why I didint think of this in the first place

Related

datagridview last programmatically changed cell does not get included in changedrows

VB.net app changes values in a datagridview programmatically. The values are all as they should be, but the save routine (dtShipments is the datatable that is the source for the datagridview)
Dim dtChanges As DataTable = dtShipments.getchanges()
If more than one has changed, dtChanges is always missing the last row.
In the routine that changes the cell values, I have tried DatagridView1.EndEdit and DatagridView1.CommitEdit, but the behavior is the same. I even tried adding a SendKeys.Send(vbTab) line, since hitting the tab key when making the changes manually is enough to get all the changes to show up in .GetChanges.
What am I missing?
code per request:
Private Sub btnAssign_Click(sender As Object, e As EventArgs) Handles btnAssign.Click
strModErr = "Form1_btnAssign_Click"
Dim sTruck As String = ""
Try
sTruck = Me.DataGridView2.SelectedCells(0).Value.ToString
For Each row As DataGridViewRow In DataGridView1.SelectedRows
row.Cells("Truck").Value = sTruck
Next
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
Catch ex As Exception
WriteErrorToLog(Err.Number, strModErr + " - " + Err.Description)
End Try
End Sub
Private Function SaveChanges() As Boolean
strModErr = "Form1_SaveChanges"
Dim Conn As New SqlConnection(My.Settings.SQLConnectionString)
Dim sSQL As String = "UPDATE fs_Shipments SET Truck = #Truck, Stop = #Stop WHERE SalesOrder = #SO"
Dim cmd As New SqlCommand(sSQL, Conn)
Dim sSO, sTruck As String
Dim iStop As Integer = 0
Try
DataGridView1.EndEdit()
DataGridView1.ClearSelection()
Dim dtChanges As DataTable = dtShipments.getchanges() 'DataRowState.Modified
If Not dtChanges Is Nothing Then
Conn.Open()
For Each row As DataRow In dtChanges.Rows
sSO = row("SalesOrder").ToString
sTruck = row("Truck").ToString
iStop = CInt(row("Stop").ToString)
With cmd.Parameters
.Clear()
.AddWithValue("#SO", sSO)
.AddWithValue("#Truck", sTruck)
.AddWithValue("#Stop", iStop)
End With
cmd.ExecuteNonQuery()
Next
End If
Return True
Catch ex As Exception
WriteErrorToLog(Err.Number, strModErr + " - " + Err.Description)
Return False
End Try
End Function
I am not exactly 100% sure why this happens. The problem appears to be specific to when the user “selects” the cells in the first grid, and then calls the SaveChanges code “BEFORE” the user has made another selection in the first grid. In other words, if the user “selects” the rows to “assign” the truck to in grid 1, then, “AFTER” the “selected” cells have been filled with the selected truck, THEN, the user selects some other cell in grid 1, THEN calls the save changes code. In that case the code works as expected.
All my attempts at committing the changes, either failed or introduced other issues. I am confident a lot of this has to do with the grids SelectedRows collection. IMHO, this looks like a risky way to set the cell values, I would think a more user-friendly approach may be to add a check box on each row and assign the truck values to the rows that are checked. But this is an opinion.
Anyway, after multiple attempts, the solution I came up with only further demonstrates why using a BindingSource is useful in many ways. In this case, it appears the DataTable is not getting updated with the last cell change. Again, I am not sure “why” this is, however since it appears to work using a BindingSource, I can only assume it has something to do with the DataTable itself. In other words, before the “Save” code is executed, we could call the tables AcceptChanges method, but then we would lose those changes. So that is not an option.
To help, below is a full (no-fluff) example. In the future, I highly recommend you pull out the parts of the code that do NOT pertain to the question. Example, all the code that saves the changes to the DB is superfluous in relation to the question… so remove it. The more unnecessary code you add to your question only increases the number of SO users that will “ignore” the question. If you post minimal, complete and working code that reproduces the problem so that users can simply copy and paste without having to add addition code or remove unnecessary code will increase you chances of getting a good answer. Just a heads up for the future.
The solution below simply adds a BindingSource. The BindingSource’s DataSource is the DataTable dtShipments then the BindingSource is used a DataSource to DataGridView1. Then in the SaveChanges method, “before” the code calls the dtShipment.GetChanges() method, we will call the BindingSources.ResetBinding() method which should complete the edits to the underlying data source, in this case the DataTable dtShipments.
If you create a new VS-VB winforms solution, drop two (2) grids and two (2) buttons onto the form as shown below, then change the button names to “btnAssign” and “btnApplyChanges.” The assign button will add the selected truck in grid two to the selected rows in grid one. The apply changes button simply resets the binding source and displays a message box with the number of rows that were changed in the dtShipments DataTable. It should be noted, that the code calls the dtShipments.AcceptChanges() method to clear the changes. Otherwise, the code will update changes that have already been made.
Dim dtShipments As DataTable
Dim dtTrucks As DataTable
Dim shipBS As BindingSource
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dtShipments = GetShipmentsDT()
shipBS = New BindingSource()
shipBS.DataSource = dtShipments
dtTrucks = GetTrucksDT()
dtShipments.AcceptChanges()
DataGridView1.DataSource = shipBS
DataGridView2.DataSource = dtTrucks
End Sub
Private Function GetShipmentsDT() As DataTable
Dim dt = New DataTable()
dt.Columns.Add("ID", GetType(String))
dt.Columns.Add("Truck", GetType(String))
dt.Rows.Add(1)
dt.Rows.Add(2)
dt.Rows.Add(3)
dt.Rows.Add(4)
Return dt
End Function
Private Function GetTrucksDT() As DataTable
Dim dt = New DataTable()
dt.Columns.Add("Truck", GetType(String))
For index = 1 To 10
dt.Rows.Add("Truck" + index.ToString())
Next
Return dt
End Function
Private Sub btnAssign_Click(sender As Object, e As EventArgs) Handles btnAssign.Click
Dim sTruck = DataGridView2.SelectedCells(0).Value.ToString
'Dim drv As DataRowView
For Each row As DataGridViewRow In DataGridView1.SelectedRows
'drv = row.DataBoundItem
'drv("Truck") = sTruck
row.Cells("Truck").Value = sTruck
Next
'DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
'shipBS.ResetBindings(True)
'DataGridView1.CurrentCell = DataGridView1.Rows(0).Cells(0)
End Sub
Private Function SaveChanges() As Boolean
Try
shipBS.ResetBindings(True)
Dim dtChanges As DataTable = dtShipments.GetChanges()
If (dtChanges IsNot Nothing) Then
MessageBox.Show("There are " & dtChanges.Rows.Count & " rows changed in the data table")
' update sql DB
dtShipments.AcceptChanges()
End If
Return True
Catch ex As Exception
Return False
End Try
End Function
Private Sub btnApplyChanges_Click(sender As Object, e As EventArgs) Handles btnApplyChanges.Click
Dim ChangesMade As Boolean
ChangesMade = SaveChanges()
End Sub
Lastly, since you are using VB… I highly recommend that you set the “Strict” option to “ON.” When this option is ON, it will flag possible errors that you may be missing. To do this for all future VB solutions, open VS, close any solutions that may be open, then go to Tools->Options->Projects and Solutions->VB Defaults, then set “Option Strict” to “On.” The default setting is off.
I hope this makes sense and helps.

Cognex Insight SetComment Not Persisting

I have a job that stores cell locations as comments in particular cells, but I'm running into a situation where the CvsInsight::SetComment method is not persisting.
I'm displaying a form as a dialog wherein the user can change the cell locations that are stored in the comment cells and when the user clicks the save button I'm creating a new instance of a custom class, setting the properties to the new cell locations (set by the user), setting the DialogResult as OK, and then closing the form. Then in the form where I called ShowDialog, I call the SetComment method for each property in the custom class on their respective cell.
This is what I'm doing in the dialog's Save button:
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
' Check if the name, username, password, pass, fail, total, reset, and results are all set
Dim invalidFields As List(Of String) = New List(Of String)()
For Each pair In _requiredFields
If (String.IsNullOrWhiteSpace(DirectCast(Controls.Find(pair.Key, True).FirstOrDefault, TextBox).Text)) Then
invalidFields.Add(pair.Value)
End If
Next
If (invalidFields.Any()) Then
MessageBox.Show($"The following required fields are missing a value: {String.Join(", ", invalidFields)}", "Invalid Form", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
' Set the returned object's values, set the dialog result, and then close the dialog
CameraSettings = New CameraSettings() With {
.FailCell = FailCellLocation.Text,
.FocusCell = FocusCell.Text,
.IsVisible = Visibility.Checked,
.PassCell = PassCellLocation.Text,
.ResetCell = ResetCellLocation.Text,
.ResultsCell = ResultsCellLocation.Text,
.TotalCell = TotalCellLocation.Text
}
DialogResult = DialogResult.OK
Close()
End Sub
And this is what I'm doing in the form that opens the dialog:
Private Sub Settings_Click(sender As Object, e As EventArgs) Handles Settings.Click
Using cameraSettingsDialog As frmCameraSetting = New frmCameraSetting(InsightDisplay.InSight)
With cameraSettingsDialog
If (.ShowDialog = DialogResult.OK) Then
InsightDisplay.InSight.SetComment(New CvsCellLocation(_focusCell), New CvsCellComment(.CameraSettings.FocusCell))
InsightDisplay.InSight.SetComment(New CvsCellLocation(_passCell), New CvsCellComment(.CameraSettings.PassCell))
InsightDisplay.InSight.SetComment(New CvsCellLocation(_failCell), New CvsCellComment(.CameraSettings.FailCell))
InsightDisplay.InSight.SetComment(New CvsCellLocation(_totalCell), New CvsCellComment(.CameraSettings.TotalCell))
InsightDisplay.InSight.SetComment(New CvsCellLocation(_resultCell), New CvsCellComment(.CameraSettings.ResultsCell))
InsightDisplay.InSight.SetComment(New CvsCellLocation(_resetCell), New CvsCellComment(.CameraSettings.ResetCell))
GetSettingCells()
End If
End With
End Using
End Sub
What is happening is that the code executes without throwing any exceptions, but the comment is not set. What's frustrating is that I'm not able to debug because the CvsInsightDisplay's Insight gets set to null anytime I try to access the results in the middle of setting the comment. However, I can verify that the CameraSettings' properties are what I'd expect them to be because if I setup a Console.WriteLine to print the various properties they're right.
Looking through the SDK, I cannot find any documentation as to why it wouldn't set the value without throwing an exception.
For those of you who run into the same issue, the problem resolved around the fact that I was trying to set a cell that was past the maximum row in the job. To fix the issue, I had to change the cells in which I was setting the comments for to ones with a lower row index.
Unfortunately, Cognex does not have this behavior documented in any of their documentation.

How to make sortable a DataGrid bounded with a table

I load data on a DataGrid from a SQLite database with code like this:
conn.Open()
Dim sql = "SELECT * FROM TableDataGrid"
Dim cmdConnection As SQLiteCommand = New SQLiteCommand(sql, conn)
Dim da As New SQLiteDataAdapter
da.SelectCommand = cmdConnection
Dim dt As New DataTable
da.Fill(dt)
MyDataGrid.DataSource = dt
When the user click on a Row I display the data on a few textbox, combobox, no problem at all.
Now I need to let the user sort the DataGrid by clicking the column he want and of course is not that simple.
After research on the "e.RowIndex >= 0" and "System.NullReferenceException" error I understand that "The problem is that out of the box the BindingList does not support sorting! I know - sounds dumb but that is how it is." posted here: WinForms: DataGridView - programmatic sorting
So If I'm right I need to implement my own SortableBindingList but I'm confused because the samples of code are about a LIST and I load the database records on a TABLE (dt)
Example:
http://timvw.be/2007/02/22/presenting-the-sortablebindinglistt/
Another post say I can fix the trouble with a line like:
SortableBindingList<YourListType> sortableBindingList = new SortableBindingList<YourListType>(list)
What is the generic solution to make the DataGrid sortable on this case?
Thanks to the tips of the users I found the solution
Private Sub MyDataGrid_SelectionChanged(sender As Object, e As EventArgs) Handles MyDataGrid.SelectionChanged
'Check if the user clicked on a header or a row
Try
If MyDataGrid.CurrentRow.Index = Nothing Then
'Header clicked!!
Else
'Row clicked!!!
'Here I load the data of the clicked row on my form
FormCleanDataEntry()
FormShowRowData()
End If
Catch ex As Exception
'MsgBox(ex.ToString())
End Try
End Sub
The curiosity is that the exception is thrown but anyway I don't show any message to the user and the DataGrid is sorted and the program works fine

VB.NET Reloading Active Docked Forms

I hope you can help as this little problem has been real headaches and after lengthy research I have found no viable solution.
My program uses an employee pay number to retrieve data from a database with nearly all forms being docked using WeifenLuo. When the pay number is changed, it clears the datasets with new information but we don't want the users to have to manually close all the open docked windows down - in effect each open window needs to refresh with new employee information. I have tried .refersh(), .invalidate() - which does not seem to reload the data within each combo/textbox.
After much research I tried this:
Private Sub tbPayNumber_KeyDown(sender As Object, e As KeyEventArgs) Handles tbPayNumber.KeyDown
If e.KeyCode = Keys.Enter Then
Call Paynumber_Authentication()
'close and re-open any active forms
Dim table As New DataTable
table.Columns.Add("Forms", GetType(Form))
For Each frm As Form In MdiChildren
table.Rows.Add(frm)
Next
For i = MdiChildren.Length - 1 To 0 Step -1
MdiChildren(i).Close()
Next
For i As Integer = 0 To table.Rows.Count - 1
Dim ResetForms = table.Rows(i)("Forms")
ResetForms.Show(pnlDockMain, DockState.Document)
Next
End If
End Sub
If I run the code as above I get the annoying MS designed error "Cannot access a disposed object."
If I change the last part of the code to:
For i As Integer = 0 To table.Rows.Count - 1
Dim ResetForms As New Form
ResetForms = table.Rows(i)("Forms")
ResetForms.Show(pnlDockMain, DockState.Document)
Next
I get the informous overload resolution error on the ResetForms.Show line - too many arguments.
Its worth saying that each form will get any information it needs as the form opens, which works correctly. Any help reloading each form would be appreciated as the only other way I can think of is to list every textbox on every form (over 30 forms) and give them new values individually - and manually. A lot of people talk about this problem, taking about the .IsDisposed method etc So I'm hoping you might find an elegant solution to this.
Thanks in advance, Shane
Private Sub tbPayNumber_KeyDown(sender As Object, e As KeyEventArgs) Handles tbPayNumber.KeyDown
If e.KeyCode = Keys.Enter Then
Call Paynumber_Authentication()
Call Load_Employee()
'close and re-open any active forms
Dim table As New DataTable
table.Columns.Add("Forms", GetType(String))
For Each frm As Form In MdiChildren 'load the name of each open form into a dataset
table.Rows.Add(frm.Name)
Next
For i = MdiChildren.Length - 1 To 0 Step -1 'closes all open docked windows
MdiChildren(i).Close()
Next
For i As Integer = 0 To table.Rows.Count - 1
Select Case table.Rows(i)("Forms")
Case "EmployeeDetails"
Dim LoadForm As New EmployeeDetails
EmployeeDetails.Show(pnlDockMain, DockState.Document)
Case "HomePage"
Dim LoadForm As New HomePage
HomePage.Show(pnlDockMain, DockState.Document)
Case "SOEInput"
Dim LoadForm As New SOEInput
SOEInput.Show(pnlDockMain, DockState.Document)
End Select
Next
End If
End Sub

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