Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class frmRevenue
- 'array holding strings that are destined to be the labels of the textboxes
- 'that are created on form load. i use the number of the quarter as the
- 'hotkey for each textbox. last string is for yearly revenue label
- Private a_sQuarters() As String = {"Quarter &1 Revenue", "Quarter &2 Revenue", _
- "Quarter &3 Revenue", "Quarter &4 Revenue", _
- "Yearly Revenue"}
- 'array to be used to hold the values of the individual
- 'quarters
- Private a_dQuarters(_iQuarterCnt) As Decimal
- Private _iQuarterCnt As Integer = a_sQuarters.GetUpperBound(0)
- Private txtQuarters(_iQuarterCnt) As TextBox
- Private lblQuarters(_iQuarterCnt) As Label
- Private txtRevenueDisplay As TextBox
- 'Private lblRevenueDisplay As Label
- Private Function IsValidRevenue(ByVal i As Integer, _
- ByRef dRevenueQuarterly As Decimal) As Boolean
- 'quarterly revenue minimum and maximum constants
- Const c_dRevenueQuarterlyMin As Decimal = 100000D
- Const c_dRevenueQuarterlyMax As Decimal = 10000000D
- 'error message. includes current values of the min/max constants.
- Dim ErrorMsg As String = String.Format("Please enter an amount between {0:c} and {1:c}!", _
- c_dRevenueQuarterlyMin, c_dRevenueQuarterlyMax)
- 'making sure it isn't the wrong type of data and is within the
- 'acceptable range
- If Decimal.TryParse(txtQuarters(i).Text, dRevenueQuarterly) Then
- If dRevenueQuarterly >= c_dRevenueQuarterlyMin AndAlso _
- dRevenueQuarterly <= c_dRevenueQuarterlyMax Then
- Return True
- End If
- End If
- 'if any of the data is the wrong type or outside the bounds of the
- 'constants, an error message is shown, the first offending textbox
- 'is focused on, and the contained text is 'selected'
- MessageBox.Show(ErrorMsg)
- txtQuarters(i).Focus()
- txtQuarters(i).SelectAll()
- Return False
- End Function
- Private Sub frmRevenue_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- 'constructs most of the form controls at runtime
- Dim x, y As Integer
- 'for next loop that constructs the textboxes and labels. the amount
- 'of iterations is determined by the size of the Quarters array
- For i As Integer = 0 To _iQuarterCnt
- 'creates the label(s)
- lblQuarters(i) = New Label
- 'positions, fills with data, and sizes the labels
- With lblQuarters(i)
- x = 30
- y += 30
- .Location = New Point(x, y)
- .Text = a_sQuarters(i)
- .AutoSize = True
- End With
- 'adds label to the form
- Controls.Add(lblQuarters(i))
- 'creates the textbox(es)
- txtQuarters(i) = New TextBox
- 'positions the textbox(es)
- With txtQuarters(i)
- x += 115
- .Location = New Point(x, y)
- '.TabIndex = i
- End With
- If i = _iQuarterCnt Then
- With txtQuarters(i)
- .Name = "txtRevenueDisplay"
- .Enabled = False
- End With
- End If
- 'adds textbox to the form
- Controls.Add(txtQuarters(i))
- 'ties the created textboxes to the _KeyPress
- 'event handler
- AddHandler txtQuarters(i).KeyPress, _
- AddressOf txtQuarters_KeyPress
- 'Me.Height = y + 200
- 'txtRevenueDisplay.Enabled = False
- Next
- ' I was trying to get the form to draw both the yearly
- ' revenue label and textbox as well. For some reason,
- ' they will not display. And since I messed with this
- ' part of the code, the Me.Height seems to have no effect.
- ' I had used a bit of trial and error to set it to the
- ' right size, but now it doesn't seem to do anything!
- 'lblRevenueDisplay = New Label
- 'With lblRevenueDisplay
- ' x = 30
- ' y += 30
- ' .Location = New Point(x, y)
- ' .Text = "Yearly Revenue"
- ' .AutoSize = True
- 'End With
- 'Controls.Add(lblRevenueDisplay)
- 'txtRevenueDisplay = New TextBox
- 'With txtRevenueDisplay
- ' .ReadOnly = True
- 'End With
- 'Controls.Add(txtRevenueDisplay)
- End Sub
- Private Function SumArray(ByVal a_sum() As Decimal) As Decimal
- Dim sum As Decimal
- 'array containing quarterly revenue is passed to this function,
- 'which uses a for loop to add all the values together, and then
- 'returns that value as a decimal
- For i As Integer = 0 To a_sum.GetUpperBound(0)
- sum += a_sum(i)
- Next
- Return sum
- End Function
- Private Sub txtQuarters_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
- 'Allow only numbers, decimals or backspaces. This was taken
- 'from Demo 3: Daily Temperature
- Select Case e.KeyChar
- Case "0"c To "9"c, "."c, ControlChars.Back
- Case Else
- Beep()
- e.Handled = True
- End Select
- End Sub
- Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
- Dim decQuarterlyRevenue(_iQuarterCnt) As Decimal
- Dim i As Integer = 0
- 'do loop that validates all data contained in the textboxes. if all are
- 'determined to contain valid data, SumArray is called, calculating the
- 'necessary equation, and finally displaying it.
- Do While i < _iQuarterCnt AndAlso _
- IsValidRevenue(i, decQuarterlyRevenue(i))
- i += 1
- If i = _iQuarterCnt Then
- txtRevenueDisplay.Text = Me.SumArray(decQuarterlyRevenue).ToString("c")
- End If
- Loop
- End Sub
- Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
- For i As Integer = 0 To (_iQuarterCnt - 1)
- With txtQuarters(i)
- .Clear()
- End With
- Next
- txtRevenueDisplay.Clear()
- txtQuarters(0).Focus()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement