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 Application

  1. Start Microsoft Visual C#
  2. Create a new Windows Application named PledgeDistribution1
  3. In the Solution Explorer, right-click Form1.cs and click Rename 
  4. Type PledgeDistribution.cs 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 void updAmount1_ValueChanged(object sender, EventArgs e)
    {
                double AmountPledged = 0.00D;
                double RateAmount1, RateAmount2, RateAmount3,
                       Amount1, Amount2, Amount3,
                       Rest;
    
                // Get the current value of the amount pledged
                try
                {
                    AmountPledged = double.Parse(this.txtAmountPledged.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("The amount you entered to pledge is not valid");
                }
    
                // Get the percentage that is displaying in the UpDown controls other
                // than this one
                RateAmount2 = (double)this.updAmount2.Value;
                RateAmount3 = (double)this.updAmount3.Value;
                // 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
                this.updAmount1.Maximum = (decimal)(100 - RateAmount2 - RateAmount3);
                // Now that we have an appropriate percentage value on the current
                // UpDown control, retrieve it
                RateAmount1 = (double)this.updAmount1.Value;
    
                // 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 
                this.txtAmount1.Text = Amount1.ToString("C");
                this.txtAmount2.Text = Amount2.ToString("C");
                this.txtAmount3.Text = Amount3.ToString("C");
    
                // If there is still money left, let the user know
                if (Rest > 0)
                    this.lblMessage.Text = Rest.ToString("C") + " still to be used";
                else
                    this.lblMessage.Text = "";
    }
  12. Return to the form
  13. Double-click the middle up-down control and implement its event as follows:
     
    private void updAmount2_ValueChanged(object sender, EventArgs e)
    {
                double AmountPledged = 0.00D;
                double RateAmount1, RateAmount2, RateAmount3,
                    Amount1, Amount2, Amount3,
                    Rest;
    
                try
                {
                    AmountPledged = double.Parse(this.txtAmountPledged.Text);
    
                }
                catch (FormatException)
                {
                    MessageBox.Show("The amount you entered to pledge is not valid");
                }
    
                RateAmount1 = (double)this.updAmount1.Value;
                RateAmount3 = (double)this.updAmount3.Value;
                this.updAmount2.Maximum = (decimal)(100 - RateAmount1 - RateAmount3);
                RateAmount2 = (double)this.updAmount2.Value;
    
                Amount1 = AmountPledged * RateAmount1 / 100;
                Amount2 = AmountPledged * RateAmount2 / 100;
                Amount3 = AmountPledged * RateAmount3 / 100;
                Rest = AmountPledged - Amount1 - Amount2 - Amount3;
    
                this.txtAmount1.Text = Amount1.ToString("C");
                this.txtAmount2.Text = Amount2.ToString("C");
                this.txtAmount3.Text = Amount3.ToString("C");
    
                if (Rest > 0)
                    this.lblMessage.Text = Rest.ToString("C") + " still to be used";
                else
                    this.lblMessage.Text = "";
    }
  14. Return to the form
  15. Double-click the bottom up-down control and implement its event as follows:
     
    private void updAmount3_ValueChanged(object sender, EventArgs e)
    {
                double AmountPledged = 0.00D;
                double RateAmount1, RateAmount2, RateAmount3,
                    Amount1, Amount2, Amount3,
                    Rest;
    
                try
                {
                    AmountPledged = double.Parse(this.txtAmountPledged.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("The amount you entered to pledge is not valid");
                }
    
                RateAmount1 = (double)this.updAmount1.Value;
                RateAmount2 = (double)this.updAmount2.Value;
                this.updAmount3.Maximum = (decimal)(100 - RateAmount1 - RateAmount2);
                RateAmount3 = (double)this.updAmount3.Value;
    
                Amount1 = AmountPledged * RateAmount1 / 100;
                Amount2 = AmountPledged * RateAmount2 / 100;
                Amount3 = AmountPledged * RateAmount3 / 100;
                Rest = AmountPledged - Amount1 - Amount2 - Amount3;
    
                this.txtAmount1.Text = Amount1.ToString("C");
                this.txtAmount2.Text = Amount2.ToString("C");
                this.txtAmount3.Text = Amount3.ToString("C");
    
                if (Rest > 0)
                    this.lblMessage.Text = Rest.ToString("C") + " still to be used";
                else
                    this.lblMessage.Text = "";
    }
  16. Return to the form and click (once) the Amount Pledged text box
  17. In the Properties window, click the Events button
  18. Double-click the Leave field to generate its event and implement it as follows:
     
    private void txtAmountPledged_Leave(object sender, EventArgs e)
    {
            // 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 == "" )
            {
                    updAmount1.Enabled = false;
                    updAmount2.Enabled = false;
                    updAmount3.Enabled = false;
            }
            else
            {
                    updAmount1.Enabled = true;
                    updAmount2.Enabled = true;
                    updAmount3.Enabled = true;
            }
    }
  19. Return to the form 
  20. Double-click the Close button and implement its Click event its event as follows:
     
    private void btnClose_Click(object sender, System.EventArgs e)
    {
    	Close();
    }
  21. Test the application
     
    Test of the Pledge Distribution Application
  22. Close the form and return to your programming environment
 

Home Copyright © 2007 FunctionX, Inc.