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:

  • Dialog Boxes
  • Label
  • TextBox
  • NumericUpDown
  • Button

 

Practical LearningPractical Learning: Managing a Pledge

  1. Create a Windows 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:
     
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    
        Public Sub New()
            MyBase.New()
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
    
        End Sub
    
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
    
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
    
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents txtAmountPledged As System.Windows.Forms.TextBox
        Friend WithEvents Label2 As System.Windows.Forms.Label
        Friend WithEvents updAmount1 As System.Windows.Forms.NumericUpDown
        Friend WithEvents Label3 As System.Windows.Forms.Label
        Friend WithEvents txtAmount1 As System.Windows.Forms.TextBox
        Friend WithEvents txtAmount2 As System.Windows.Forms.TextBox
        Friend WithEvents Label4 As System.Windows.Forms.Label
        Friend WithEvents updAmount2 As System.Windows.Forms.NumericUpDown
        Friend WithEvents Label5 As System.Windows.Forms.Label
        Friend WithEvents txtAmount3 As System.Windows.Forms.TextBox
        Friend WithEvents Label6 As System.Windows.Forms.Label
        Friend WithEvents updAmount3 As System.Windows.Forms.NumericUpDown
        Friend WithEvents Label7 As System.Windows.Forms.Label
        Friend WithEvents btnClose As System.Windows.Forms.Button
        Friend WithEvents lblMessage As System.Windows.Forms.Label
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
            Me.Label1 = New System.Windows.Forms.Label
            Me.txtAmountPledged = New System.Windows.Forms.TextBox
            Me.Label2 = New System.Windows.Forms.Label
            Me.updAmount1 = New System.Windows.Forms.NumericUpDown
            Me.Label3 = New System.Windows.Forms.Label
            Me.txtAmount1 = New System.Windows.Forms.TextBox
            Me.txtAmount2 = New System.Windows.Forms.TextBox
            Me.Label4 = New System.Windows.Forms.Label
            Me.updAmount2 = New System.Windows.Forms.NumericUpDown
            Me.Label5 = New System.Windows.Forms.Label
            Me.txtAmount3 = New System.Windows.Forms.TextBox
            Me.Label6 = New System.Windows.Forms.Label
            Me.updAmount3 = New System.Windows.Forms.NumericUpDown
            Me.Label7 = New System.Windows.Forms.Label
            Me.btnClose = New System.Windows.Forms.Button
            Me.lblMessage = New System.Windows.Forms.Label
            CType(Me.updAmount1, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.updAmount2, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.updAmount3, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            '
            'Label1
            '
            Me.Label1.Location = New System.Drawing.Point(16, 18)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(96, 16)
            Me.Label1.TabIndex = 0
            Me.Label1.Text = "Amount Pledged:"
            '
            'txtAmountPledged
            '
            Me.txtAmountPledged.Location = New System.Drawing.Point(128, 16)
            Me.txtAmountPledged.Name = "txtAmountPledged"
            Me.txtAmountPledged.Size = New System.Drawing.Size(144, 20)
            Me.txtAmountPledged.TabIndex = 1
            Me.txtAmountPledged.Text = "0.00"
            Me.txtAmountPledged.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
            '
            'Label2
            '
            Me.Label2.Location = New System.Drawing.Point(16, 58)
            Me.Label2.Name = "Label2"
            Me.Label2.Size = New System.Drawing.Size(112, 20)
            Me.Label2.TabIndex = 2
            Me.Label2.Text = "Rotherham College:"
            '
            'updAmount1
            '
            Me.updAmount1.Location = New System.Drawing.Point(128, 56)
            Me.updAmount1.Name = "updAmount1"
            Me.updAmount1.Size = New System.Drawing.Size(48, 20)
            Me.updAmount1.TabIndex = 3
            Me.updAmount1.Value = New Decimal(New Integer() {50, 0, 0, 0})
            '
            'Label3
            '
            Me.Label3.Location = New System.Drawing.Point(176, 59)
            Me.Label3.Name = "Label3"
            Me.Label3.Size = New System.Drawing.Size(16, 16)
            Me.Label3.TabIndex = 4
            Me.Label3.Text = "%"
            '
            'txtAmount1
            '
            Me.txtAmount1.Location = New System.Drawing.Point(200, 56)
            Me.txtAmount1.Name = "txtAmount1"
            Me.txtAmount1.Size = New System.Drawing.Size(72, 20)
            Me.txtAmount1.TabIndex = 5
            Me.txtAmount1.Text = "0.00"
            Me.txtAmount1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
            '
            'txtAmount2
            '
            Me.txtAmount2.Location = New System.Drawing.Point(200, 88)
            Me.txtAmount2.Name = "txtAmount2"
            Me.txtAmount2.Size = New System.Drawing.Size(72, 20)
            Me.txtAmount2.TabIndex = 9
            Me.txtAmount2.Text = "0.00"
            Me.txtAmount2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
            '
            'Label4
            '
            Me.Label4.Location = New System.Drawing.Point(176, 88)
            Me.Label4.Name = "Label4"
            Me.Label4.Size = New System.Drawing.Size(16, 16)
            Me.Label4.TabIndex = 8
            Me.Label4.Text = "%"
            '
            'updAmount2
            '
            Me.updAmount2.Location = New System.Drawing.Point(128, 88)
            Me.updAmount2.Name = "updAmount2"
            Me.updAmount2.Size = New System.Drawing.Size(48, 20)
            Me.updAmount2.TabIndex = 7
            Me.updAmount2.Value = New Decimal(New Integer() {25, 0, 0, 0})
            '
            'Label5
            '
            Me.Label5.Location = New System.Drawing.Point(16, 90)
            Me.Label5.Name = "Label5"
            Me.Label5.Size = New System.Drawing.Size(112, 20)
            Me.Label5.TabIndex = 6
            Me.Label5.Text = "Leicester University:"
            '
            'txtAmount3
            '
            Me.txtAmount3.Location = New System.Drawing.Point(200, 120)
            Me.txtAmount3.Name = "txtAmount3"
            Me.txtAmount3.Size = New System.Drawing.Size(72, 20)
            Me.txtAmount3.TabIndex = 13
            Me.txtAmount3.Text = "0.00"
            Me.txtAmount3.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
            '
            'Label6
            '
            Me.Label6.Location = New System.Drawing.Point(178, 124)
            Me.Label6.Name = "Label6"
            Me.Label6.Size = New System.Drawing.Size(16, 16)
            Me.Label6.TabIndex = 12
            Me.Label6.Text = "%"
            '
            'updAmount3
            '
            Me.updAmount3.Location = New System.Drawing.Point(130, 121)
            Me.updAmount3.Name = "updAmount3"
            Me.updAmount3.Size = New System.Drawing.Size(48, 20)
            Me.updAmount3.TabIndex = 11
            Me.updAmount3.Value = New Decimal(New Integer() {25, 0, 0, 0})
            '
            'Label7
            '
            Me.Label7.Location = New System.Drawing.Point(18, 125)
            Me.Label7.Name = "Label7"
            Me.Label7.Size = New System.Drawing.Size(112, 20)
            Me.Label7.TabIndex = 10
            Me.Label7.Text = "Lars Union Academy:"
            '
            'btnClose
            '
            Me.btnClose.Location = New System.Drawing.Point(200, 160)
            Me.btnClose.Name = "btnClose"
            Me.btnClose.TabIndex = 14
            Me.btnClose.Text = "Close"
            '
            'lblMessage
            '
            Me.lblMessage.Location = New System.Drawing.Point(16, 160)
            Me.lblMessage.Name = "lblMessage"
            Me.lblMessage.Size = New System.Drawing.Size(168, 23)
            Me.lblMessage.TabIndex = 15
            Me.lblMessage.Text = "Message"
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(292, 198)
            Me.Controls.Add(Me.lblMessage)
            Me.Controls.Add(Me.btnClose)
            Me.Controls.Add(Me.txtAmount3)
            Me.Controls.Add(Me.Label6)
            Me.Controls.Add(Me.updAmount3)
            Me.Controls.Add(Me.Label7)
            Me.Controls.Add(Me.txtAmount2)
            Me.Controls.Add(Me.Label4)
            Me.Controls.Add(Me.updAmount2)
            Me.Controls.Add(Me.Label5)
            Me.Controls.Add(Me.txtAmount1)
            Me.Controls.Add(Me.Label3)
            Me.Controls.Add(Me.updAmount1)
            Me.Controls.Add(Me.Label2)
            Me.Controls.Add(Me.txtAmountPledged)
            Me.Controls.Add(Me.Label1)
            Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
            Me.MaximizeBox = False
            Me.Name = "Form1"
            Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
            Me.Text = "Pledge Distribution"
            CType(Me.updAmount1, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.updAmount2, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.updAmount3, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)
    
        End Sub
    
    #End Region
    
        Private Sub updAmount1_ValueChanged(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
    
            AmountPledged = CDbl(Me.txtAmountPledged.Text)
    
            RateAmount2 = Me.updAmount2.Value
            RateAmount3 = Me.updAmount3.Value
            Me.updAmount1.Maximum = 100 - RateAmount2 - RateAmount3
            RateAmount1 = Me.updAmount1.Value
    
            Amount1 = AmountPledged * RateAmount1 / 100
            Amount2 = AmountPledged * RateAmount2 / 100
            Amount3 = AmountPledged * RateAmount3 / 100
            Rest = AmountPledged - Amount1 - Amount2 - Amount3
    
            Me.txtAmount1.Text = Amount1.ToString("C")
            Me.txtAmount2.Text = Amount2.ToString("C")
            Me.txtAmount3.Text = Amount3.ToString("C")
    
            If Rest > 0 Then
                Me.lblMessage.Text = Rest.ToString("C") & " still to be used"
            Else
                Me.lblMessage.Text = ""
            End If
        End Sub
    
        Private Sub updAmount2_ValueChanged(ByVal sender As System.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
    
            AmountPledged = CDbl(Me.txtAmountPledged.Text)
    
            RateAmount1 = Me.updAmount1.Value
            RateAmount3 = Me.updAmount3.Value
            Me.updAmount2.Maximum = 100 - RateAmount1 - RateAmount3
            RateAmount2 = Me.updAmount2.Value
    
            Amount1 = AmountPledged * RateAmount1 / 100
            Amount2 = AmountPledged * RateAmount2 / 100
            Amount3 = AmountPledged * RateAmount3 / 100
            Rest = AmountPledged - Amount1 - Amount2 - Amount3
    
            Me.txtAmount1.Text = Amount1.ToString("C")
            Me.txtAmount2.Text = Amount2.ToString("C")
            Me.txtAmount3.Text = Amount3.ToString("C")
    
            If Rest > 0 Then
                Me.lblMessage.Text = CStr(Rest) & " still to be used"
            Else
                Me.lblMessage.Text = ""
            End If
        End Sub
    
        Private Sub updAmount3_ValueChanged(ByVal sender As System.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
    
            AmountPledged = CDbl(Me.txtAmountPledged.Text)
    
            RateAmount1 = Me.updAmount1.Value
            RateAmount2 = Me.updAmount2.Value
            Me.updAmount3.Maximum = 100 - RateAmount1 - RateAmount2
            RateAmount3 = Me.updAmount3.Value
    
            Amount1 = AmountPledged * RateAmount1 / 100
            Amount2 = AmountPledged * RateAmount2 / 100
            Amount3 = AmountPledged * RateAmount3 / 100
            Rest = AmountPledged - Amount1 - Amount2 - Amount3
    
            Me.txtAmount1.Text = Amount1.ToString("C")
            Me.txtAmount2.Text = Amount2.ToString("C")
            Me.txtAmount3.Text = Amount3.ToString("C")
    
            If Rest > 0 Then
                Me.lblMessage.Text = CStr(Rest) & " still to be used"
            Else
                Me.lblMessage.Text = ""
            End If
        End Sub
    
        Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
            End
        End Sub
    
        Private Sub txtAmountPledged_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmountPledged.TextChanged
            If IsNumeric(Me.txtAmountPledged.Text) Then
                Me.updAmount1.Enabled = True
                Me.updAmount2.Enabled = True
                Me.updAmount3.Enabled = True
            Else
                Me.updAmount1.Enabled = False
                Me.updAmount2.Enabled = False
                Me.updAmount3.Enabled = False
            End If
        End Sub
    End Class
  5. Test the application
 

Home Copyright 2004 FunctionX, Inc.