I am using Visual Studio 13 and programming in visual basic for a intro CSCi class. I need to write a program to calculate weekly pay using a textbox to enter the number of hours via click event, a listbox showing the hours entered, then a text box for hourly rate, another button to calculate weekly pay, then finally two text boxes one displaying the total hours worked and another showing total weekly pay. I got alot of my basics in place and set up my form but none of the code im using is doing what I need to... please help with demonstration of how to do it.

Dani AI

Generated

Clarification for : the form needs two discrete actions — add a validated hours value into the ListBox, and calculate pay from the ListBox total and an hourly-rate textbox. Common issues are non-numeric input, culture-dependent decimal separators, and forgetting to recalc totals after list changes. As asked for code, a minimal WinForms VB.NET pattern is below; ’s Excel AutoSum suggestion applies to spreadsheets, not a Visual Basic form.

' Add hours and refresh totals (assumes controls: txtHours, lstHours, txtTotalHours)
Private Sub btnAddHours_Click(sender As Object, e As EventArgs) Handles btnAddHours.Click
    Dim hours As Double
    If Double.TryParse(txtHours.Text.Trim(), hours) AndAlso hours >= 0 Then
        lstHours.Items.Add(hours.ToString("0.##"))
        UpdateTotals()
        txtHours.Clear()
        txtHours.Focus()
    Else
        MessageBox.Show("Enter a valid non-negative number for hours.", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    End If
End Sub

Private Sub UpdateTotals()
    Dim total As Double = 0
    For Each itm As Object In lstHours.Items
        Dim h As Double
        If Double.TryParse(itm.ToString(), h) Then total += h
    Next
    txtTotalHours.Text = total.ToString("0.##")
End Sub
' Calculate weekly pay (assumes controls: txtHourlyRate, txtTotalPay)
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim rate As Double
    If Not Double.TryParse(txtHourlyRate.Text.Trim(), rate) OrElse rate < 0 Then
        MessageBox.Show("Enter a valid hourly rate.", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Return
    End If

    UpdateTotals()
    Dim totalHours As Double
    Double.TryParse(txtTotalHours.Text, totalHours)

    Dim weeklyPay As Double = rate * totalHours
    txtTotalPay.Text = weeklyPay.ToString("C2")
End Sub

Troubleshooting notes: ensure control names in code match the form; make total textboxes ReadOnly to prevent desync; test parsing with both dot and comma decimals if the machine uses a non-US culture (or use CultureInfo.InvariantCulture when parsing). If overtime rules are needed, apply a separate calculation (e.g., hours > 40 at 1.5x). This pattern keeps input validation, summing, and formatting separate and easy to debug.

Recommended Answers

All 2 Replies

Can't say what you are doing wrong unless you show us the code you are running. We are not going to just give you the solution.

Add the values in a column or row by using a button. You can use AutoSum to quickly sum a range of numbers in a column or row. Click an empty cell below a column of numbers or to the right of a row of numbers, and then click AutoSum. Excel selects what it determines to be the most likely range of data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.