CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    9

    Checkbox calculation problem.

    I have this problem i can't seem to figure out,i'll do my best to explain it here.

    I have a series of checkboxes each one having a name and a value and a person can choose either one or all of the boxes(it can't be radiobuttons because you can only make a single choice)
    I want to know if it is possible to calculate the total amount if the person chooses any number of checkboxes from one to eight.
    I.e checkbox1 value = 5
    checkbox2 value = 15
    checkbox3 value = 7.
    etc

    I would apprieciate any kind of help here thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Checkbox calculation problem.

    Probably, but a more exact definition is needed. Plus, show some code of how you THINK it should work...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Checkbox calculation problem.

    If you mean could you tell which and/or how many are selected then of course you can.

    No idea what you are trying to do though you little example of checkbox values is just confusing as those would not be valid values for a checkbox.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    9

    Re: Checkbox calculation problem.

    Quote Originally Posted by DataMiser View Post
    If you mean could you tell which and/or how many are selected then of course you can.

    No idea what you are trying to do though you little example of checkbox values is just confusing as those would not be valid values for a checkbox.

    Its part of a project i am trying to build.After a client chooses what car they want and how many days they want to rent it for ,you would then calculate the total amount owed for the rental.i have that part worked out,the second part is the one that i can't figure.

    after the vehicle selection has been made the client chooses from five differant extras,these are,

    Childseat @ �10 fixed rate
    sat-nav@ �5 fixed rate
    Luggage rack @ �15 fixed rate
    Ski-Equiped @ �10 fixed rate

    Choice on the form is made via a checkbox for each item,the client can choose either one or all of the extras,my problem is how to workout how to calculate the total owing for the extras based on how many checkboxes have been choosen

    Code:
    Public class Rentalform
    
    'declare module level constants
    
    Const CHILDSEAT_EXTRA as Decimal = 10
    Const SATNAV_EXTRA as decimal = 5
    const LUGGAGE_RACK_EXTRA as decimal = 15
    Const SKI_EQUIPED_EXTRA as decimal = 10
    
    
    if chkChildseat.checked = true and
    chkSatnav.checked = false and
    chkLuggageRack.checked = false and 
    chkSkiEquiped.checked = false then
    
    txtTotal.text = CHILDSEAT_EXTRA 
    
    If chkchildseat.checked = true and 
    chkSatNav.checked = false and 
    chkLuggageRack.checked = True and 
    chkSkiEquiped.checked = false then
    
    txtTotal.text = (CHILDSEAT_EXTRA + LUGGAGE_RACK_EXTRA )
    Do i have to churn out streams of code to cover every possible choice variation i.e chilseat only,childseat and Luggagerack or maybe the client chooses all of the extras. Help please

  5. #5
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    9

    Re: Checkbox calculation problem.

    Its part of a project i am trying to build.After a client chooses what car they want and how many days they want to rent it for ,you would then calculate the total amount owed for the rental.i have that part worked out,the second part is the one that i can't figure.

    after the vehicle selection has been made the client chooses from five differant extras,these are,

    Childseat @ 10 fixed rate
    sat-nav@ 5 fixed rate
    Luggage rack @ 15 fixed rate
    Ski-Equiped @ 10 fixed rate

    Amounts above are currency (pounds,Euros,Dollars)

    Choice on the form is made via a checkbox for each item,the client can choose either one or all of the extras,my problem is how to workout how to calculate the total owing for the extras based on how many checkboxes have been choosen



    Code:
    Public class Rentalform
    
    'declare module level constants
    
    Const CHILDSEAT_EXTRA as Decimal = 10
    Const SATNAV_EXTRA as decimal = 5
    const LUGGAGE_RACK_EXTRA as decimal = 15
    Const SKI_EQUIPED_EXTRA as decimal = 10
    
    
    if chkChildseat.checked = true and
    chkSatnav.checked = false and
    chkLuggageRack.checked = false and 
    chkSkiEquiped.checked = false then
    
    txtTotal.text = CHILDSEAT_EXTRA 
    
    If chkchildseat.checked = true and 
    chkSatNav.checked = false and 
    chkLuggageRack.checked = True and 
    chkSkiEquiped.checked = false then
    
    txtTotal.text = (CHILDSEAT_EXTRA + LUGGAGE_RACK_EXTRA )
    Do i have to churn out streams of code to cover every possible choice variation i.e chilseat only,childseat and Luggagerack or maybe the client chooses all of the extras. Help please

  6. #6
    Join Date
    Dec 2012
    Posts
    2

    Re: Checkbox calculation problem.

    You could do something like this.

    ExtrasCost = 0
    If chkChildSeat.Checked = True Then ExtrasCost = ExtrasCost + 10
    If chkSatVan.Checked = True Then ExtrasCost = ExtrasCost + 5
    if chkLuggageRack.Checked = True Then ExtrasCost = ExtrasCost + 15
    if SkiEquipped.Checked = True Then ExtrasCost = ExtrasCost + 10

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Checkbox calculation problem.

    Just keep a RUNNING TOTAL between the calculations. Create a CALCULATE function that calls each IF/THEN as a separate FUNCTION that returns either a value or ZERO.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured