Results 1 to 7 of 7

Thread: Adding Checkbox items and text box

  1. #1

    Thread Starter
    Junior Member VBAppleCoder's Avatar
    Join Date
    Oct 2017
    Posts
    26

    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!

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  3. #3

    Thread Starter
    Junior Member VBAppleCoder's Avatar
    Join Date
    Oct 2017
    Posts
    26

    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.

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    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.

  5. #5

    Thread Starter
    Junior Member VBAppleCoder's Avatar
    Join Date
    Oct 2017
    Posts
    26

    Re: Adding Checkbox items and text box

    Name:  Untitled.jpg
Views: 540
Size:  19.8 KB

    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.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    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?
    Quote Originally Posted by VBAppleCoder View Post
    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
  •  



Click Here to Expand Forum to Full Width