Pledge Distribution

Pledge Distribution

Introduction

In this exercise, we are asked to create an application used to raise money for three educational institutions. To proceed, we will call people on the phone or post the application in an intranet's directory. When the application starts, an amount of money will be entered. Then, the person who is pledging money will specify the percentage of money that can be allocated to each institution. We will use three up down buttons to set the percentages. We must synchronize them so that the total values of the up down buttons must not exceed 100. The amount pledged is then converted with regards to each up down.

Prerequisites:

 

Practical LearningPractical Learning: Launching Microsoft Visual Basic

  1. Create a Windows Form Application named Pledge1
  2. Design the form as follows:
     
    Pledge Distribution
    Control Text Name Additional Properties
    Label Amount Pledged:    
    TextBox 0.00 txtAmountPledged Text Align: Right
    Label Rotherham College:    
    NumericUpDown   updAmount1 Value: 50
    Label %    
    TextBox 0.00 txtAmount1 Text Align: Right
    Label Leicester University:    
    NumericUpDown   updAmount2 Value: 25
    Label %    
    TextBox 0.00 txtAmount2 Text Align: Right
    Label Lars Union Academy:    
    NumericUpDown     Value: 25
    Label %    
    TextBox 0.00 txtAmount3 Text Align: Right
    Label Message lblMessage  
    Button Close btnClose  
  3. Double-click each up down control, the Amount Pledged text box, and the Close button
  4. Implement the events as follows:
     
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    namespace Pledge1
    {
    	/// <summary>
    	/// Summary description for Form1.
    	/// </summary>
    	public class Form1 : System.Windows.Forms.Form
    	{
    		internal System.Windows.Forms.Label lblMessage;
    		internal System.Windows.Forms.Button btnClose;
    		internal System.Windows.Forms.TextBox txtAmount3;
    		internal System.Windows.Forms.Label Label6;
    		internal System.Windows.Forms.NumericUpDown updAmount3;
    		internal System.Windows.Forms.Label Label7;
    		internal System.Windows.Forms.TextBox txtAmount2;
    		internal System.Windows.Forms.Label Label4;
    		internal System.Windows.Forms.NumericUpDown updAmount2;
    		internal System.Windows.Forms.Label Label5;
    		internal System.Windows.Forms.TextBox txtAmount1;
    		internal System.Windows.Forms.Label Label3;
    		internal System.Windows.Forms.NumericUpDown updAmount1;
    		internal System.Windows.Forms.Label Label2;
    		internal System.Windows.Forms.TextBox txtAmountPledged;
    		internal System.Windows.Forms.Label Label1;
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		private System.ComponentModel.Container components = null;
    
    		public Form1()
    		{
    			//
    			// Required for Windows Form Designer support
    			//
    			InitializeComponent();
    
    			//
    			// TODO: Add any constructor code after InitializeComponent call
    			//
    		}
    
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if (components != null) 
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    
    		#region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{
    			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
    			this.lblMessage = new System.Windows.Forms.Label();
    			this.btnClose = new System.Windows.Forms.Button();
    			this.txtAmount3 = new System.Windows.Forms.TextBox();
    			this.Label6 = new System.Windows.Forms.Label();
    			this.updAmount3 = new System.Windows.Forms.NumericUpDown();
    			this.Label7 = new System.Windows.Forms.Label();
    			this.txtAmount2 = new System.Windows.Forms.TextBox();
    			this.Label4 = new System.Windows.Forms.Label();
    			this.updAmount2 = new System.Windows.Forms.NumericUpDown();
    			this.Label5 = new System.Windows.Forms.Label();
    			this.txtAmount1 = new System.Windows.Forms.TextBox();
    			this.Label3 = new System.Windows.Forms.Label();
    			this.updAmount1 = new System.Windows.Forms.NumericUpDown();
    			this.Label2 = new System.Windows.Forms.Label();
    			this.txtAmountPledged = new System.Windows.Forms.TextBox();
    			this.Label1 = new System.Windows.Forms.Label();
    			((System.ComponentModel.ISupportInitialize)(this.updAmount3)).BeginInit();
    			((System.ComponentModel.ISupportInitialize)(this.updAmount2)).BeginInit();
    			((System.ComponentModel.ISupportInitialize)(this.updAmount1)).BeginInit();
    			this.SuspendLayout();
    			// 
    			// lblMessage
    			// 
    			this.lblMessage.Location = new System.Drawing.Point(16, 160);
    			this.lblMessage.Name = "lblMessage";
    			this.lblMessage.Size = new System.Drawing.Size(168, 23);
    			this.lblMessage.TabIndex = 47;
    			this.lblMessage.Text = "Message";
    			// 
    			// btnClose
    			// 
    			this.btnClose.Location = new System.Drawing.Point(200, 160);
    			this.btnClose.Name = "btnClose";
    			this.btnClose.TabIndex = 46;
    			this.btnClose.Text = "Close";
    			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
    			// 
    			// txtAmount3
    			// 
    			this.txtAmount3.Location = new System.Drawing.Point(200, 120);
    			this.txtAmount3.Name = "txtAmount3";
    			this.txtAmount3.Size = new System.Drawing.Size(72, 20);
    			this.txtAmount3.TabIndex = 45;
    			this.txtAmount3.Text = "0.00";
    			this.txtAmount3.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
    			// 
    			// Label6
    			// 
    			this.Label6.Location = new System.Drawing.Point(176, 128);
    			this.Label6.Name = "Label6";
    			this.Label6.Size = new System.Drawing.Size(16, 16);
    			this.Label6.TabIndex = 44;
    			this.Label6.Text = "%";
    			// 
    			// updAmount3
    			// 
    			this.updAmount3.Location = new System.Drawing.Point(128, 120);
    			this.updAmount3.Name = "updAmount3";
    			this.updAmount3.Size = new System.Drawing.Size(48, 20);
    			this.updAmount3.TabIndex = 43;
    			this.updAmount3.Value = new System.Decimal(new int[] {
    																	 25,
    																	 0,
    																	 0,
    																	 0});
    			this.updAmount3.ValueChanged += new System.EventHandler(this.updAmount3_ValueChanged);
    			// 
    			// Label7
    			// 
    			this.Label7.Location = new System.Drawing.Point(16, 128);
    			this.Label7.Name = "Label7";
    			this.Label7.Size = new System.Drawing.Size(112, 20);
    			this.Label7.TabIndex = 42;
    			this.Label7.Text = "Lars Union Academy:";
    			// 
    			// txtAmount2
    			// 
    			this.txtAmount2.Location = new System.Drawing.Point(200, 88);
    			this.txtAmount2.Name = "txtAmount2";
    			this.txtAmount2.Size = new System.Drawing.Size(72, 20);
    			this.txtAmount2.TabIndex = 41;
    			this.txtAmount2.Text = "0.00";
    			this.txtAmount2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
    			// 
    			// Label4
    			// 
    			this.Label4.Location = new System.Drawing.Point(176, 88);
    			this.Label4.Name = "Label4";
    			this.Label4.Size = new System.Drawing.Size(16, 16);
    			this.Label4.TabIndex = 40;
    			this.Label4.Text = "%";
    			// 
    			// updAmount2
    			// 
    			this.updAmount2.Location = new System.Drawing.Point(128, 88);
    			this.updAmount2.Name = "updAmount2";
    			this.updAmount2.Size = new System.Drawing.Size(48, 20);
    			this.updAmount2.TabIndex = 39;
    			this.updAmount2.Value = new System.Decimal(new int[] {
    																	 25,
    																	 0,
    																	 0,
    																	 0});
    			this.updAmount2.ValueChanged += new System.EventHandler(this.updAmount2_ValueChanged);
    			// 
    			// Label5
    			// 
    			this.Label5.Location = new System.Drawing.Point(16, 88);
    			this.Label5.Name = "Label5";
    			this.Label5.Size = new System.Drawing.Size(112, 20);
    			this.Label5.TabIndex = 38;
    			this.Label5.Text = "Leicester University:";
    			// 
    			// txtAmount1
    			// 
    			this.txtAmount1.Location = new System.Drawing.Point(200, 56);
    			this.txtAmount1.Name = "txtAmount1";
    			this.txtAmount1.Size = new System.Drawing.Size(72, 20);
    			this.txtAmount1.TabIndex = 37;
    			this.txtAmount1.Text = "0.00";
    			this.txtAmount1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
    			// 
    			// Label3
    			// 
    			this.Label3.Location = new System.Drawing.Point(176, 64);
    			this.Label3.Name = "Label3";
    			this.Label3.Size = new System.Drawing.Size(16, 16);
    			this.Label3.TabIndex = 36;
    			this.Label3.Text = "%";
    			// 
    			// updAmount1
    			// 
    			this.updAmount1.Location = new System.Drawing.Point(128, 56);
    			this.updAmount1.Name = "updAmount1";
    			this.updAmount1.Size = new System.Drawing.Size(48, 20);
    			this.updAmount1.TabIndex = 35;
    			this.updAmount1.Value = new System.Decimal(new int[] {
    																	 50,
    																	 0,
    																	 0,
    																	 0});
    			this.updAmount1.ValueChanged += new System.EventHandler(this.updAmount1_ValueChanged);
    			// 
    			// Label2
    			// 
    			this.Label2.Location = new System.Drawing.Point(16, 56);
    			this.Label2.Name = "Label2";
    			this.Label2.Size = new System.Drawing.Size(112, 20);
    			this.Label2.TabIndex = 34;
    			this.Label2.Text = "Rotherham College:";
    			// 
    			// txtAmountPledged
    			// 
    			this.txtAmountPledged.Location = new System.Drawing.Point(128, 16);
    			this.txtAmountPledged.Name = "txtAmountPledged";
    			this.txtAmountPledged.Size = new System.Drawing.Size(144, 20);
    			this.txtAmountPledged.TabIndex = 33;
    			this.txtAmountPledged.Text = "0.00";
    			this.txtAmountPledged.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
    			this.txtAmountPledged.TextChanged += new System.EventHandler(this.txtAmountPledged_TextChanged);
    			// 
    			// Label1
    			// 
    			this.Label1.Location = new System.Drawing.Point(16, 16);
    			this.Label1.Name = "Label1";
    			this.Label1.Size = new System.Drawing.Size(96, 16);
    			this.Label1.TabIndex = 32;
    			this.Label1.Text = "Amount Pledged:";
    			// 
    			// Form1
    			// 
    			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    			this.ClientSize = new System.Drawing.Size(288, 198);
    			this.Controls.Add(this.lblMessage);
    			this.Controls.Add(this.btnClose);
    			this.Controls.Add(this.txtAmount3);
    			this.Controls.Add(this.Label6);
    			this.Controls.Add(this.updAmount3);
    			this.Controls.Add(this.Label7);
    			this.Controls.Add(this.txtAmount2);
    			this.Controls.Add(this.Label4);
    			this.Controls.Add(this.updAmount2);
    			this.Controls.Add(this.Label5);
    			this.Controls.Add(this.txtAmount1);
    			this.Controls.Add(this.Label3);
    			this.Controls.Add(this.updAmount1);
    			this.Controls.Add(this.Label2);
    			this.Controls.Add(this.txtAmountPledged);
    			this.Controls.Add(this.Label1);
    			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
    			this.Name = "Form1";
    			this.Text = "Pledge Distribution";
    			((System.ComponentModel.ISupportInitialize)(this.updAmount3)).EndInit();
    			((System.ComponentModel.ISupportInitialize)(this.updAmount2)).EndInit();
    			((System.ComponentModel.ISupportInitialize)(this.updAmount1)).EndInit();
    			this.ResumeLayout(false);
    
    		}
    		#endregion
    
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main() 
    		{
    			Application.Run(new Form1());
    		}
    
    		private void updAmount1_ValueChanged(object sender, System.EventArgs e)
    		{
    			double AmountPledged,
    			       RateAmount1, RateAmount2, RateAmount3,
    				   Amount1, Amount2, Amount3,
    				   Rest;
    
    			AmountPledged = double.Parse(this.txtAmountPledged.Text);
    
    			RateAmount2 = (double)this.updAmount2.Value;
    			RateAmount3 = (double)this.updAmount3.Value;
    			this.updAmount1.Maximum = (decimal)(100 - RateAmount2 - RateAmount3);
    			RateAmount1 = (double)this.updAmount1.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 = "";
    		}
    
    		private void updAmount2_ValueChanged(object sender, System.EventArgs e)
    		{
    			double AmountPledged,
    				RateAmount1, RateAmount2, RateAmount3,
    				Amount1, Amount2, Amount3,
    				Rest;
    
    			AmountPledged = double.Parse(this.txtAmountPledged.Text);
    
    			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 = "";
    		}
    
    		private void updAmount3_ValueChanged(object sender, System.EventArgs e)
    		{
    			double AmountPledged,
    				RateAmount1, RateAmount2, RateAmount3,
    				Amount1, Amount2, Amount3,
    				Rest;
    
    			AmountPledged = double.Parse(this.txtAmountPledged.Text);
    
    			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 = "";
    		}
    
    		private void txtAmountPledged_TextChanged(object sender, System.EventArgs e)
    		{
    			if( this.txtAmountPledged.Text.Equals("") )
    			{
    				this.updAmount1.Enabled = false;
    				this.updAmount2.Enabled = false;
    				this.updAmount3.Enabled = false;
    			}
    			else
    			{
    				this.updAmount1.Enabled = true;
    				this.updAmount2.Enabled = true;
    				this.updAmount3.Enabled = true;
    			}
    		}
    
    		private void btnClose_Click(object sender, System.EventArgs e)
    		{
    			Close();
    		}
    	}
    }
  5. Test the application
 

Home Copyright 2004 FunctionX, Inc.