|
Example Application: Pledge Distribution |
|
|
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.
|
Application:
Creating the Application
|
|
- Start Microsoft Visual C#
- Create a new Windows Application named PledgeDistribution1
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type PledgeDistribution.cs and press Enter
- Design the form as follows:
|
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 |
|
|
- Execute the application to test the form
- Close the form and return to your programming environment
- On the form, click the top numeric up-down control
- In the Properties, click Value, type 50 and press Enter
- In the same way, change the following properties for the controls:
|
Control |
Name |
Additional Properties |
NumericUpDown |
updAmount1 |
Value: 50 |
NumericUpDown |
updAmount2 |
Value: 25 |
NumericUpDown |
updAmount3 |
Value: 25 |
|
- 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 = "";
}
- Return to the form
- 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 = "";
}
- Return to the form
- 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 = "";
}
- Return to the form and click (once) the Amount Pledged text box
- In the Properties window, click the Events button
- 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;
}
}
- Return to the form
- Double-click the Close button and implement its Click event its
event as follows:
private void btnClose_Click(object sender, System.EventArgs e)
{
Close();
}
- Test the application
- Close the form and return to your programming environment
|
|