Question:
Visual Basic - Check Boxes/Input box?
Rachel C
2010-02-20 23:24:58 UTC
Alright. For a programming class, I'm working on a Shipping Order Form. I have always been so confused with check boxes and making more than one thing show up in a text box if more than one check box is selected. So here is my problem.

I have 3 check boxes. One is an option for Overnight Shipping ($5), one for Saturday Shipping ($12) and one for Full Insurance (10%). If the full insurance is selected, there needs to be an input box that pops up that says "How much insurance do you want?". [ If the number is less than 1 or more than 100,000 dollars, a MsgBox appears asking to fix ] If all three are selected, then my shipping total text box needs to read Overnight + Saturday + Insurance. The insurance being 10% of the number the user would input.

My problem is adding all of those together into one textbox.. But also the input box. I defined it as a variable and have an If line, but when I click my Compute button, the input box pops up even if the checkbox is unchecked.

I hope that all makes sense.
Three answers:
Mike L
2010-02-20 23:58:37 UTC
Personally, I don't like using the InputBox at all... Not sure how much flexibility you have in the design of the form, since it's a programming class assignment, but given full freedom of design, I'd use a textbox that is initially not Enabled for the input under Full Insurance.



[ ] Overnight

[ ] Saturday

[ ] Full Insurance (10%)

How Much insurance?: [ ]





When the user checks the Full Insurance checkbox, set the text box's Enabled property to True and set the focus to that text box.



txtInsurance.Enabled = chkInsurance.Checked



When the user clicks the Compute button, then you might have a sequence such as:



' check the insurance amount

Dim dblInsurance As Double

CONST MIN_INSURANCE = 1

CONST MAX_INSURANCE = 100000



dblInsurance = CDbl(txtInsurance)



If dblInsurance < MIN_INSURANCE Or dblInsurance > MAX_INSURANCE Then

MsgBox "Amount for insurance must be between " & MIN_INSURANCE & " and " & MAX_INSURANCE &".", vbExclamation, "Invalid Insurance Amount"

Exit Sub

End If



Dim dblTotal As Double



Const OVERNIGHT_RATE = 5

Const SATURDAY_RATE = 12



If chkOvernight.Checked Then

dblTotal = OVERNIGHT_RATE

End If



If chkSaturday.Checked Then

dblTotal = dblTotal + SATURDAY_RATE

End If



If chkInsurance.Checked Then

dblTotal = dblTotal + (dblInsurance * 0.1 )

End If



txtTotal = Format(dblTotal, "0.00")



Note this code makes assumptions as to the control names on your form. It also assumes you are programming in VBA or VB 4, 5 or 6 (assumption made because you mentioned MsgBox rather than MessageBox ), but it should be fairly easy to adapt to later versions that target the .NET framework. It is AS-IS, and has been written off the top of my head without testing or error handlers, to demonstrate the concepts for you. Adapt as necessary to your particular project.



Hope this helps you.



Mike
2016-04-12 13:21:38 UTC
Radio buttons dragged onto a group box will do the trick without any extra code and all you then need to worry about is the event handle of each radio button. With check boxes you could easily put them in a list or array and let a while or for loop uncheck them except one if you so wished. You could make a method out of this and call it in each check boxes event handler.
J.J.'s Advice / Avis de J.J.
2010-02-20 23:30:40 UTC
Well, the problem is with your if statement then. You could either set a separate event for when the checkbox gets checked, or seet an if statement like

if CheckBox.Checked

' do something

endif

or whatever it is exactly you want to do.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...