Average.frm

 VERSION 5.00
Begin VB.Form frmAverage
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Averages"
   ClientHeight    =   2970
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   4395
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   2970
   ScaleWidth      =   4395
   StartUpPosition =   2  'CenterScreen
   Begin VB.CommandButton cmdExit
      Caption         =   "E&xit"
      Height          =   375
      Left            =   2760
      TabIndex        =   15
      Top             =   2280
      Width           =   1095
   End
   Begin VB.Frame Frame1
      Height          =   1695
      Left            =   240
      TabIndex        =   11
      Top             =   960
      Width           =   2055
      Begin VB.CommandButton cmdCalculate
         Caption         =   "Calculate A&verage"
         Height          =   375
         Left            =   120
         TabIndex        =   14
         Top             =   1080
         Width           =   1815
      End
      Begin VB.TextBox txtAnswer
         Height          =   285
         Left            =   480
         TabIndex        =   13
         Top             =   600
         Width           =   1095
      End
      Begin VB.Label Label1
         AutoSize        =   -1  'True
         Caption         =   "Scores Average"
         Height          =   195
         Index           =   0
         Left            =   480
         TabIndex        =   12
         Top             =   240
         Width           =   1140
      End
   End
   Begin VB.CommandButton cmdClear
      Caption         =   "Clear &Boxes"
      Height          =   375
      Left            =   2760
      TabIndex        =   5
      Top             =   1680
      Width           =   1095
   End
   Begin VB.TextBox txtScores
      Height          =   285
      Index           =   4
      Left            =   3480
      TabIndex        =   4
      Top             =   360
      Width           =   735
   End
   Begin VB.TextBox txtScores
      Height          =   285
      Index           =   3
      Left            =   2640
      TabIndex        =   3
      Top             =   360
      Width           =   735
   End
   Begin VB.TextBox txtScores
      Height          =   285
      Index           =   2
      Left            =   1800
      TabIndex        =   2
      Top             =   360
      Width           =   735
   End
   Begin VB.TextBox txtScores
      Height          =   285
      Index           =   1
      Left            =   960
      TabIndex        =   1
      Top             =   360
      Width           =   735
   End
   Begin VB.TextBox txtScores
      Height          =   285
      Index           =   0
      Left            =   120
      TabIndex        =   0
      Top             =   360
      Width           =   735
   End
   Begin VB.Label Label1
      Caption         =   "Score 1"
      Height          =   255
      Index           =   5
      Left            =   120
      TabIndex        =   10
      Top             =   120
      Width           =   735
   End
   Begin VB.Label Label1
      Caption         =   "Score 2"
      Height          =   255
      Index           =   4
      Left            =   960
      TabIndex        =   9
      Top             =   120
      Width           =   735
   End
   Begin VB.Label Label1
      Caption         =   "Score 3"
      Height          =   255
      Index           =   3
      Left            =   1800
      TabIndex        =   8
      Top             =   120
      Width           =   735
   End
   Begin VB.Label Label1
      Caption         =   "Score 4"
      Height          =   255
      Index           =   2
      Left            =   2640
      TabIndex        =   7
      Top             =   120
      Width           =   735
   End
   Begin VB.Label Label1
      Caption         =   "Score 5"
      Height          =   255
      Index           =   1
      Left            =   3480
      TabIndex        =   6
      Top             =   120
      Width           =   735
   End
End
Attribute VB_Name = "frmAverage"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' Calculate average scores
'
'For the purpose of this demo I've used text boxes.
'Ideally one would use labels rather than text boxes
'for displaying scores. That way we don't let the
'user cheat changing scores ;-)

'  :-: Give credit where credit is due :-:

'I read an answer to a question in one of the vb
'news groups titled "Sums in VB3" by Markus
'markus.lausen@amd.com and ended up trying it myself
'So thanks go to Markus wherever you are.

'Also used in this demo is some code by Adnan Hasanovic
'which can be found at:
'http://www.freevbcode.com/ShowCode.Asp?ID=2377
'Nice 3D Effect, try it!

'Email: i-am@thepub.co.za

Option Explicit

Dim counter As Integer
Dim sum As Integer

Private Sub cmdCalculate_Click()
    Dim i As Integer
    Dim average As String

    'Set counter to 0
    counter = 0

    'Set sum to 0
    sum = 0

    'Count number of text boxes that have values
    'in them and sum them. If any text box has no
    'value (empty) then the counter excludes that
    'box in the final equation
     For i = 0 To 4
        If txtScores(i) <> "" And Not IsNumeric(txtScores(i)) Then
            MsgBox "All values must be numeric!"
            Exit Sub
        End If
    Next i
   
    For i = 0 To 4
        If txtScores(i) <> "" Then
            sum = sum + txtScores(i)
            counter = counter + 1
        End If
    Next i

    'Straight forward math done here.
    If counter > 0 Then average = sum / counter

    txtAnswer.Text = average
End Sub

Private Sub cmdClear_Click()

    'Clear all text boxes.
    Dim i As Integer

    For i = 0 To 4
        txtScores(i).Text = ""
    Next i

    txtAnswer.Text = ""
End Sub

Private Sub cmdExit_Click()
    Set frmAverage = Nothing
    Unload Me
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Set frmAverage = Nothing
End Sub

Sub PaintForm3D(frm As Form)
    ' This Sub draws lines around the Form to make it 3d

    ' white, upper - horizontal
    frm.Line (0, 0)-(frm.ScaleWidth, 0), &HFFFFFF, BF
    ' white, left - vertical
    frm.Line (0, 0)-(0, frm.ScaleHeight), &HFFFFFF, BF
    ' darkgrey, right - vertical
    frm.Line (frm.ScaleWidth - 15, 0)-(frm.ScaleWidth - 15, _
            frm.Height), &H808080, BF
    ' darkgrey, lower - horizontal
    frm.Line (0, frm.ScaleHeight - 15)-(frm.ScaleWidth, _
            frm.ScaleHeight - 15), &H808080, BF

End Sub

Private Sub Form_Load()
    Me.AutoRedraw = True
    PaintForm3D Me
End Sub

Project Homepage: