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 50 updAmount1  
    Label %    
    TextBox 0.00 txtAmount1 Text Align: Right
    Label Leicester University:    
    NumericUpDown 25 updAmount2  
    Label %    
    TextBox 0.00 txtAmount2 Text Align: Right
    Label Lars Union Academy:    
    NumericUpDown 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:
     
    #pragma once
    
    
    namespace Pledge1
    {
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    	/// <summary> 
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the 
    	///          'Resource File Name' property for the managed resource compiler tool 
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public __gc class Form1 : public System::Windows::Forms::Form
    	{	
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    		}
      
    	protected:
    		void Dispose(Boolean disposing)
    		{
    			if (disposing && components)
    			{
    				components->Dispose();
    			}
    			__super::Dispose(disposing);
    		}
    	public private: System::Windows::Forms::Label *  lblMessage;
    	public private: System::Windows::Forms::Button *  btnClose;
    	public private: System::Windows::Forms::TextBox *  txtAmount3;
    	public private: System::Windows::Forms::Label *  Label6;
    	public private: System::Windows::Forms::NumericUpDown *  updAmount3;
    	public private: System::Windows::Forms::Label *  Label7;
    	public private: System::Windows::Forms::TextBox *  txtAmount2;
    	public private: System::Windows::Forms::Label *  Label4;
    	public private: System::Windows::Forms::NumericUpDown *  updAmount2;
    	public private: System::Windows::Forms::Label *  Label5;
    	public private: System::Windows::Forms::TextBox *  txtAmount1;
    	public private: System::Windows::Forms::Label *  Label3;
    	public private: System::Windows::Forms::NumericUpDown *  updAmount1;
    	public private: System::Windows::Forms::Label *  Label2;
    	public private: System::Windows::Forms::TextBox *  txtAmountPledged;
    	public private: System::Windows::Forms::Label *  Label1;
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container * components;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			System::Resources::ResourceManager *  resources = new System::Resources::ResourceManager(__typeof(Pledge1::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();
    			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->updAmount3))->BeginInit();
    			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->updAmount2))->BeginInit();
    			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->updAmount1))->BeginInit();
    			this->SuspendLayout();
    			// 
    			// lblMessage
    			// 
    			this->lblMessage->Location = System::Drawing::Point(16, 160);
    			this->lblMessage->Name = S"lblMessage";
    			this->lblMessage->Size = System::Drawing::Size(168, 23);
    			this->lblMessage->TabIndex = 31;
    			this->lblMessage->Text = S"Message";
    			// 
    			// btnClose
    			// 
    			this->btnClose->Location = System::Drawing::Point(200, 157);
    			this->btnClose->Name = S"btnClose";
    			this->btnClose->TabIndex = 30;
    			this->btnClose->Text = S"Close";
    			this->btnClose->Click += new System::EventHandler(this, btnClose_Click);
    			// 
    			// txtAmount3
    			// 
    			this->txtAmount3->Location = System::Drawing::Point(200, 120);
    			this->txtAmount3->Name = S"txtAmount3";
    			this->txtAmount3->Size = System::Drawing::Size(72, 20);
    			this->txtAmount3->TabIndex = 29;
    			this->txtAmount3->Text = S"0.00";
    			this->txtAmount3->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
    			// 
    			// Label6
    			// 
    			this->Label6->Location = System::Drawing::Point(176, 128);
    			this->Label6->Name = S"Label6";
    			this->Label6->Size = System::Drawing::Size(16, 16);
    			this->Label6->TabIndex = 28;
    			this->Label6->Text = S"%";
    			// 
    			// updAmount3
    			// 
    			this->updAmount3->Location = System::Drawing::Point(128, 120);
    			this->updAmount3->Name = S"updAmount3";
    			this->updAmount3->Size = System::Drawing::Size(48, 20);
    			this->updAmount3->TabIndex = 27;
    			System::Int32 __mcTemp__1[] = new System::Int32[4];
    			__mcTemp__1[0] = 25;
    			__mcTemp__1[1] = 0;
    			__mcTemp__1[2] = 0;
    			__mcTemp__1[3] = 0;
    			this->updAmount3->Value = System::Decimal(__mcTemp__1);
    			this->updAmount3->ValueChanged += new System::EventHandler(this, updAmount3_ValueChanged);
    			// 
    			// Label7
    			// 
    			this->Label7->Location = System::Drawing::Point(16, 128);
    			this->Label7->Name = S"Label7";
    			this->Label7->Size = System::Drawing::Size(112, 20);
    			this->Label7->TabIndex = 26;
    			this->Label7->Text = S"Lars Union Academy:";
    			// 
    			// txtAmount2
    			// 
    			this->txtAmount2->Location = System::Drawing::Point(200, 88);
    			this->txtAmount2->Name = S"txtAmount2";
    			this->txtAmount2->Size = System::Drawing::Size(72, 20);
    			this->txtAmount2->TabIndex = 25;
    			this->txtAmount2->Text = S"0.00";
    			this->txtAmount2->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
    			// 
    			// Label4
    			// 
    			this->Label4->Location = System::Drawing::Point(176, 88);
    			this->Label4->Name = S"Label4";
    			this->Label4->Size = System::Drawing::Size(16, 16);
    			this->Label4->TabIndex = 24;
    			this->Label4->Text = S"%";
    			// 
    			// updAmount2
    			// 
    			this->updAmount2->Location = System::Drawing::Point(128, 88);
    			this->updAmount2->Name = S"updAmount2";
    			this->updAmount2->Size = System::Drawing::Size(48, 20);
    			this->updAmount2->TabIndex = 23;
    			System::Int32 __mcTemp__2[] = new System::Int32[4];
    			__mcTemp__2[0] = 25;
    			__mcTemp__2[1] = 0;
    			__mcTemp__2[2] = 0;
    			__mcTemp__2[3] = 0;
    			this->updAmount2->Value = System::Decimal(__mcTemp__2);
    			this->updAmount2->ValueChanged += new System::EventHandler(this, updAmount2_ValueChanged);
    			// 
    			// Label5
    			// 
    			this->Label5->Location = System::Drawing::Point(16, 88);
    			this->Label5->Name = S"Label5";
    			this->Label5->Size = System::Drawing::Size(112, 20);
    			this->Label5->TabIndex = 22;
    			this->Label5->Text = S"Leicester University:";
    			// 
    			// txtAmount1
    			// 
    			this->txtAmount1->Location = System::Drawing::Point(200, 56);
    			this->txtAmount1->Name = S"txtAmount1";
    			this->txtAmount1->Size = System::Drawing::Size(72, 20);
    			this->txtAmount1->TabIndex = 21;
    			this->txtAmount1->Text = S"0.00";
    			this->txtAmount1->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
    			// 
    			// Label3
    			// 
    			this->Label3->Location = System::Drawing::Point(176, 64);
    			this->Label3->Name = S"Label3";
    			this->Label3->Size = System::Drawing::Size(16, 16);
    			this->Label3->TabIndex = 20;
    			this->Label3->Text = S"%";
    			// 
    			// updAmount1
    			// 
    			this->updAmount1->Location = System::Drawing::Point(128, 56);
    			this->updAmount1->Name = S"updAmount1";
    			this->updAmount1->Size = System::Drawing::Size(48, 20);
    			this->updAmount1->TabIndex = 19;
    			System::Int32 __mcTemp__3[] = new System::Int32[4];
    			__mcTemp__3[0] = 50;
    			__mcTemp__3[1] = 0;
    			__mcTemp__3[2] = 0;
    			__mcTemp__3[3] = 0;
    			this->updAmount1->Value = System::Decimal(__mcTemp__3);
    			this->updAmount1->ValueChanged += new System::EventHandler(this, updAmount1_ValueChanged);
    			// 
    			// Label2
    			// 
    			this->Label2->Location = System::Drawing::Point(16, 56);
    			this->Label2->Name = S"Label2";
    			this->Label2->Size = System::Drawing::Size(112, 20);
    			this->Label2->TabIndex = 18;
    			this->Label2->Text = S"Rotherham College:";
    			// 
    			// txtAmountPledged
    			// 
    			this->txtAmountPledged->Location = System::Drawing::Point(128, 16);
    			this->txtAmountPledged->Name = S"txtAmountPledged";
    			this->txtAmountPledged->Size = System::Drawing::Size(144, 20);
    			this->txtAmountPledged->TabIndex = 17;
    			this->txtAmountPledged->Text = S"0.00";
    			this->txtAmountPledged->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
    			this->txtAmountPledged->TextChanged += new System::EventHandler(this, txtAmountPledged_TextChanged);
    			// 
    			// Label1
    			// 
    			this->Label1->Location = System::Drawing::Point(16, 16);
    			this->Label1->Name = S"Label1";
    			this->Label1->Size = System::Drawing::Size(96, 16);
    			this->Label1->TabIndex = 16;
    			this->Label1->Text = S"Amount Pledged:";
    			// 
    			// Form1
    			// 
    			this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
    			this->ClientSize = 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->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedDialog;
    			this->Icon = (__try_cast<System::Drawing::Icon *  >(resources->GetObject(S"$this.Icon")));
    			this->MaximizeBox = false;
    			this->MinimizeBox = false;
    			this->Name = S"Form1";
    			this->Text = S"Pledge Distribution";
    			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->updAmount3))->EndInit();
    			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->updAmount2))->EndInit();
    			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->updAmount1))->EndInit();
    			this->ResumeLayout(false);
    
    		}	
    	private: System::Void updAmount1_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    			 {
    				 double AmountPledged,
    					    RateAmount1, RateAmount2, RateAmount3,
    						Amount1, Amount2, Amount3,
    						Rest;
    
    				 AmountPledged = this->txtAmountPledged->Text->ToDouble(0);
    
    				 RateAmount2 = static_cast<double>(this->updAmount2->Value);
    				 RateAmount3 = static_cast<double>(this->updAmount3->Value);
    				 this->updAmount1->Maximum = 100 - RateAmount2 - RateAmount3;
    				 RateAmount1 = static_cast<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 = String::Concat(Rest.ToString("C"), " still to be used");
    				 else
    					 this->lblMessage->Text = "";
    			 }
    
    private: System::Void updAmount2_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 double AmountPledged,
    					    RateAmount1, RateAmount2, RateAmount3,
    						Amount1, Amount2, Amount3,
    						Rest;
    
    			 AmountPledged = this->txtAmountPledged->Text->ToDouble(0);
    
    			 RateAmount1 = static_cast<double>(this->updAmount1->Value);
    			 RateAmount3 = static_cast<double>(this->updAmount3->Value);
    			 this->updAmount2->Maximum = 100 - RateAmount1 - RateAmount3;
    			 RateAmount2 = static_cast<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 = String::Concat(Rest.ToString("C"), " still to be used");
    			 else
    				 this->lblMessage->Text = "";
    		 }
    
    private: System::Void updAmount3_ValueChanged(System::Object *  sender, System::EventArgs *  e)
    		 {
    			  double AmountPledged,
    					    RateAmount1, RateAmount2, RateAmount3,
    						Amount1, Amount2, Amount3,
    						Rest;
    
    			  AmountPledged = this->txtAmountPledged->Text->ToDouble(0);
    
    			  RateAmount1 = static_cast<double>(this->updAmount1->Value);
    			  RateAmount2 = static_cast<double>(this->updAmount2->Value);
    			  this->updAmount3->Maximum = 100 - RateAmount1 - RateAmount2;
    			  RateAmount3 = static_cast<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 = String::Concat(Rest.ToString("C"), " still to be used");
    			  else
    				  this->lblMessage->Text = "";
    		 }
    
    private: System::Void txtAmountPledged_TextChanged(System::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: System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 Close();
    		 }
    
    };
    }  
  5. Test the application
 

Home Copyright © 2004-2014 FunctionX, Inc.