Practical
Learning: Creating the Applications
|
|
- Start Microsoft Visual Basic
- Create a new Windows Application named PledgeDistribution1
- In the Solution Explorer, right-click Form1.vb and click Rename
- Type PledgeDistribution.vb 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 Sub updAmount1ValueChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Handles updAmount1.ValueChanged
Dim AmountPledged As Double
Dim RateAmount1 As Double
Dim RateAmount2 As Double
Dim RateAmount3 As Double
Dim Amount1 As Double
Dim Amount2 As Double
Dim Amount3 As Double
Dim Rest As Double
' Get the current value of the amount pledged
Try
AmountPledged = CDbl(txtAmountPledged.Text)
Catch ex As FormatException
MsgBox("The amount you entered to pledge is not valid")
End Try
' Get the percentage that is displaying in the UpDown controls other
' than this one
RateAmount2 = CType(updAmount2.Value, Double)
RateAmount3 = CType(updAmount3.Value, Double)
' 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
updAmount1.Maximum = CType(100 - RateAmount2 - RateAmount3, Double)
' Now that we have an appropriate percentage value on the current
' UpDown control, retrieve it
RateAmount1 = CType(updAmount1.Value, Double)
' 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
txtAmount1.Text = FormatCurrency(Amount1)
txtAmount2.Text = FormatCurrency(Amount2)
txtAmount3.Text = FormatCurrency(Amount3)
' If there is still money left, let the user know
If Rest > 0 Then
lblMessage.Text = Rest.ToString("C") & " still to be used"
Else
lblMessage.Text = ""
End If
End Sub
|
- In the Class Name combo box, select updAmount2
- In the Method Name combo box, select ValueChanged and implement the
event as follows:
Private Sub updAmount2ValueChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles updAmount2.ValueChanged
Dim AmountPledged As Double
Dim RateAmount1 As Double
Dim RateAmount2 As Double
Dim RateAmount3 As Double
Dim Amount1 As Double
Dim Amount2 As Double
Dim Amount3 As Double
Dim Rest As Double
Try
AmountPledged = CDbl(txtAmountPledged.Text)
Catch ex As FormatException
MsgBox("The amount you entered to pledge is not valid")
End Try
RateAmount1 = CType(updAmount1.Value, Double)
RateAmount3 = CType(updAmount3.Value, Double)
updAmount2.Maximum = CType(100 - RateAmount1 - RateAmount3, double)
RateAmount2 = CType(updAmount2.Value, Double)
Amount1 = AmountPledged * RateAmount1 / 100
Amount2 = AmountPledged * RateAmount2 / 100
Amount3 = AmountPledged * RateAmount3 / 100
Rest = AmountPledged - Amount1 - Amount2 - Amount3
txtAmount1.Text = FormatCurrency(Amount1)
txtAmount2.Text = FormatCurrency(Amount2)
txtAmount3.Text = FormatCurrency(Amount3)
If Rest > 0 Then
lblMessage.Text = Rest.ToString("C") & " still to be used"
Else
lblMessage.Text = ""
End If
End Sub
|
- In the Class Name combo box, select updAmount3
- In the Method Name combo box, select ValueChanged and implement the
event as follows:
Private Sub updAmount3ValueChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles updAmount3.ValueChanged
Dim AmountPledged As Double
Dim RateAmount1 As Double
Dim RateAmount2 As Double
Dim RateAmount3 As Double
Dim Amount1 As Double
Dim Amount2 As Double
Dim Amount3 As Double
Dim Rest As Double
Try
AmountPledged = CDbl(txtAmountPledged.Text)
Catch ex As FormatException
MsgBox("The amount you entered to pledge is not valid")
End Try
RateAmount1 = CType(updAmount1.Value, Double)
RateAmount2 = CType(updAmount2.Value, Double)
updAmount3.Maximum = CType(100 - RateAmount1 - RateAmount2, Double)
RateAmount3 = CType(updAmount3.Value, Double)
Amount1 = AmountPledged * RateAmount1 / 100
Amount2 = AmountPledged * RateAmount2 / 100
Amount3 = AmountPledged * RateAmount3 / 100
Rest = AmountPledged - Amount1 - Amount2 - Amount3
txtAmount1.Text = FormatCurrency(Amount1)
txtAmount2.Text = FormatCurrency(Amount2)
txtAmount3.Text = FormatCurrency(Amount3)
If Rest > 0 Then
lblMessage.Text = Rest.ToString("C") & " still to be used"
Else
lblMessage.Text = ""
End If
End Sub
|
- In the Class Name combo box, select txtAmountPledged
- In the Method Name combo box, select Leave and implement the event as
follows:
Private Sub txtAmountPledgedLeave(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles txtAmountPledged.Leave
' 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 = "" Then
updAmount1.Enabled = False
updAmount2.Enabled = False
updAmount3.Enabled = False
Else
updAmount1.Enabled = True
updAmount2.Enabled = True
updAmount3.Enabled = True
End If
End Sub
|
- In the Class Name combo box, select btnClose
- In the Method Name combo box, select Click and implement the event as
follows:
Private Sub btnCloseClick(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles btnClose.Click
End
End Sub
|
- Test the application
- Close the form and return to your programming environment
Download
|
|