Example Application: Pledge Distribution |
|
Description
In the .NET Framework, the spin button is represented as the up/down control. It is used to select a value in a range. This application explores its characteristics.
Practical Learning: Creating the Application
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 |
Control | Name | Additional Properties |
NumericUpDown | updAmount1 | Value: 50 |
NumericUpDown | updAmount2 | Value: 25 |
NumericUpDown | updAmount3 | Value: 25 |
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 = ""; }
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 = ""; }
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 = ""; }
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; } }
private void btnClose_Click(object sender, System.EventArgs e) { Close(); }
|
||
Home | Copyright © 2010-2022, FunctionX | |
|