Displaying Input into textbox

时间:2015-07-08 15:40:04

标签: vb.net

I am trying to display a calculation into a TextBox, but having trouble with getting it to show. I want it to show once all input fields are true.

Public Class VehicleAudit

 Private Sub Calculate()
    Dim validMiles As Boolean = False
    Dim validPercent As Boolean = False
    Dim validAvg As Boolean = False

    Dim inputtedMiles As Double
    Dim inputtedPercent As Double
    Dim inputtedAvgCost As Double
    Dim servTruck As Integer
    Try
        inputtedMiles = Double.Parse(txtMilesDriven.Text)
        inputtedPercent = Double.Parse(txtPercent.Text)
        inputtedAvgCost = Double.Parse(txtAvgCost.Text)
    Catch ex As FormatException
        MessageBox.Show("Please enter all values and try again")
        Return
    End Try

    Dim cal As String = FormatCurrency(Convert.ToString(inputtedAvgCost * inputtedMiles * (1.0 + inputtedPercent))) + " dollars."

    ValidateBoxes(inputtedMiles, 0, 10000, "Miles must range from 0-10000.", validMiles)
    ValidateBoxes(inputtedPercent, 0.1, 0.5, "Please enter percent from .10 to .50", validPercent)
    ValidateBoxes(inputtedAvgCost, 0.25, 0.75, " Please enter Average cost from .25 to .75", validAvg)

    If (validAvg And validMiles And validPercent) Then
        Dim totalCost As Double
        If boxVehicleSelect.SelectedIndex = 9 Then
            servTruck = inputtedMiles / 100 'this way we lose precision using the integer, so values below 100s are dropped.
            totalCost = servTruck * 15.46
        Else
            totalCost = inputtedAvgCost * inputtedMiles * (1.0 + inputtedPercent)
        End If
    End If


End Sub

Private Sub txtTotalCost_TextChanged(ByVal Calculate As String, e As EventArgs) Handles txtTotalCost.TextChanged

End Sub

1 个答案:

答案 0 :(得分:1)

当所有三个值都“有效”时,您似乎已经有一个块运行。只需在其底部输出该值:

If (validAvg And validMiles And validPercent) Then
    Dim totalCost As Double
    If boxVehicleSelect.SelectedIndex = 9 Then
        servTruck = inputtedMiles / 100 'this way we lose precision using the integer, so values below 100s are dropped.
        totalCost = servTruck * 15.46
    Else
        totalCost = inputtedAvgCost * inputtedMiles * (1.0 + inputtedPercent)
    End If

    ' Output the computed "totalCost" some where.
    ' Here I'm using a Textbox called "txtTotalCost":
    txtTotalCost.Text = totalCost.ToString()

End If

编辑...

每当您的一个文本框发生更改时,也可以调用Calculate()方法:

Private Sub TextChanged(sender As Object, e As EventArgs) Handles txtMilesDriven.TextChanged, txtAvgCost.TextChanged, txtPercent.TextChanged
    Calculate()
End Sub

请注意handles关键字后面列出的所有三个文本框。