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?
How can I add things in Visual Basic?
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
Reply:if statements
In java:
int total = 0;
if(checkbox1.checked())
total += checkbox1.value();
....
Reply: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.
Reply: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
botanical garden
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment