I am working on a program that I want to use for a simple home energy data information system. I am stuck at trying to
figure out how add an inputbox to the washing machine selection that is in my listbox. I would like to add an inputbox that
allows, (only if the user selects the washing machine) the user to add the amount of water they used in gallons per hour and
how much that water costs per gallon. In addition I can't figure out how to make all of my calculations appear in a column in
my second listbox. The data that I want to appear (as soon as the user enters it) is the appliance that they select, number of
hours they are used per day and the cost. Where I am having trouble at is with the calculations and making them appear in
the listbox without the use of a calculate button control. I want the calculations to be automatic as soon as I as the user would
enter it in the textbox controls. I originally created labels for the data to appear in but I plan to remove them since I really
want the data to appear in the second listbox. Here is the code from the form1.vb:
Public Class Form1
'Class level constants
Const dblHOURS_DAY As Double = 24 'Hours a day
Dim intPowerNeeded As Integer
Private Sub txtCostPerHour_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
txtCostPerHour.TextChanged
'Declare cost per hour
Dim decCostPerHour As Decimal
decCostPerHour = CDec(txtCostPerHour.Text)
If decCostPerHour <= 0 Then
'Display out of range message
MessageBox.Show("Please enter a positive number greater than zero.")
End If
'Displays the inputted data without the use label
lblDisplayKiloWattHourCost.Text = txtCostPerHour.Text
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim decCostPerHour As Decimal
Dim lblDisplayKiloWattHourCost As Decimal
decCostPerHour = CDec(txtCostPerHour.Text)
lblDisplayKiloWattHourCost = CDec(decCostPerHour.ToString("n"))
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
ListBox1.SelectedIndexChanged
Dim strOut As String
strOut = ListBox1.SelectedItem.ToString
'Create the above variable name in the label area where I need this data to be copied to.
End Sub
Private Sub txtNumberOfHours_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
txtNumberOfHours.TextChanged
lblNumberOfHoursDisplay.Text = txtNumberOfHours.Text
Dim intNumberOfHoursDisplay As Integer
Dim intCostDisplay As Integer
intNumberOfHoursDisplay = CInt(txtNumberOfHours.Text)
intCostDisplay = intNumberOfHoursDisplay * intPowerNeeded
End Sub
Private Sub strSelectedItemFromListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles strOut.SelectedIndexChanged
strOut.Text = lblCostDisplay.Text
End Sub
Private Sub txtPowerNeeded_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
txtPowerNeeded.TextChanged
intPowerNeeded = CInt(txtPowerNeeded.Text)
lblCostDisplay.Text = CStr(intPowerNeeded)
End Sub
End Class
Attachment:- Form1.zip