Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public Class frmRevenue
  2.     'array holding strings that are destined to be the labels of the textboxes
  3.    'that are created on form load. i use the number of the quarter as the
  4.    'hotkey for each textbox. last string is for yearly revenue label
  5.    Private a_sQuarters() As String = {"Quarter &1 Revenue", "Quarter &2 Revenue", _
  6.                                        "Quarter &3 Revenue", "Quarter &4 Revenue", _
  7.                                        "Yearly Revenue"}
  8.     'array to be used to hold the values of the individual
  9.    'quarters
  10.    Private a_dQuarters(_iQuarterCnt) As Decimal
  11.     Private _iQuarterCnt As Integer = a_sQuarters.GetUpperBound(0)
  12.     Private txtQuarters(_iQuarterCnt) As TextBox
  13.     Private lblQuarters(_iQuarterCnt) As Label
  14.     Private txtRevenueDisplay As TextBox
  15.     'Private lblRevenueDisplay As Label
  16.  
  17.  
  18.     Private Function IsValidRevenue(ByVal i As Integer, _
  19.                                     ByRef dRevenueQuarterly As Decimal) As Boolean
  20.         'quarterly revenue minimum and maximum constants
  21.        Const c_dRevenueQuarterlyMin As Decimal = 100000D
  22.         Const c_dRevenueQuarterlyMax As Decimal = 10000000D
  23.         'error message. includes current values of the min/max constants.
  24.        Dim ErrorMsg As String = String.Format("Please enter an amount between {0:c} and {1:c}!", _
  25.                                  c_dRevenueQuarterlyMin, c_dRevenueQuarterlyMax)
  26.         'making sure it isn't the wrong type of data and is within the
  27.        'acceptable range
  28.        If Decimal.TryParse(txtQuarters(i).Text, dRevenueQuarterly) Then
  29.             If dRevenueQuarterly >= c_dRevenueQuarterlyMin AndAlso _
  30.                dRevenueQuarterly <= c_dRevenueQuarterlyMax Then
  31.                 Return True
  32.             End If
  33.         End If
  34.         'if any of the data is the wrong type or outside the bounds of the
  35.        'constants, an error message is shown, the first offending textbox
  36.        'is focused on, and the contained text is 'selected'
  37.        MessageBox.Show(ErrorMsg)
  38.         txtQuarters(i).Focus()
  39.         txtQuarters(i).SelectAll()
  40.         Return False
  41.  
  42.     End Function
  43.  
  44.     Private Sub frmRevenue_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  45.         'constructs most of the form controls at runtime
  46.        Dim x, y As Integer
  47.         'for next loop that constructs the textboxes and labels. the amount
  48.        'of iterations is determined by the size of the Quarters array
  49.        For i As Integer = 0 To _iQuarterCnt
  50.             'creates the label(s)
  51.            lblQuarters(i) = New Label
  52.             'positions, fills with data, and sizes the labels
  53.            With lblQuarters(i)
  54.                 x = 30
  55.                 y += 30
  56.                 .Location = New Point(x, y)
  57.                 .Text = a_sQuarters(i)
  58.                 .AutoSize = True
  59.             End With
  60.             'adds label to the form
  61.            Controls.Add(lblQuarters(i))
  62.             'creates the textbox(es)
  63.            txtQuarters(i) = New TextBox
  64.             'positions the textbox(es)
  65.            With txtQuarters(i)
  66.                 x += 115
  67.                 .Location = New Point(x, y)
  68.                 '.TabIndex = i
  69.            End With
  70.             If i = _iQuarterCnt Then
  71.                 With txtQuarters(i)
  72.                     .Name = "txtRevenueDisplay"
  73.                     .Enabled = False
  74.                 End With
  75.             End If
  76.             'adds textbox to the form
  77.            Controls.Add(txtQuarters(i))
  78.             'ties the created textboxes to the _KeyPress
  79.            'event handler
  80.            AddHandler txtQuarters(i).KeyPress, _
  81.                        AddressOf txtQuarters_KeyPress
  82.             'Me.Height = y + 200
  83.            'txtRevenueDisplay.Enabled = False
  84.  
  85.         Next
  86.         ' I was trying to get the form to draw both the yearly
  87.        ' revenue label and textbox as well. For some reason,
  88.        ' they will not display. And since I messed with this
  89.        ' part of the code, the Me.Height seems to have no effect.
  90.        ' I had used a bit of trial and error to set it to the
  91.        ' right size, but now it doesn't seem to do anything!
  92.        'lblRevenueDisplay = New Label
  93.        'With lblRevenueDisplay
  94.        '    x = 30
  95.        '    y += 30
  96.        '    .Location = New Point(x, y)
  97.        '    .Text = "Yearly Revenue"
  98.        '    .AutoSize = True
  99.        'End With
  100.  
  101.         'Controls.Add(lblRevenueDisplay)
  102.  
  103.         'txtRevenueDisplay = New TextBox
  104.        'With txtRevenueDisplay
  105.        '    .ReadOnly = True
  106.        'End With
  107.  
  108.         'Controls.Add(txtRevenueDisplay)
  109.  
  110.  
  111.  
  112.     End Sub
  113.  
  114.     Private Function SumArray(ByVal a_sum() As Decimal) As Decimal
  115.         Dim sum As Decimal
  116.         'array containing quarterly revenue is passed to this function,
  117.        'which uses a for loop to add all the values together, and then
  118.        'returns that value as a decimal
  119.        For i As Integer = 0 To a_sum.GetUpperBound(0)
  120.             sum += a_sum(i)
  121.         Next
  122.         Return sum
  123.     End Function
  124.  
  125.     Private Sub txtQuarters_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
  126.         'Allow only numbers, decimals or backspaces. This was taken
  127.        'from Demo 3: Daily Temperature
  128.        Select Case e.KeyChar
  129.             Case "0"c To "9"c, "."c, ControlChars.Back
  130.             Case Else
  131.                 Beep()
  132.                 e.Handled = True
  133.         End Select
  134.     End Sub
  135.  
  136.     Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
  137.  
  138.         Dim decQuarterlyRevenue(_iQuarterCnt) As Decimal
  139.         Dim i As Integer = 0
  140.         'do loop that validates all data contained in the textboxes. if all are
  141.        'determined to contain valid data, SumArray is called, calculating the
  142.        'necessary equation, and finally displaying it.
  143.        Do While i < _iQuarterCnt AndAlso _
  144.                         IsValidRevenue(i, decQuarterlyRevenue(i))
  145.             i += 1
  146.             If i = _iQuarterCnt Then
  147.                 txtRevenueDisplay.Text = Me.SumArray(decQuarterlyRevenue).ToString("c")
  148.             End If
  149.         Loop
  150.  
  151.     End Sub
  152.  
  153.     Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
  154.         For i As Integer = 0 To (_iQuarterCnt - 1)
  155.             With txtQuarters(i)
  156.                 .Clear()
  157.             End With
  158.         Next
  159.         txtRevenueDisplay.Clear()
  160.         txtQuarters(0).Focus()
  161.     End Sub
  162. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement