• ### Question

• I have a homework assignment to create a calculator for a donut/coffee shop. what im supposed to do is to be able to:

click a checkbox(there are 7 to choose from), press a button that brings up an inputbox and i enter a number in there for how many of the checkbox items i
want. press ok, and it calculates the cost and displays it in a text
(say the first 2 check boxes are coffee and decaf. i check both, press a button and an input box comes up for coffee. i put 3 in the box and hit ok and it
displays the cost for the 3 coffees in the text box. right away the inputbox for the decaf comes up and i enter in three and hit enter and it displays the cost for 3 decafs. Then i am supposed to be able to press a 2nd button and it totals the
amount in the text box and displays it into a label.

what i need to do in the end is calculate the total for the text box and display it in a label. i have the checkbox/inputbox thing figured out. i just cant
figure out how to display those total costs in a label. here is my code so far. thanks much.

Private Sub btnTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTotal.Click

Call chkGlazed1()
Call chksugar1()
Call chkchocolate1()
Call chkfilled1()
Call chkregular1()
Call chkdecaf1()
Call chkcappucino1()
end sub

Private Function chkGlazed1() As Integer
Dim output As Double
If chkGlazed.Checked = True Then
output = InputBox(“Enter how many Glazed”, “”, “”) * 0.65
txtOrder.Text &= output.ToString(“C2″) & ” Glazed” & ControlChars.NewLine
End If
Return output
End Function

Private Function chksugar1() As Integer
Dim output As Double
If chkSugar.Checked = True Then
output = InputBox(“Enter how many Sugar”, “”, “”) * 0.65
txtOrder.Text &= output.ToString(“C2″) & ” Sugar” & ControlChars.NewLine
End If
Return output
End Function

Private Function chkchocolate1() As Integer
Dim output As Double
If chkChocolate.Checked = True Then
output = InputBox(“Enter how many Chocolate”, “”, “”) * 0.85
txtOrder.Text &= output.ToString(“C2″) & ” Chocolate” & ControlChars.NewLine
End If
Return output
End Function

Private Function chkfilled1() As Integer
Dim output As Double
If chkFilled.Checked = True Then
output = InputBox(“Enter how many Filled”, “”, “”) * 1
txtOrder.Text &= output.ToString(“C2″) & ” Filled” & ControlChars.NewLine
End If
Return output
End Function

Private Function chkregular1() As Integer
Dim output As Double
If chkRegular.Checked = True Then
output = InputBox(“Enter how many Regular”, “”, “”) * 1.8
txtOrder.Text &= output.ToString(“C2″) & ” Regular” & ControlChars.NewLine
End If
Return output
End Function

Private Function chkdecaf1() As Integer
Dim output As Double
If chkDecaf.Checked = True Then
output = InputBox(“Enter how many Decaf”, “”, “”) * 1.8
txtOrder.Text &= output.ToString(“C2″) & ” Decaf” & ControlChars.NewLine
End If
Return output
End Function

Private Function chkcappucino1() As Integer
Dim output As Double
If chkCap.Checked = True Then
output = InputBox(“Enter how many Cappucinos”, “”, “”) * 2.5
txtOrder.Text &= output.ToString(“C2″) & ” Cappucino” & ControlChars.NewLine
End If
Return output
End Function

• i can get the numbers from the called functions. how would i do that though? i am completely lost at this stage.

Here is a function:

Private Function chksugar1() As Integer
Dim output As Double
If chkSugar.Checked = True Then
output = InputBox(“Enter how many Sugar”, “”, “”) * 0.65
txtOrder.Text &= output.ToString(“C2″) & ” Sugar” & ControlChars.NewLine
End If
Return output
End Function

The first problem is that you are creating and returning a variable that is not the same type as the function. Your function is type integer, but your return value is type double. Change the function type to double.

The second problem is that you can’t multiply a string by a number. You need to make the string a number first. The correct way to do that is to use Double.TryParse, and advise the user if the input could not be converted (typically,
they left the input blank) and ask them to enter a valid value. See:
http://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx

For testing purposes only you could use CDBL
output = CDbl(InputBox(“Enter how many Sugar”, “”, “”)) * 0.65

This is the function call:
Call chkGlazed1()

You should not use Call. You should use the function as an expression, because it represents a value – the value it represents is the returned result
of the function.
Dim GlazedAmount as Double = ChkGlazed1()

Then GlazedAmount can be used with the other costs you collect in a similar manner to calculate the total cost of the order by adding those costs together. Your textboxes are used to show what the user entered and the results of your
calcultions – they are not part of the calculation.

• Ryan,

Youâre asking a homework question, so I wonât answer it directly, but I am going to try to help you by showing you another project
which is similar. Itâs not exact, and for some of what Iâll show, it doesnât apply at all, but I hope that at the end of all this, it will give you some ideas about how you might can use the concepts presented.

This project â oh, I meant to tell you â congratulations, you now own a pizza shop!

For the most part this is fairly straightforward, but do notice that there are many options and some of them are exclusive (shown
using RadioButtons) while others arenât exclusive; in fact with what you see there that has checkboxes, they can have as many as they choose to. So we need to be able to accurately test for all of these things.

Also though, notice that the user will enter the quantity by typing it into a TextBox. What if they donât type in a number?
What if they do type in a number but itâs not a whole number (sorry, we donât sell 0.35 of a pizza!), or what if they enter in a negative number?

All of these things have to be dealt with and the code that Iâll show momentarily will do that.

In what Iâll show in the code, youâll see some things which you may not be familiar with. At the top of this forum is a header
which includes âDocumentationâ. You can use that to look up anything that youâre not familiar with.

In the code, a few things that you may not be familiar with are:

• Enumerators
• The
.Validating Event Of A Control
• An
Error Provider
• A
Stringbuilder

The fact of the matter is that if this were a real project of mine, Iâd most likely do things differently, but no matter that,
what follows is still valid. Have a look at the code, then Iâll discuss more about it following that:

If youâd like to see the code better formatted, have a look at it on a
page
of my website here.

Watching it in operation:

• The first thing is that weâll establish a âbase priceâ for any pizza. The options shown will be added to the base price to arrive
at a subtotal price. Weâll next add sales tax which for our purpose will be a straight rate to be multiplied by the subtotal, and finally then, we can calculate the total selling price.

Both of those are shown at the top of the code, class-scoped so that everything in the code can âseeâ them.

The next thing that youâll see are a number of enums (enumerators). You certainly donât have to use them, now or ever, but in
time youâll see that it makes coding easier on you â you donât have to rely on memory about certain constants, and with this many options, theyâre quite handy.

Youâll see some subs, broken out by what they do, and which have multiple event handlers. Using multiple event handlers in one
single sub makes things easier on us. Have a look at one in particular:

Private
Sub
MeatCheckBoxes_CheckedChanged(ByVal
sender As
System.Object, _

ByVal
e As
System.EventArgs) _

Handles
cb_Meats_Pepperoni.CheckedChanged, _

cb_Meats_Ham.CheckedChanged,
_

cb_Meats_Pork.CheckedChanged,
_

cb_Meats_Beef.CheckedChanged,
_

cb_Meats_ItalianSausage.CheckedChanged

tb_Total.Clear()

If
(cb_Meats_Beef.Checked Or
_

cb_Meats_Ham.Checked
Or
_

cb_Meats_ItalianSausage.Checked
Or
_

cb_Meats_Pepperoni.Checked
Or
_

cb_Meats_Pork.Checked)
AndAlso
_

tb_Qty.Text.Trim <>
“”
Then

btn_ViewTotal.Enabled =
True

Else

btn_ViewTotal.Enabled =
False

End
If

End
Sub

The fact that our ârules of the roadâ are that they must include at least one meat, this is checked in this sub. Since
theyâre CheckBoxes and not RadioButtons, you canât assume that at least one is always checked.

In several of the other subs, youâll notice that I direct the code back to this sub in order to clear the text of the total
(which may or may not have text in it) and to test whether or not minimum information has been included to allow them to press the button to calculate the total.

You canât always do this; when I call this sub from other subs, notice how I do it:

MeatCheckBoxes_CheckedChanged(Nothing,
Nothing)

The âsenderâ and âEventArgsâ are set to be null (nothing), so if the sub
uses
those â which this one does not â then obviously setting them to nothing will cause an exception.

When the user types text into the TextBox used for the quantity, my philosophy is to let them type anything they want to, but
once the TextBox loses focus, I do like I did here which is to use the .Validating event.

The .Validating event has an event argument thatâs special â telling it to âcancelâ â which I used here so let me elucidate
on what that does and also explain a caveat about it.

When you set e.Cancel = True, youâre telling the method to NOT allow the user to leave the control (until theyâve entered a
valid value, based on whatever youâve told it to consider valid). Thatâs good âŠ and potentially bad. If e.Cancel has been set to true, they canât even close the form! Nope â not until they enter something valid. Well what if they just donât know the answer
or decide they want to close the program from who knows what reason?

Hereâs how Iâve come to handle that:

Look at the .TextChanged event for that TextBox (tb_Qty). It does two things: First it clears the ErrorProvider (that yellow
box you saw shown in the previous screenshots when they entered invalid information) and it calls the sub which checks to see, among other things, that the text in that TextBox isnât empty. If it *is* empty, is disables the button to calculate the total.

So what happens then is that if they have typed something in which isnât valid and they want to exit the program, theyâre instructed
to clear the text out. Notice in the code of the .Validating event, the first thing thatâs looked for is if thereâs text in the TextBox and if not, it just exits that sub all together, so no harm done.

Either way though, they either enter valid information or they canât click the button â so it works either way.

The rest of the code consists of a variety of functions which is used in the buttonâs .Click event; those should be straightforward
and not need explanation, but do take note of how Iâm handling the options â if itâs a âone or the otherâ (a Radiobutton), then I use âElseIfâ, but when they can select more than one, each one of those is tested individually.

I hope that you can make use of some of this in your own program. Look up what you donât know, and most of all: Think it through.
Youâll get it. đ