1

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
6
  • I can't see where you are changing the variable at (validMiles, validPercent and validAvg). You are passing them into ValidateBoxes, but what does that do? Please update question with this. Commented Jul 8, 2015 at 16:01
  • Private Sub ValidateBoxes(ByVal boxVal As Double, ByVal testLowVal As Double, ByVal testHighVal As Double, ByVal err As String, ByRef errCheck As Boolean) If (boxVal < testLowVal Or boxVal > testHighVal) Then MessageBox.Show(err) Else errCheck = True End If End Sub Commented Jul 8, 2015 at 16:11
  • I didn't put it because its a method for validating the input. I just want to take that calculate method and pass the result into the txtTotalCost textBox Commented Jul 8, 2015 at 16:12
  • Perhaps this is the issue: where are you setting the text on your output textbox? I'm thinking you need a line like txtTotalCost.Text = totalCost.ToString() somewhere after you calculate totalCost. Commented Jul 8, 2015 at 16:26
  • Ok I was trying to pass that method to the txtTotalCost text box object for display but your saying to just add the line to the calculate method for displaying correct. Commented Jul 8, 2015 at 16:30

1 Answer 1

1

You appear to already have a block that runs when all three values are "valid". Simply output that value at the bottom of it:

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

Edit...

Also call your Calculate() method whenever one of your textboxes changes:

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

Note how all three textboxes are listed after the handles keyword.

Sign up to request clarification or add additional context in comments.

6 Comments

Yeah I tried that no output. Before I was using a button and when clicked it would display in a messagebox but not they want it to do it automatically without a button click.
Yeah I had that originally in the txtTotalCost text box and no dice but when I add that to the other text boxes now its constantly running the loop. I can tell because every time I enter a numeric position it pops up my error message
Right...just get rid of that MessageBox line. You could blank out "txtTotalCost" instead. Then it will only update with a new value once all valid values have been entered.
I removed that messagebox a long time ago, It is exactly as you see and have provided to me
Make sure you aren't calling Calculate() when txtTotalCost changes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.