-
Oct 12th, 2017, 02:56 PM
#1
Thread Starter
Junior Member
Adding Checkbox items and text box
Hello,
In my project I have a textbox where you can enter a number for "parts" and then several check boxes that have different prices. I am wanting to know how to add what I have in the textbox (a number) and the checkboxes that have been checked. In my program you click a calculate total button and it displays this information in a label. I am new to visual basic so my understanding is not very much. Thanks for any help!
-
Oct 12th, 2017, 03:24 PM
#2
Re: Adding Checkbox items and text box
It would be considerably better to use a NumericUpDown control rather than a textbox. If you use a textbox, you get back a string. That string has to be converted to a number before you can do any calculation. There are ways to do that, but from your description, the only safe one is Integer.TryParse. If you aren't using that, then what you are doing will crash under certain circumstances. With a NumericUpDown, the .Value is a Decimal. You may still want to convert that to an integer, but you could use CInt for that, and you may not even care about the conversion.
As for the rest, you talk about checkboxes for price. You can select more than one checkbox. Does that really make sense when it comes to price? It seems like you'd want to use a radiobutton, such that the user can only choose one of the prices.
Each control has a .Tag property. This is an Object which can hold anything you want. In this case, it may be useful for each checkbox/radiobutton to have the price in the .Tag property. You could then do something like this:
YourTotalLabel.Text = yourNumericUpDown.Value * CDec(yourRadioButton.Tag)
That would require you to know which radiobutton was selected, but there are ways to do that. Alternatively, you could have a variable at form scope (not in any method), and in the CheckChanged event for either the radiobutton or checkbox, you could take the value from the .Tag and put it into that variable. This would work better for either radiobuttons or checkboxes.
My usual boring signature: Nothing
-
Oct 12th, 2017, 03:28 PM
#3
Thread Starter
Junior Member
Re: Adding Checkbox items and text box
Actually I was over thinking. The number from the Text box just has to go to a label when I click calculate. Then it is added to a total label. So for instance if I say Parts were $30 then $30 would go to the label for parts. Then if I have other charges say $120 then in the total label it should read $150.
-
Oct 12th, 2017, 04:02 PM
#4
Re: Adding Checkbox items and text box
I'm having trouble visualizing this application, so I can't really understand the question. What are the checkboxes for? How do they relate to the text box?
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Oct 12th, 2017, 04:12 PM
#5
Thread Starter
Junior Member
Re: Adding Checkbox items and text box
This is the applications format. Two things I need. 1: When I enter a number in the Parts textbox I need that number to go to the Parts Label under "Summary of Charges" and the that number also needs to be added to the total fees when I click the "Calculate Total" button.
Then next thing I need is the same for the labor text box. But for the labor textbox, the charge for labor is $60 per hour. So im not sure how to code for that.
-
Oct 12th, 2017, 04:24 PM
#6
Re: Adding Checkbox items and text box
Labor is a number of hours.
Everything I said in my first post is still valid: Use NumericUpDown rather than textboxes and consider what you can do with the .Tag property to attach a number to a control.
My usual boring signature: Nothing
-
Oct 13th, 2017, 12:14 AM
#7
Re: Adding Checkbox items and text box
Why include all that empty space, tool bars, etc in your screen shot when you could have just included the actual form?
Originally Posted by VBAppleCoder
that number also needs to be added to the total fees when I click the "Calculate Total" button.
So declare a variable of the appropriate type (probably Decimal for money) and store the total fees value in that. All calculations are done on numbers and that is your number. Whenever you need to display that value in a Label or anywhere else, convert it to a String and then display that. Always remember that data should be stored and used in its native format and converted to text, i.e. a String, only for display or serialisation (e.g. saving to a text file) purposes.
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
|