0% found this document useful (0 votes)
36 views

Imports System

This document contains code for a form that calculates the time required for an object to reach thermal equilibrium based on initial and surrounding temperatures and a thermal conductivity value. It displays the calculated time, exports a temperature over time graph to an image file, and includes error handling. The form allows users to input values, view the results, reset the inputs and delete temporary files on closing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Imports System

This document contains code for a form that calculates the time required for an object to reach thermal equilibrium based on initial and surrounding temperatures and a thermal conductivity value. It displays the calculated time, exports a temperature over time graph to an image file, and includes error handling. The form allows users to input values, view the results, reset the inputs and delete temporary files on closing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Imports System.

ComponentModel
Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1

Dim sngInitTemp As Single


Dim sngSurrTemp As Single
Dim sngK As Single
Dim sngTime As Single
Dim strExportDirectory As String = Nothing

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


txtTime.ReadOnly = True
btnShowGraph.Enabled = False
End Sub

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles


btnCalculate.Click
Try
sngInitTemp = txtInitTemp.Text
sngSurrTemp = txtSurrTemp.Text
sngK = txtK.Text
'Note that the 0.0001 below is simply a tolerance on the difference of
the
'surrounding and current temperatures.
'Note that sngTime is declared as a field variable.
sngTime = (1 / -sngK) * Math.Log((0.0001) / (sngInitTemp - sngSurrTemp))
txtTime.Text = sngTime
btnShowGraph.Enabled = True

'Each error expected is supplied a response of its own accordingly.


Catch ex As System.InvalidCastException
MessageBox.Show("Input is Invalid.", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)

Catch ex1 As System.OverflowException


MessageBox.Show("Input leads to Overflow.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)

Catch ex2 As System.NotFiniteNumberException


MessageBox.Show("Input leads to Infinite Values.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)

Catch ex3 As System.Exception


MessageBox.Show("Unknown Error.", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)

End Try
End Sub

Private Sub btnShowGraph_Click(sender As Object, e As EventArgs) Handles


btnShowGraph.Click
Try
Dim oXL As New Excel.Application
oXL.Visible = True

Dim oWB As Excel.Workbook = oXL.Workbooks.Add


Dim oSheet As Excel.Worksheet = oWB.Sheets("Sheet1")
Dim oChart As Excel.Chart = oWB.Charts.Add

With oSheet
.Range("A1").Value = "Initial Temperature, °C"
.Range("B1").Value = sngInitTemp
.Range("A2").Value = "Surrounding Temperature, °C"
.Range("B2").Value = sngSurrTemp
.Range("C1").Value = "Time, s"
.Range("D1").Value = "Current Temperature, °C"
End With

'Temp will be calculated every 0.5 seconds.


Dim sngTimeInt As Single = 0.5
'Simply calculate the number of steps for the For-Next statement
Dim intSteps As Integer = Math.Round((sngTime / sngTimeInt), 0)

'Column A
For intCounter As Integer = 0 To (intSteps - 1)
'Added +2 to intCounter to start at row 2 since row 1 is for the
heading
oSheet.Cells(intCounter + 2, 3).value = intCounter * sngTimeInt
Next

'Column B
For intCounter As Integer = 0 To (intSteps - 1)
oSheet.Cells(intCounter + 2, 4).formula = "=$B$2+($B$1-$B$2)*EXP(-"
& sngK & "*C" & intCounter + 2 & ")"
If intCounter = 3 And sngK = 0.1 Then oSheet.Cells(5, 4).Formula =
"=$B$2+($B$1-$B$2)*EXP(-0.1*C5)"
Next

'Add the chart


With oChart
.ApplyCustomType(Excel.XlChartType.xlXYScatterSmoothNoMarkers)
.SetSourceData(Source:=oSheet.Range("C2:D" & intSteps + 1))
.HasLegend = False
.HasTitle = True
.ChartTitle.Text = "Time vs. Temperature"
End With
'X-Axis
Dim XAxis As Excel.Axis = oChart.Axes(Excel.XlAxisType.xlCategory,
Excel.XlAxisGroup.xlPrimary)
XAxis.HasTitle = True
XAxis.AxisTitle.Characters.Text = "Time, s"
'Y-Axis
Dim YAxis As Excel.Axis = oChart.Axes(Excel.XlAxisType.xlValue,
Excel.XlAxisGroup.xlPrimary)
YAxis.HasTitle = True
YAxis.AxisTitle.Characters.Text = "Temperature, °C"

'Export Chart to Image


strExportDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop &
"\Chart.gif"
oChart.Export(Filename:=strExportDirectory)
picChart.SizeMode = PictureBoxSizeMode.Zoom
picChart.Image = Image.FromFile(strExportDirectory)

'Closes Excel and releases all Excel Objects without saving


oWB.Saved = True
oChart = Nothing
oSheet = Nothing
oWB = Nothing
oXL.Quit()

'Each error expected is supplied a response of its own accordingly.


Catch ex As System.InvalidCastException
MessageBox.Show("Input is Invalid.", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)

Catch ex1 As System.OverflowException


MessageBox.Show("Input leads to Overflow.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)

Catch ex2 As System.NotFiniteNumberException


MessageBox.Show("Input leads to Infinite Values.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)

Catch ex3 As System.Exception


MessageBox.Show("Unknown Error.", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub

Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles


btnReset.Click
txtInitTemp.Clear()
txtK.Clear()
txtSurrTemp.Clear()
txtTime.Clear()
picChart.Image.Dispose()
picChart.Image = Nothing
If strExportDirectory <> Nothing Then
'Delete picture
My.Computer.FileSystem.DeleteFile(strExportDirectory)
strExportDirectory = Nothing
End If
End Sub

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs)


Handles Me.FormClosing
If strExportDirectory <> Nothing Then
'Delete picture
picChart.Image.Dispose()
picChart.Image = Nothing
My.Computer.FileSystem.DeleteFile(strExportDirectory)
End If
End Sub
End Class

You might also like