Make a program that will allow the user to enter up to ten digits and then average them. The numbers entered can have more than one digit and can have only one decimal point.
Requirements:
1.Use a procedure in a standard module to display the digit on the screen.
2. Use a constant for the upper limit of the array.
3.The user should not be allowed to store more than 10 numbers.
4. The users should only be allowed to press cmdAverage if there are numbers in the array.
5. the user should only be allowed to enter one decimal point per array.
6.If the display area is blank the user should not be able to store a value. (I think you should use the Len() function)
i know I looked at it , see the problem im having with mine is that I have to use 10 buttons and 1 display and I have to add the numbers one at a time with in a loop I think, and I have no idea how to do that.
add a text box called txtNum
Copy and paste it 9 more times. Choose YES for create control array(10 total..)
add a cmnd btn named cmdAverage
drop this code in the form:
Code:
Private Sub cmdAverage_Click()
For x = 0 To txtNum.Count - 1
If txtNum(x).Text <> "" Then
tmp = tmp + Val(txtNum(x).Text)
cnt = cnt + 1
End If
Next
MsgBox "The Average of the " & cnt & " Numbers entered is : " & tmp / cnt
End Sub
Private Sub txtNum_Change(Index As Integer)
For x = 0 To txtNum.Count - 1
If txtNum(x).Text <> "" Then cmdAverage.Enabled = True: Exit Sub
Next
cmdAverage.Enabled = False
End Sub
Private Sub txtNum_GotFocus(Index As Integer)
txtNum(Index).SelStart = 0
txtNum(Index).SelLength = Len(txtNum(Index))
End Sub
Private Sub txtNum_KeyPress(Index As Integer, KeyAscii As Integer)
If KeyAscii = Asc(".") And InStr(txtNum(Index).Text, ".") Then KeyAscii = 0
If IsNumeric(Chr(KeyAscii)) = True Or KeyAscii = Asc(".") Or KeyAscii = 8 Then Exit Sub
KeyAscii = 0
End Sub
VBBrowser v2.2.4
This code:
Disables Button if no nums are entered
allows only numbers and only 1 "." per numb
uses a control array
loops through and counts/ add num to get average..
also auto selects enter num when focus is given (so user doesnt have to delete)
I have not looked at plenderj's code but knowing him I am guessing his code is complete as well.
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
Private Sub Command1_Click()
Dim i As Long
Dim numNumbers As Long
Dim tempX As Double
For i = 0 To txtNum.UBound
If (txtNum(i) <> "") Then
If (IsNumeric(txtNum(i))) Then
tempX = tempX + txtNum(i)
numNumbers = numNumbers + 1
End If
End If
Next i
If (numNumbers <> 0) Then MsgBox "Average : " & tempX / numNumbers
End Sub
Private Sub Form_Load()
Dim i As Long
For i = 1 To 9
Load txtNum(i)
txtNum(i).Move txtNum(0).Left, i * (130 + txtNum(0).Height)
txtNum(i).Visible = True
Load lblNum(i)
lblNum(i).Move lblNum(0).Left, i * (130 + txtNum(0).Height)
lblNum(i).Visible = True
lblNum(i).Caption = "Number " & i & " : "
Next i
End Sub
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]