English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am making an order form and I have a section of options that are checkboxes. I want to add the options (if they are selected) together and add them to the total. How can I do that?

2007-04-20 04:09:35 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

I'm still confused. This is what I have right now and it's only counting the first one checked:

Private Function ExtrasPrice() As Double
Dim price As Double = 0
If CheckBox1.Checked Then
price = THICK_ENVELOPES
End If
If CheckBox2.Checked Then
price = ENVELOPE_SEALS
End If
If CheckBox3.Checked Then
price = FOIL_ENVELOPES
End If
If CheckBox4.Checked Then
price = STAMPS_PRICE
End If
If CheckBox5.Checked Then
price = LABELS_PRICE
End If
Return price
End Function

2007-04-20 04:26:15 · update #1

4 answers

One problem is that you are re-setting 'price' with each if statement instead of adding.

If CheckBox1.Checked Then
price = THICK_ENVELOPES 'price is now the value for thick_envelopes
End If
If CheckBox2.Checked Then
price = ENVELOPE_SEALS 'price is now the value for envelope_seals
End If
If CheckBox3.Checked Then
price = FOIL_ENVELOPES 'price is now the value for foil_envelopes
End If

So you need to use either 'price = price + variable or
price += variable

If CheckBox1.Checked Then
price = price + THICK_ENVELOPES 'price is now the value for thick_envelopes
End If
If CheckBox2.Checked Then
price = price + ENVELOPE_SEALS 'price is now the value for thick_envelopes PLUS envelope_seals
End If
If CheckBox3.Checked Then
price += FOIL_ENVELOPES 'price is now the value for foil_envelopes plus the previous total from thick_envelopes and envelope_seals
End If

hope that is clear

2007-04-20 05:30:20 · answer #1 · answered by rod 6 · 0 0

Here is some code which will work for anynumber of check boxes you add into a groupbox.

The groupbox must only contain checkboxes and the value you wish to assign for each checkbox mut be the first characters in the text property. (OR you can place the value in the tag property of each check box and change the code to read itemval= val(chk.tag)

Because the checked property uses a value of zero to represent an unckecked box you can multiply the value times the checked property to set the itemval = to zero.
When checked the checked property has a value of -1 which is why the math.abs function is used

Dim chk As New CheckBox
Dim tot As Double
Dim itemVal As Double

tot = 0

For Each chk In Me.GroupBox1.Controls
'object is a check box
'chk = obj 'copy the object into a checkbox variable
itemVal = Val(chk.Text) 'extract the value from the checkbox text
itemVal = itemVal * Math.Abs(CInt(chk.Checked)) 'Boolean false = 0
'Boolean is either a zero or one which will result in
'in either itemVale * 1 OR itemVal * 0
tot = tot + itemVal 'totalize the values
Next
Label1.Text = tot

2007-04-20 12:24:49 · answer #2 · answered by MarkG 7 · 0 0

Scratch what I said before...

All you need to do to your calculating code is...

price + = THICK_ENVELOPS

That's short term for "price = price + THICK_ENVELOPS"

Note: Make sure you do this for all your variable calculating lines of code.

2007-04-20 11:25:01 · answer #3 · answered by Yep-itsMe 3 · 0 0

if statements
In java:
int total = 0;
if(checkbox1.checked())
total += checkbox1.value();
....

2007-04-20 11:17:42 · answer #4 · answered by Anonymous · 0 1

fedest.com, questions and answers