-
Jul 11th, 2019, 07:28 PM
#1
Thread Starter
Registered User
Code Logic problem using group boxes, text boxes and check boxes
Hi all,
I have made a VB form for a pizza calculator
- Users are to populate the size of pizza they want (Check Box 1, 2 &3) they can only click one
- They can choose extra toppings they like (Combo Box 4, 5, 6 & 7- Multiple allowed)
- Sides they want (Textbox 2, 3, 4) - they can enter a value
- And a button to calculate the cost, the code does not always include all costs
Code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim TotalCost As Double
TotalCost = 0
If CheckBox1.Checked Then TotalCost = TotalCost + 7
If CheckBox2.Checked Then TotalCost = TotalCost + 9
If CheckBox3.Checked Then TotalCost = TotalCost + 10
If CheckBox4.Checked Then TotalCost = TotalCost + 2
If CheckBox5.Checked Then TotalCost = TotalCost + 2
If CheckBox6.Checked Then TotalCost = TotalCost + 2
If CheckBox7.Checked Then TotalCost = TotalCost + 2
TotalCost = TotalCost + 2 * TextBox2.Text + 3 * TextBox3.Text + 5 * TextBox4.Text
TextBox1.Text = TotalCost
End Sub
End Class
-
Jul 11th, 2019, 07:53 PM
#2
Re: Code Logic problem using group boxes, text boxes and check boxes
If you want to allow the user only one selection, use Radiobuttons, not checkboxes.
My usual boring signature: Nothing
-
Jul 11th, 2019, 08:07 PM
#3
Re: Code Logic problem using group boxes, text boxes and check boxes
This is a fantastic time to get in the habit of giving controls meaningful names. If this is part of a school assignment, your instructor would have to do some additional checking to match up your logic with your control names to make sure the prices for sides are correct, prices for the pizza sizes are correct, etc.
If I were writing this for a school assignment, and once I converted the pizza sizes to radio buttons as suggested above, I might use names like:
radSmall, radMedium, ... for the size radio buttons
chkSalami, chkEgg, ... for the toppings check boxes
txtGarlicBread, txtCanOfDrink, ... for the sides text boxes.
Its up to you to come up with what works for you, but leaving control names as the default assigned names is a really bad habit to break as soon as possible.
-
Jul 11th, 2019, 08:36 PM
#4
Re: Code Logic problem using group boxes, text boxes and check boxes
Also, you shouldn't do math with TextBoxes. TextBox1.Text is a String, you should convert that String to a numeric datatype before you do the math,
Code:
Dim breadValue As Double
If Me.BreadTextBox.Text.Trim = String.Empty Then
breadValue = 0
Else
If Not Double.TryParse(Me.BreadTextBox.Text, breadValue) Then
MessageBox.Show("Invalid Bread Entry")
End If
End If
Do this for all TextBox values then do the math
TotalCost = TotalCost + (2 * breadValue) + (3 * drinkValue) + (5 * wingsValue)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|