Hello VB community,
I am taking a intro to VB class this fall and the assignment for this week is to create a program that keeps a running total of the numbers a user enters and also can average out the running total. I've been at it for a couple of days now but I cannot get the program to store and display the running total or average out that total. When I click the add to total button hit just displays the last entered number, and the average button only causes a zero to be displayed regardless of the user input. I have only been using VB for a week now and I truly appreciate any hints you can offer. Fallow the screen shot of the program is the code I have built so far. Thank you.
![Name: Running Toatl Prog.PNG
Views: 21
Size: 12.3 KB]()
Public Class RunningAverage
Inherits System.Windows.Forms.Form
'global variables
Dim RunTotals As Integer
Dim TotalClicks As Integer
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblIntro.Click
'Initialize Text Boxes, labels, and variables to zero when form is loaded
txtNumEnter.Text = ""
lblRunTotals.Text = ""
RunTotals = 0
TotalClicks = 0
End Sub
Private Sub numEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblInstruct.Click
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNumEnter.TextChanged
End Sub
Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
Me.Close() 'Remove active window
End 'End program
End Sub
Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
txtNumEnter.Text = ""
lblRunTotals.Text = ""
'Re-initialize RunTotal
RunTotals = 0
'Set focus back to text box
txtNumEnter.Focus()
End Sub
Private Sub cmdAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAverage.Click
Dim TotalClicks As Integer = 0
Dim RunAverage As Integer
'keeps track of the total of numbers entered by the user
TotalClicks = TotalClicks + 1
lblInstruct.Text = CStr(TotalClicks)
'computes the average of the running total
RunAverage = RunTotals / TotalClicks
'Place average into label
lblAverage.Text = RunAverage
End Sub
Private Sub cmdTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRunTotals.Click
'Declare local variables
Dim NumEntered As Integer 'Number entered by user
'Dim RunTotals As Integer '
'Get number entered into text box and put into variable
NumEntered = Val(txtNumEnter.Text)
'Calculate running total;
RunTotals = RunTotals + NumEntered
'Place new total into label
lblRunTotals.Text = RunTotals
'Clear out text box for next number
txtNumEnter.Text = ""
'Return cursor to text box for next number
txtNumEnter.Focus()
End Sub
Private Sub lblRunTotals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblRunTotals.Click
End Sub
End Class
I am taking a intro to VB class this fall and the assignment for this week is to create a program that keeps a running total of the numbers a user enters and also can average out the running total. I've been at it for a couple of days now but I cannot get the program to store and display the running total or average out that total. When I click the add to total button hit just displays the last entered number, and the average button only causes a zero to be displayed regardless of the user input. I have only been using VB for a week now and I truly appreciate any hints you can offer. Fallow the screen shot of the program is the code I have built so far. Thank you.
Public Class RunningAverage
Inherits System.Windows.Forms.Form
'global variables
Dim RunTotals As Integer
Dim TotalClicks As Integer
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblIntro.Click
'Initialize Text Boxes, labels, and variables to zero when form is loaded
txtNumEnter.Text = ""
lblRunTotals.Text = ""
RunTotals = 0
TotalClicks = 0
End Sub
Private Sub numEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblInstruct.Click
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNumEnter.TextChanged
End Sub
Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
Me.Close() 'Remove active window
End 'End program
End Sub
Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
txtNumEnter.Text = ""
lblRunTotals.Text = ""
'Re-initialize RunTotal
RunTotals = 0
'Set focus back to text box
txtNumEnter.Focus()
End Sub
Private Sub cmdAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAverage.Click
Dim TotalClicks As Integer = 0
Dim RunAverage As Integer
'keeps track of the total of numbers entered by the user
TotalClicks = TotalClicks + 1
lblInstruct.Text = CStr(TotalClicks)
'computes the average of the running total
RunAverage = RunTotals / TotalClicks
'Place average into label
lblAverage.Text = RunAverage
End Sub
Private Sub cmdTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRunTotals.Click
'Declare local variables
Dim NumEntered As Integer 'Number entered by user
'Dim RunTotals As Integer '
'Get number entered into text box and put into variable
NumEntered = Val(txtNumEnter.Text)
'Calculate running total;
RunTotals = RunTotals + NumEntered
'Place new total into label
lblRunTotals.Text = RunTotals
'Clear out text box for next number
txtNumEnter.Text = ""
'Return cursor to text box for next number
txtNumEnter.Focus()
End Sub
Private Sub lblRunTotals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblRunTotals.Click
End Sub
End Class