Explore BrainMass
Share

Explore BrainMass

    VB Program to calculate energy costs

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    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

    © BrainMass Inc. brainmass.com October 10, 2019, 7:22 am ad1c9bdddf
    https://brainmass.com/computer-science/visual-basic/vb-program-to-calculate-energy-costs-578853

    Attachments

    Solution Preview

    Dear student,

    Please find attached the complete VB project. I have reworked your form. The calculation is done whenever any of the parameters change and the calculated cost ...

    Solution Summary

    The VB program calculates the cost of different appliances in the house.
    The user should be able to do the following:

    - Enter the cost per kilowatt-hour.
    - Select from a list of home appliances which includes a washer, among other appliances.
    - Enter the power needed in kilowatts (kW) for the selected appliance.
    - Enter the number of hours used per day for the selected appliance.
    - If the washer is selected, enter both the number of gallons of water used by the washer per hour and the cost per gallon.

    The program should do the following:

    - Calculate and display the cost for operating a home appliance as soon as the data is entered.
    - Use arrays or structures where applicable.
    - Create a listing area to display each of the following for each appliance entered:
    o Home appliance
    o Number of hours per day
    o Cost
    o Display and update the total cost of all appliances, as soon as a new entry is added to the listing area.

    $2.19