images vb.net file used by another process error - vb.net

I'm writing a little program where I select a picture through an open file dialogue. When I selected a picture I want it to overwrite the current picture and display the new image. Now I don't have any problems with picking an image with a different extension. So when I currently have a .png I can select a .jpg but when I choose an image with the same extension as the current image I get an error:
The process cannot access the file 'C:\Users....\woontypeimages\chalet_foto.jpg' because it is being used by another process.
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim sFilename As String = cboWoningtypesWoningtype.SelectedItem.ToString & "_foto" & System.IO.Path.GetExtension(ofd.FileName)
System.IO.File.Copy(ofd.FileName, Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Camping Relaxx\woontypeimages\" & sFilename, True)
txtWoningtypesFoto.Text = sFilename
updateImages()
End If
Private Sub updateImages()
Try
picFoto.Image = Nothing
txtWoningtypesFoto.BackColor = clrReadonly
txtWoningtypesFoto.ForeColor = Color.Black
picFoto.Image = System.Drawing.Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Camping Relaxx\woontypeimages\" & txtWoningtypesFoto.Text)
Catch ex As Exception
txtWoningtypesFoto.BackColor = clrError
txtWoningtypesFoto.ForeColor = Color.White
End Try
Try
picGrondplan.Image = Nothing
txtWoningtypesGrondplan.BackColor = clrReadonly
txtWoningtypesGrondplan.ForeColor = Color.Black
picGrondplan.Image = System.Drawing.Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Camping Relaxx\woontypeimages\" & txtWoningtypesGrondplan.Text)
Catch ex As Exception
txtWoningtypesGrondplan.BackColor = clrError
txtWoningtypesGrondplan.ForeColor = Color.White
End Try
End Sub
If anyone could help me I would be pleased
Thanks in advance

Instead of worrying about Dispose() you can instead use the Load(string) method of the PictureBox which won't lock the file.
Me.PictureBox1.Load("C:\test.png")

Use these :
picFoto.Image.Dispose()
picGrondplan.Image.Dispose()
instead of :
picFoto.Image = Nothing
picGrondplan.Image = Nothing
The Image.FromFile method maintains a lock on the source file until the image has been disposed. Setting an object to nothing does not immediately dispose it - the garbage collector will take care of that in its own time (which might well not be until you've closed the form with the picture box on). Dispose is required to immediately free the file handle.

Related

PDFsharp Beta 1.50 PdfTextField, Null Exception Error, But Still Works?

I'm using PDFsharp 1.50 beta 3b. I'm mainly using it to access the ability to use newer PDF Documents. I'm not using any newer features. Down converting my PDF docs is killing them and I don't know why. That said;
Private Sub Print_Form()
Dim filename As String = ""
If IO.File.Exists(String.Format("{0}Template\Form.pdf", AppDomain.CurrentDomain.BaseDirectory)) Then
filename = String.Format("{0}Template\Form.pdf", AppDomain.CurrentDomain.BaseDirectory)
Else
MessageBox.Show("You're missing the Templates directory. If you don't know what this means, tell your IT Administrator.", "Missing Files")
Exit Sub
End If
Dim PDFDocument As PdfSharp.Pdf.PdfDocument = PdfSharp.Pdf.IO.PdfReader.Open(filename, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Modify)
Dim form As PdfSharp.Pdf.AcroForms.PdfAcroForm = PDFDocument.AcroForm
If form.Elements.ContainsKey("/NeedAppearances") Then
form.Elements("/NeedAppearances") = New PdfSharp.Pdf.PdfBoolean(True)
Else
form.Elements.Add("/NeedAppearances", New PdfSharp.Pdf.PdfBoolean(True))
End If
Try
'the subsequent line causes the exception to be thrown
CType(form.Fields("StringTest"), PdfSharp.Pdf.AcroForms.PdfTextField).Text = "Test"
Catch ex As Exception
Clipboard.SetText(ex.StackTrace)
End Try
CType(form.Fields("CheckBoxTest"), PdfSharp.Pdf.AcroForms.PdfCheckBoxField).Checked = True
PDFDocument.Save("temp.pdf")
Dim p As New System.Diagnostics.ProcessStartInfo()
p.Verb = "print"
p.WindowStyle = ProcessWindowStyle.Hidden
p.FileName = "temp.pdf"
p.UseShellExecute = True
System.Diagnostics.Process.Start(p)
End Sub
This creates an error;
An unhandled exception of type 'System.NullReferenceException' occurred in PdfSharp.dll
Additional information: Object reference not set to an instance of an object.
at PdfSharp.Pdf.AcroForms.PdfTextField.RenderAppearance()
at PdfSharp.Pdf.AcroForms.PdfTextField.set_Text(String value)
at WOTC_FE.frmInterview.Print_ICF() in d:\Programming\FE\FE\Applications\frmInterview.vb:line 2886
Now what makes this weird and why I'm asking is that this still works with the try/catch block. It will fill the field and the file has the correct text in the PDF file. I just want to know why does it throw this exception?
I figured out the issue. The new PDFSharp uses a different access method for it's controls.
First, our declarations;
Dim PDFDocument As PdfSharp.Pdf.PdfDocument = PdfSharp.Pdf.IO.PdfReader.Open(filename, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Modify)
Dim form As PdfSharp.Pdf.AcroForms.PdfAcroForm = PDFDocument.AcroForm
For checkboxes;
CType(form.Fields(<Field Name>), PdfSharp.Pdf.AcroForms.PdfCheckBoxField).Checked = True
And for strings;
PDFDocument.AcroForm.Fields("Field Name").Value = New PdfSharp.Pdf.PdfString("Input Text")
Doing it this way works, doesn't need a try/catch block, and doesn't throw an error.

Why does File.Open() not open the file?

I am implementing a Save File button in my VB.NET Windows Forms application.
I am attempting to encapsulate the normally expected behaviour of Save buttons in Windows applications. I.E: If a file was already selected then open the current file it, write to it and save it; else if there is no current file, or Save As was used, then show a SaveFileDialog, then open, write and save just the same.
I currently have coded the function below but I keep getting an exception:
Cannot access a closed file
The file is created just fine, but is empty (It should contain "Test string"). I can't understand how the file is closed unless some kind of garbage collection is doing away with it somehow??
The current code:
Function SaveFile(ByVal Type As ProfileType, ByVal suggestedFileName As String, ByVal saveAs As Boolean, ByVal writeData As String) As Boolean
Dim FileStream As Stream = Nothing
Dim FolderPath As String = Nothing
Dim CancelSave As Boolean = False
Dim SaveFileDialog As SaveFileDialog = New SaveFileDialog()
Try
If Type = ProfileType.Product Then 'Select the initial directory path
FolderPath = ProductPath
Else
FolderPath = ProfilePath
End If
If (FileName = String.Empty Or saveAs = True) Then 'If a file is not already selected launch a dialog to allow the user to select one
With SaveFileDialog
.Title = "Save"
.AddExtension = True
.CheckPathExists = True
.CreatePrompt = False
.DefaultExt = "xml"
.Filter = "Xml Files (*.xml)|*.xml"
.FilterIndex = 0
.FileName = suggestedFileName
.InitialDirectory = FolderPath
If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
FullyQualfiedPathName = New String(SaveFileDialog.FileName) 'Save the path and name of the file
FileName = Path.GetFileName(FullyQualfiedPathName)
Else
CancelSave = True
End If
.Dispose()
End With
End If
If (FileName <> String.Empty) Then 'Write the string to the file if the filewas correctly selected
FileStream = File.Open(FullyQualfiedPathName, FileMode.OpenOrCreate, FileAccess.ReadWrite) 'Open the file
Using FileStreamWriter As New StreamWriter(FileStream) 'Create the stream writer
FileStreamWriter.Write(writeData) 'Write the data
FileStream.Close() 'Clse the file
End Using
ElseIf (CancelSave <> True) Then 'Only throw an exception if the user *didn't* cancel the SavefileDialog
Throw New Exception("File stream was nothing", New IOException())
End If
Catch ex As Exception
MessageBox.Show(ex.Message & Environment.NewLine & FullyQualfiedPathName)
End Try
Return True
End Function
One problem I see is that you should be putting your File.Open in a Using block:
Using fs = File.Open(fullyQualfiedPathName, FileMode.OpenOrCreate, FileAccess.ReadWrite)
Using writer As New StreamWriter(fs) 'Create the stream writer
writer.Write(writeData) 'Write the data
'fs.Close() <--- you do not need this line becuase the "Using" block will take care of this for you.
End Using
End Using
I'm not sure if this will resolve your issue because I can't run your code, but the Using block will automatically take care of closing and cleaning up disposable instances like FileStream and StreamWriter, even if an exception is thrown.
By the way, you should use proper naming conventions (lower camel case) for local variables.

" A Sharing Violation occured" error when editing images in Paint using VB.NET

I have written a program in VB.NET to monitor file changes to recent saved screenshots taken from the tool.
I have a custom FileSystemWatcher class written (shared by someone on the internet,just customized) and have an instance invoked in my program.
The basic issue or objective is that when i capture a screen area using my program, I want it to immediately copy it over to clipboard. There are times when I would want to edit the captured image, in which case any editing made and saved, after saving the edited image must automatically be copied over to clipboard.
I use the below code to copy the image over to clipboard
'Subroutine to Copy the captured screenshot
'Added 15/09/2014
Sub CopyImageCapture(ByVal ImgPath)
Dim ThreadA As Thread
ThreadA = New Thread(AddressOf Me.MyAsyncTask)
'Setting ApartmentState.STA as this is needed for Clipboard related functionalities
ThreadA.SetApartmentState(ApartmentState.STA)
ThreadA.Start(ImgPath)
End Sub
'Threading to handle the Clipboard copy function
'Copy the screenshot to Clipboard
'Added 15/09/2014
Private Sub MyAsyncTask(ByVal ImgPath)
Clipboard.Clear()
Clipboard.SetImage(ImgPath)
End Sub
And when i choose to edit the image, the below code takes care to monitor the file changes
Sub MonitorFileChange(ByVal FolderPath, ByVal ItemName)
Try
Dim sFolder As String
Dim sFile As String
sFolder = FolderPath
sFile = ItemName
If IO.Directory.Exists(sFolder) Then
objWatcher.FolderToMonitor = sFolder
objWatcher.FileToMonitor = sFile
'objWatcher.NotifyFilter = (NotifyFilters.FileName Or NotifyFilters.Size)
objWatcher.StartWatch()
Else
MessageBox.Show("Folder does not exist!")
End If
Catch ex As Exception
MsgBox("Encountered an exception in MonitorFileChange subroutine. Details - " + ex.Message)
End Try
End Sub
'Subrountine to capture FileChanged events (esp. when captured image is edited in Paint and Saved)
'Works in sync with custom class 'clsFSW' for this purpose.
'ADDED: 15/09/2014
Private Sub objWatcher_FileChanged(ByVal FullPath As String) Handles objWatcher.FileChanged
'Try
Dim lastWriteTime As DateTime
lastWriteTime = File.GetLastWriteTime(FullPath)
Debug.Print(lastWriteTime)
If (lastWriteTime <> lastRead) Then
objWatcher.EnableRaisingEvents = False
'QuickCopy for changes files. BUG FIX
If (QuickSaveToolStripMenuItem.Checked) Then
Debug.Print("File Changed: " & FullPath & vbCrLf)
Dim tempI As System.Drawing.Bitmap = New System.Drawing.Bitmap(FullPath)
Dim tempI2 As System.Drawing.Bitmap = New System.Drawing.Bitmap(tempI, tempI.Width, tempI.Height)
CopyImageCapture(tempI2)
tempI.Dispose()
End If
lastRead = lastWriteTime
End If
'Catch ex As Exception
' MsgBox("Encountered an exception in objWatcher_FileChanged subrountine. Details - " + ex.Message)
' 'Finally
' 'objWatcher.EnableRaisingEvents = True
'End Try
End Sub
The problem is sometimes when I open the captured image and make quick saves or press save a couple of times real quick I get a "A Sharing violation occured" error
Can anyone help resolve the above issue? Which part of the code or logic is causing this to happen?
Also, if you check the above snippet, sometimes (havent seen any particular pattern) but the catch block in objWatcher_FileChanged is triggered and the error is "Parameter not found"
Can someone please help into this too?
Thanks in advance! Please let me know if any info more is required.
EDIT: Please note, the sharing violation error seems invoked from the Paint application itself and not my program, but why does it happen randomly, is a mystery to me. Is there nay loophole in the logic? Any alternative logic to what I want to achieve?
Try disposing tempI2. Windows may be locking the file used with tempI, and keeping it locked since it is assigned to tempI2. If so, it may remain locked until tempI2 is disposed.

Directshow GMFBridge saving multiple clips to a single file

Here is the main body of code that I am using with GMFBridge. I have an sg (sourcegraph) and rg (rendergraph) object that keeps the filtergraph, mediaseeking, etc., in the one place so I can release them.
When I run this it renders the clips to the screen perfectly. Seeking to each clip and playing properly. I get the right sequence of event codes in the "Bridge End of Segment" that I expect (i.e. one BEOS message per clip).
However, When I try to write the clips out to an AVI file, I only ever get the first clip. I also only ever get one "Bridge End of Segment" message. The code that is commented out in the "with rg" section is what I use to write to an AVI file. If I check the graphs in GraphEdit they are connected properly.
I have also added the code for the "Bridge End of Segment" message at the bottom.
This is driving me crazy. I must be missing something really simple. Any help appreciated.
' Get the source media file...
If openfile.ShowDialog = Windows.Forms.DialogResult.OK Then
If Bridge Is Nothing Then
Bridge = New GMFBridgeController
Bridge.AddStream(True, eFormatType.eAny, True)
'Bridge.AddStream(False, eFormatType.eMuxInputs, False)
Bridge.SetBufferMinimum(200)
End If
With sg
' Create graph to use.
.graph = New FilterGraph
' Create other interfaces. Don't need them all but am
' experimenting.
.mediacontrol = DirectCast(.graph, IMediaControl)
.mediaseeking = DirectCast(sg.graph, IMediaSeeking)
.mediaeventex = DirectCast(.graph, IMediaEventEx)
.mediaeventex.SetNotifyWindow(Me.Handle, WM_GRAPHNOTIFY, Nothing)
' Create the graphedit entry so I can see what I've made.
.rotentry = New DsROTEntry(.graph)
' If not using bridge comment out this line.
sg.filter = Bridge.CreateSourceGraph(openfile.FileName, sg.graph)
End With
With rg
.graph = DirectCast(New FilterGraph, IFilterGraph2)
.capgraph = New CaptureGraphBuilder2
.capgraph.SetFiltergraph(.graph)
''''' CODE THAT I USE TO WRITE TO AN AVI FILE
'''''Dim aviDest As IBaseFilter = New AviDest()
'''''.graph.AddFilter(aviDest, "AVI Mux")
'''''Dim writer As IBaseFilter = New FileWriter
'''''.graph.AddFilter(writer, "File Writer")
'''''Dim filewriter As IFileSinkFilter2 = DirectCast(writer, IFileSinkFilter2)
'''''filewriter.SetFileName(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\StatsManager\Videos\momtest.avi", Nothing)
' Create the render graph ...
.filter = Bridge.CreateRenderGraph(sg.filter, .graph)
' Ancillary control interfaces...
.mediaseeking = DirectCast(.graph, IMediaSeeking)
.mediacontrol = DirectCast(.graph, IMediaControl)
' Let me see the graph in GraphEdit.
.rotentry = New DsROTEntry(.graph)
End With
' Add my list of offsets...
For Each obj As Object In listOffsets.Items
clipoffsets.Add(Val(obj.ToString))
Next
' How long to play the clips for...
clipplaytime = Val(txtClipTime.Text)
' Just so I know what video I chose.
Me.Text = "Video:" + Path.GetFileName(openfile.FileName)
End If
This is the code that handles the "End of Segment" message.....
Public Sub Bridge_OnEndOfSegment() Handles Bridge.OnEndOfSegment
Try
log("BEOS")
clipindex = clipindex + 1
If clipindex = clipoffsets.Count Then ' this is the proper version :)
log("End of clips")
clipindex = 0
If chkLoop.Checked = False Then
sg.mediacontrol.Stop()
rg.mediacontrol.Stop()
Else
log("....looping")
SeekToClip(clipoffsets(clipindex))
End If
Else
log("Moving to clip:" + clipoffsets(clipindex).ToString)
SeekToClip(clipoffsets(clipindex))
End If
Catch ex As Exception
log("ERROR in EOS :" + ex.Message)
End Try
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