Home

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.

Practical LearningPractical Learning: Managing a Pledge

  1. Start Microsoft Visual Basic and double-click Standard EXE
  2. Save the project in a new folder named Pledge1
  3. Save Form1 and Project1 in it
  4. Right-click the Toolbox and click Components
  5. In the Controls property page of the Components dialog box, click the check mark of the Microsoft Windows Common Controls-2 6.0 (SP4)
     
  6. Click OK
  7. Design the form as follows:
     
    Control Text Name Additional Properties
    Label Amount Pledged:    
    TextBox 0.00 txtAmountPledged Alignment: 1 - Right Justify
    Label Rotherham College:    
    TextBox 50 txtRate1 Alignment: 1 - Right Justify
    UpDown   updAmount1 BuddyControl: txtRate1
    SyncBuddy: True
    Label %    
    TextBox 0.00 txtAmount1 Alignment: 1 - Right Justify
    Label Leicester University:    
    TextBox 25 txtRate2  
    UpDown   updAmount2 BuddyControl: txtRate2
    SyncBuddy: True
    Label %    
    TextBox 0.00 txtAmount2 Alignment: 1 - Right Justify
    Label Lars Union Academy:    
    TextBox 25 txtRate3  
    UpDown   updAmount3 BuddyControl: txtRate3
    SyncBuddy: True
    Label %    
    TextBox 0.00 txtAmount3 Alignment: 1 - Right Justify
    Label Message lblMessage  
    Button Close cmdClose  
  8. Double-click each up down control, the Amount Pledged text box, and the Close button
  9. Implement the events as follows:
     
    Option Explicit
    
    Private Sub cmdClose_Click()
        End
    End Sub
    
    Private Sub txtAmountPledged_Change()
        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
    
    Private Sub updAmount1_Change()
        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 = CDbl(Me.txtRate2.Text)
        RateAmount3 = CDbl(Me.txtRate3.Text)
        Me.updAmount1.Max = 100 - RateAmount2 - RateAmount3
        RateAmount1 = CDbl(Me.txtRate1.Text)
            
        Amount1 = AmountPledged * RateAmount1 / 100
        Amount2 = AmountPledged * RateAmount2 / 100
        Amount3 = AmountPledged * RateAmount3 / 100
        Rest = AmountPledged - Amount1 - Amount2 - Amount3
    
        Me.txtAmount1.Text = Amount1
        Me.txtAmount2.Text = Amount2
        Me.txtAmount3.Text = Amount3
            
        If Rest > 0 Then
            Me.lblMessage.Caption = Rest & " still to be used"
        Else
            Me.lblMessage.Caption = ""
        End If
    End Sub
    
    Private Sub updAmount2_Change()
        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 = CDbl(Me.txtRate1.Text)
        RateAmount3 = CDbl(Me.txtRate3.Text)
        Me.updAmount2.Max = 100 - RateAmount1 - RateAmount3
        RateAmount2 = CDbl(Me.txtRate2.Text)
    
        Amount1 = AmountPledged * RateAmount1 / 100
        Amount2 = AmountPledged * RateAmount2 / 100
        Amount3 = AmountPledged * RateAmount3 / 100
        Rest = AmountPledged - Amount1 - Amount2 - Amount3
    
        Me.txtAmount1.Text = Amount1
        Me.txtAmount2.Text = Amount2
        Me.txtAmount3.Text = Amount3
    
        If Rest > 0 Then
            Me.lblMessage.Caption = CStr(Rest) & " still to be used"
        Else
            Me.lblMessage.Caption = ""
        End If
    End Sub
    
    Private Sub updAmount3_Change()
     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 = CDbl(Me.txtRate1.Text)
            RateAmount2 = CDbl(Me.txtRate2.Text)
            Me.updAmount3.Max = 100 - RateAmount1 - RateAmount2
            RateAmount3 = CDbl(Me.txtRate3.Text)
    
            Amount1 = AmountPledged * RateAmount1 / 100
            Amount2 = AmountPledged * RateAmount2 / 100
            Amount3 = AmountPledged * RateAmount3 / 100
            Rest = AmountPledged - Amount1 - Amount2 - Amount3
    
            Me.txtAmount1.Text = Amount1
            Me.txtAmount2.Text = Amount2
            Me.txtAmount3.Text = Amount3
    
            If Rest > 0 Then
                Me.lblMessage.Caption = CStr(Rest) & " still to be used"
            Else
                Me.lblMessage.Caption = ""
            End If
    End Sub
  10. Test the application
 

Home Copyright 2004 FunctionX, Inc.