初学者做Visual Basic项目

时间:2013-10-23 10:59:59

标签: vb.net click

我正在用vb.net做一个小项目,我很难将加热成本恢复到非浮动点。

  • 调用函数来计算池是小,中还是大。 在函数中使用适当的分支结构,并返回一个String 说明游泳池的类别。事件处理程序将此String分配给 类别标签的Text属性。现在有了这个,我有一个名为lblCategory的标签。

  • 最后,在事件处理程序中插入一个循环结构来计算加热的美元 - 百分之一值。需要一个循环来改变温度变量(它需要是一个浮点类型),从5到23 in的值 步骤1.5。

    在此循环中,将温度变量的值添加到Avg Temp文本框 并添加相应的值 在$ Per Month文本框中,调用一个使用以下公式的函数:

    加热成本=(25 - 温度)*体积/ 32500

    这将计算将水池加热到25度的成本,乘以 池的体积温差,并调整预定值。 确保该函数返回非浮点数据类型,以便删除任何 结果中的浮点值。

到目前为止,这是我的代码......

Public Class Form1

Const MinLength As Integer = 5
Const MaxLength As Integer = 50
Const MinWidth As Integer = 2
Const MaxWidth As Integer = 20
Const MinDepth As Integer = 2
Const MaxDepth As Integer = 4


Private Function ValidLength(ByVal TestLength As String) As Boolean
    'Length of the pool
    Dim i As Double
    Dim Message As String = ""
    If Double.TryParse(TestLength, i) Then
        If i >= MinLength AndAlso i <= MaxLength Then
            Return True
        End If
    End If
    Message = "Length measurement is not valid." & vbCrLf & vbCrLf & "Please enter a value between 5 and 50"
    MessageBox.Show(Message, "Data Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Return False
    End
End Function

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim Volume As Double
    Dim Temp As Double
    Dim SA As Double
    Dim HeatingCostInt As Integer
    If Not ValidLength(txtLength.Text) Then
        Exit Sub
    End If
    If Not ValidWidth(txtWidth.Text) Then
        Exit Sub
    End If
    If Not ValidDepth(txtAvgDepth.Text) Then
        Exit Sub
    End If
    'SurfaceArea Function
    SA = Val(SurfaceArea(txtLength.Text, txtWidth.Text))
    txtSurfaceArea.Text = SA
    'Volume Function
    Volume = Val(Vol(txtLength.Text, txtWidth.Text, txtAvgDepth.Text * 1000))
    txtVolume.Text = Volume
    If PoolVolume(Volume) Then
    End If
    'Temperature Function
    For Temp = 5 To 23 Step 1.5
        txtTableAvgTemp.AppendText(Temp & Environment.NewLine)
        HeatingCost(Temp, Volume)
    Next
End Sub

Private Function ValidWidth(ByVal TestWidth As String) As Boolean
    'Width of the Pool
    Dim Message As String = ""
    Dim i As Double
    If Double.TryParse(TestWidth, i) Then
        If i >= MinWidth And i <= MaxWidth Then
            Return True
        End If
    End If
    Message = "Width measurement is not valid." & vbCrLf & vbCrLf & "Please enter a value between 2 and 20"
    MessageBox.Show(Message, "Data Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Return False
    End
End Function

Private Function ValidDepth(ByVal TestDepth As String) As Boolean
    'Depth of the pool
    Dim Message As String = ""
    Dim i As Double
    If Double.TryParse(TestDepth, i) Then
        If i >= MinDepth And i <= MaxDepth Then
            Return True
        End If
    End If
    Message = "Depth measurement is not valid." & vbCrLf & vbCrLf & "Please enter a value between 2 and 4"
    MessageBox.Show(Message, "Data Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Return False
    End
End Function

Private Function SurfaceArea(ByVal Value1 As Double, ByVal Value2 As Double) As Double
    'Calculation for SurfaceArea
    SurfaceArea = Value1 * Value2
    Return SurfaceArea
End Function

Private Function Vol(ByVal Value1 As Double, ByVal Value2 As Double, ByVal Value3 As Double) As Double
    'Calculation for Volume
    Vol = Value1 * Value2 * Value3 * 1000
    Return Vol
End Function

Private Function PoolVolume(ByVal Value1 As Double) As Boolean
    'Pool Volume size that isn't working correctly and I don't know why
    If (Value1 <= 500000) Then
        lblCategory.Text = "Pool Category: Small"
    ElseIf (Value1 > 500000 <= 1500000) Then
        lblCategory.Text = "Pool Category: Medium"
    Else
        lblCategory.Text = "Pool Category: Large"
        Value1 = lblCategory.Text
    End If
    Return PoolVolume
End Function

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    'Ends the program
    End
End Sub

Private Sub txtSurfaceArea_TextChanged(sender As Object, e As EventArgs) Handles txtSurfaceArea.TextChanged
    'Changes text of SurfaceArea
    txtSurfaceArea.Text = txtLength.Text * txtWidth.Text
End Sub

Private Sub txtVolume_TextChanged(sender As Object, e As EventArgs) Handles txtVolume.TextChanged
    'Changes text of Volume
    txtVolume.Text = txtLength.Text * txtWidth.Text * txtAvgDepth.Text * 1000
End Sub

Private Function HeatingCost(ByVal Value1 As Double, ByVal Value2 As Double) As Double
    'Calculation for Heating Cost that isn't returning as a non-floating point. Main issue right here!!! 
    Dim Value3 As Double = 32500
    HeatingCost = (25 - Value1) * Value2 / Value3
    txtTableDollars.AppendText(HeatingCost & Environment.NewLine)
End Function

End Class

提供的任何帮助对我来说都是非常好的和有用的,感谢您仔细观察并花时间来完成它

2 个答案:

答案 0 :(得分:0)

您的代码中有一个Exit Sub。在你的三个条件块之后是一个Exit Sub,它将始终在这一行退出你的sub。

If Not ValidLength(txtLength.Text) Then
    Exit Sub
End If
If Not ValidWidth(txtWidth.Text) Then
    Exit Sub
End If
If Not ValidDepth(txtAvgDepth.Text) Then
    Exit Sub
End If
Exit Sub

删除最后一个“Exit Sub”^^

答案 1 :(得分:0)

您的ValidXXX()函数需要一些工作。例如,如果用户在TextBox中键入“ChokeOnThis”,程序将崩溃并刻录。这是因为您传递的字符串需要Single。这导致从String到Single的隐式(自动)转换。如果String不是有效的Single(如“ChokeOnThis”示例),则抛出异常并且程序崩溃。

执行此操作的正确方法是在ValidXXX()函数中接收String,然后在内部使用Integer.TryParse()函数。再次注意,函数的参数“TestLength”是一个String:

Private Function ValidLength(ByVal TestLength As String) As Boolean
    'Length of the pool
    Dim i As Single
    If Single.TryParse(TestLength, i) Then
        If i >= MinLength AndAlso i <= MaxLength Then
            Return True
        End If
    End If

    Dim Message As String = _
        "Length measurement is not valid." & vbCrLf & vbCrLf & _
        "Please enter a value between 5 and 50"
    MessageBox.Show(Message, "Data Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Return False
End Function

您对其他ValidXXX()函数进行了类似的更改。

SurfaceArea()和其他计算函数存在同样的问题,因为它们期望Integer参数,但您传递的是String值。但是,由于此时将对字符串进行验证,因此程序不会崩溃。让这些转换多次发生只是一个糟糕的设计。更好的设计将验证输入并将转换后的Integer值存储在局部变量中,以便将它们传递给计算函数。