Home

Example Application: Pledge Distribution

 

Test of the Pledge Distribution Application

Introduction

The numeric up-down is the Microsoft Windows spin button, or spin control, as the object is implemented in the .NET Framework. It is used to specify or select numeric or select a numeric value from a preset range from a minimum to a maximum.

This application uses three up-down controls. Each represents a percentage value. The total value of all three controls must not exceed 100. These values are used to calculate a proportional value that is pledged in a fund-raising for three schools.

 

Practical LearningPractical Learning: Creating the Applications

  1. Start Microsoft Visual Basic
  2. Create a new Windows Application named PledgeDistribution1
  3. In the Solution Explorer, right-click Form1.vb and click Rename 
  4. Type PledgeDistribution.vb and press Enter
  5. Design the form as follows:
     
    Pledge Distribution
     
    Control Text Name Additional Properties
    Form Pledge Distribution   Maximize Box: False
    Label Amount Pledged:    
    TextBox 0.00 txtAmountPledged Text Align: Right
    Label Rotherham College:    
    NumericUpDown      
    Label %    
    TextBox 0.00 txtAmount1 Text Align: Right
    Label Leicester University:    
    NumericUpDown      
    Label %    
    TextBox 0.00 txtAmount2 Text Align: Right
    Label Lars Union Academy:    
    NumericUpDown      
    Label %    
    TextBox 0.00 txtAmount3 Text Align: Right
    Label Message lblMessage  
    Button Close btnClose  
  6. Execute the application to test the form
  7. Close the form and return to your programming environment
  8. On the form, click the top numeric up-down control
  9. In the Properties, click Value, type 50 and press Enter
  10. In the same way, change the following properties for the controls:
     
    Pledge Distribution
    Control Name Additional Properties
    NumericUpDown updAmount1 Value: 50
    NumericUpDown updAmount2 Value: 25
    NumericUpDown updAmount3 Value: 25
  11. Double-click the top up-down control and implement its event as follows:
     
    Private Sub updAmount1ValueChanged(ByVal sender As System.Object, 
                                            ByVal e As System.EventArgs) 
                                            Handles updAmount1.ValueChanged
            Dim AmountPledged As Double
            Dim RateAmount1 As Double
            Dim RateAmount2 As Double
            Dim RateAmount3 As Double
            Dim Amount1 As Double
            Dim Amount2 As Double
            Dim Amount3 As Double
            Dim Rest As Double
    
            ' Get the current value of the amount pledged
            Try
                AmountPledged = CDbl(txtAmountPledged.Text)
            Catch ex As FormatException
                MsgBox("The amount you entered to pledge is not valid")
            End Try
    
          ' Get the percentage that is displaying in the UpDown controls other
            ' than this one
            RateAmount2 = CType(updAmount2.Value, Double)
            RateAmount3 = CType(updAmount3.Value, Double)
        ' To make sure that the total percentage applied on all three UpDown
            ' controls is = 100, get the difference left from subtracting
            ' the values of the other UpDown controls from 100
            ' Use that difference as the Maximum value applied on the current
            ' UpDown control
            updAmount1.Maximum = CType(100 - RateAmount2 - RateAmount3, Double)
            ' Now that we have an appropriate percentage value on the current
            ' UpDown control, retrieve it
            RateAmount1 = CType(updAmount1.Value, Double)
    
            ' Now we can calculate the amount to apply to each institution
            Amount1 = AmountPledged * RateAmount1 / 100
            Amount2 = AmountPledged * RateAmount2 / 100
            Amount3 = AmountPledged * RateAmount3 / 100
          ' We need the difference, if any, left after calculating the amount 
            ' pledged to each institution
            Rest = AmountPledged - Amount1 - Amount2 - Amount3
    
            ' Display the value allocated to each institution 
            txtAmount1.Text = FormatCurrency(Amount1)
            txtAmount2.Text = FormatCurrency(Amount2)
            txtAmount3.Text = FormatCurrency(Amount3)
    
            ' If there is still money left, let the user know
            If Rest > 0 Then
                lblMessage.Text = Rest.ToString("C") & " still to be used"
            Else
                lblMessage.Text = ""
            End If
    End Sub
  12. In the Class Name combo box, select updAmount2
  13. In the Method Name combo box, select ValueChanged and implement the event as follows:
     
    Private Sub updAmount2ValueChanged(ByVal sender As Object, 
                                            ByVal e As System.EventArgs) 
                                            Handles updAmount2.ValueChanged
            Dim AmountPledged As Double
            Dim RateAmount1 As Double
            Dim RateAmount2 As Double
            Dim RateAmount3 As Double
            Dim Amount1 As Double
            Dim Amount2 As Double
            Dim Amount3 As Double
            Dim Rest As Double
    
            Try
                AmountPledged = CDbl(txtAmountPledged.Text)
            Catch ex As FormatException
                MsgBox("The amount you entered to pledge is not valid")
            End Try
    
            RateAmount1 = CType(updAmount1.Value, Double)
            RateAmount3 = CType(updAmount3.Value, Double)
            updAmount2.Maximum = CType(100 - RateAmount1 - RateAmount3, double)
            RateAmount2 = CType(updAmount2.Value, Double)
    
            Amount1 = AmountPledged * RateAmount1 / 100
            Amount2 = AmountPledged * RateAmount2 / 100
            Amount3 = AmountPledged * RateAmount3 / 100
            Rest = AmountPledged - Amount1 - Amount2 - Amount3
    
            txtAmount1.Text = FormatCurrency(Amount1)
            txtAmount2.Text = FormatCurrency(Amount2)
            txtAmount3.Text = FormatCurrency(Amount3)
    
            If Rest > 0 Then
                lblMessage.Text = Rest.ToString("C") & " still to be used"
            Else
                lblMessage.Text = ""
            End If
    End Sub
  14. In the Class Name combo box, select updAmount3
  15. In the Method Name combo box, select ValueChanged and implement the event as follows:
     
    Private Sub updAmount3ValueChanged(ByVal sender As Object, 
                                            ByVal e As System.EventArgs) 
                                            Handles updAmount3.ValueChanged
            Dim AmountPledged As Double
            Dim RateAmount1 As Double
            Dim RateAmount2 As Double
            Dim RateAmount3 As Double
            Dim Amount1 As Double
            Dim Amount2 As Double
            Dim Amount3 As Double
            Dim Rest As Double
    
            Try
                AmountPledged = CDbl(txtAmountPledged.Text)
            Catch ex As FormatException
                MsgBox("The amount you entered to pledge is not valid")
            End Try
    
            RateAmount1 = CType(updAmount1.Value, Double)
            RateAmount2 = CType(updAmount2.Value, Double)
            updAmount3.Maximum = CType(100 - RateAmount1 - RateAmount2, Double)
            RateAmount3 = CType(updAmount3.Value, Double)
    
            Amount1 = AmountPledged * RateAmount1 / 100
            Amount2 = AmountPledged * RateAmount2 / 100
            Amount3 = AmountPledged * RateAmount3 / 100
            Rest = AmountPledged - Amount1 - Amount2 - Amount3
    
            txtAmount1.Text = FormatCurrency(Amount1)
            txtAmount2.Text = FormatCurrency(Amount2)
            txtAmount3.Text = FormatCurrency(Amount3)
    
            If Rest > 0 Then
                lblMessage.Text = Rest.ToString("C") & " still to be used"
            Else
                lblMessage.Text = ""
            End If
    End Sub
  16. In the Class Name combo box, select txtAmountPledged
  17. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtAmountPledgedLeave(ByVal sender As Object, 
                                           ByVal e As System.EventArgs) 
                                           Handles txtAmountPledged.Leave
            ' Make sure the amount pledged text box has a number
            ' If it doesn't, then display the UpDown controls 
            ' so the user cannot use them and cause bad events
            If txtAmountPledged.Text = "" Then
                updAmount1.Enabled = False
                updAmount2.Enabled = False
                updAmount3.Enabled = False
            Else
                updAmount1.Enabled = True
                updAmount2.Enabled = True
                updAmount3.Enabled = True
            End If
    End Sub
  18. In the Class Name combo box, select btnClose
  19. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnCloseClick(ByVal sender As Object, 
                                   ByVal e As System.EventArgs) 
                                   Handles btnClose.Click
            End
    End Sub
  20. Test the application
     
    Test of the Pledge Distribution Application
  21. Close the form and return to your programming environment

Download

 

Home Copyright © 2008-2012 FunctionX