I have a home utility auditing program that I am working on. I am very new to VB and am just learning the intricacies of the language. Basically I have everything working as I want except one feature. The program uses text boxes to accept user entry and then creates a value in a text box based on those entries (basically a user enters the cost per hour of electricity, selects an appliance, enters the amount of hours used, and the cost of using the appliance is presented in a text box. What I now need to have happen is for the program to recognize when the data in the result text box has changed, then add all of the numbers up to give a total for all appliances entered. This will allow the user to keep a running total of costs for however many appliances they want to enter. I think I need a do loop to handle this but I am unsure of the syntax. So for the following code the values that need to be added together will be from the opCostTextBox and the total should be displayed will be given in the TextBoxTotalCost. This is the first VB program I have written, so I apologize in advance for any obvious answers I am missing.
Public Class Form1
'declaring variables to store program values
Dim costEntry As Single
Dim applianceSelect As String
Dim powerRequired As Single
Dim hoursUsed As Single
Dim operCost As Single
Dim strGallons As Single
Dim strCostPerGallon As Single
Dim washerAddCost As Single
Dim strListBoxEntry As String
'this section for subroutine to run once calculate button is pressed
Private Sub calcButton_Click(sender As System.Object, e As System.EventArgs) Handles calcButton.Click
'converting variables into single data type
costEntry = CSng(costEntryTextBox.Text)
applianceSelect = applianceComboBox.Text
powerRequired = CSng(powerTextBox.Text)
hoursUsed = CSng(hoursTextBox.Text)
'math to calculate the solution
If applianceSelect = "Washer" Then
operCost = (costEntry * hoursUsed * powerRequired) + (strGallons * strCostPerGallon)
operCost = CSng(FormatCurrency(operCost, 2))
MessageBox.Show("$ " + CStr(washerAddCost) + " has been added to the cost of operating the washer to account for water useage")
Else
operCost = costEntry * hoursUsed * powerRequired
operCost = CSng(FormatCurrency(operCost, 2))
End If
'formatting results for display
opCostTextBox.Text = FormatCurrency(operCost, 2)
'causes data to be dispayed in listbox
strListBoxEntry = ((applianceComboBox.Text) + ", Cost per Hour:" + Str(costEntry) + ", Power Req'd:" + Str(powerRequired) + ", Hours Used:" + Str(hoursUsed) + ", Cost of Operation= $" + Str(operCost))
ListBox1.Items.Add(strListBoxEntry)
End Sub
'section for handling the combobox and assigning the multiplication values to determine the power requirements for each appliance
Private Sub applianceComboBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles applianceComboBox.SelectedIndexChanged
If applianceComboBox.Text = "Toaster" Then
powerTextBox.Text = CStr(1.146)
ElseIf applianceComboBox.Text = "Coffee Pot" Then
powerTextBox.Text = CStr(1.2)
ElseIf applianceComboBox.Text = "Can Opener" Then
powerTextBox.Text = CStr(0.1)
ElseIf applianceComboBox.Text = "Microwave Oven" Then
powerTextBox.Text = CStr(1.45)
ElseIf applianceComboBox.Text = "Electric Range" Then
powerTextBox.Text = CStr(12.2)
ElseIf applianceComboBox.Text = "Air Conditioner" Then
powerTextBox.Text = CStr(1.9)
ElseIf applianceComboBox.Text = "Washer" Then
powerTextBox.Text = CStr(0.512)
strGallons = CSng(CDbl(InputBox("Enter the number of gallons of water used by the washer per hour.")))
strCostPerGallon = CSng(CDbl(InputBox("Enter the cost per gallon of water used by the washer.")))
washerAddCost = strGallons * strCostPerGallon
ElseIf applianceComboBox.Text = "Dryer" Then
powerTextBox.Text = CStr(2.79)
ElseIf applianceComboBox.Text = "Television" Then
powerTextBox.Text = CStr(0.147)
ElseIf applianceComboBox.Text = "Lamp" Then
powerTextBox.Text = CStr(0.05)
End If
End Sub
Private Sub costEntryTextBox_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles costEntryTextBox.KeyPress
'validation controls to ensure numeric data only is entered in cost entry text box
If Asc(e.KeyChar) < 46 Or Asc(e.KeyChar) > 57 Then
e.Handled = True
'displays box telling the user to enter only numbers
MessageBox.Show("Please enter only numbers")
End If
End Sub
'This section handles the reset button functionality to clear all textboxes
Private Sub resetButton_Click(sender As System.Object, e As System.EventArgs) Handles resetButton.Click
costEntryTextBox.Text = ""
applianceComboBox.SelectedIndex = -1
powerTextBox.Text = ""
hoursTextBox.Text = ""
opCostTextBox.Text = ""
End Sub
Private Sub powerTextBox_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles powerTextBox.KeyPress
'validation controls to ensure numeric data only is entered in cost entry text box
If Asc(e.KeyChar) < 46 Or Asc(e.KeyChar) > 57 Then
e.Handled = True
'displays box telling the user to enter only numbers
MessageBox.Show("Please enter only numbers")
End If
End Sub
Private Sub hoursTextBox_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles hoursTextBox.KeyPress
'validation controls to ensure numeric data only is entered in cost entry text box
If Asc(e.KeyChar) < 46 Or Asc(e.KeyChar) > 57 Then
e.Handled = True
'displays box telling the user to enter only numbers
MessageBox.Show("Please enter only numbers")
End If
End Sub
Private Sub TotalCostTextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles TotalCostTextBox.TextChanged
End Sub
End Class