Results 1 to 14 of 14

Thread: Noob needs help with Check Boxes!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2004
    Posts
    22

    Noob needs help with Check Boxes!

    What I need is EXTREMELY basic code to make these checkboxes add .25 cents for each one that is clicked. I tried this:

    If chkBox1.Checked = True Then
    txtTextBox1.Text = txtTextBox1.Text + 0.25
    End If

    If chkBox2.Checked = True Then
    txtTextbox1.Text = txtTextbox1.Text + 0.25
    End If

    etc... and it seems to add incorrectly, .25 at first, then .75, then it goes up by .75 intrevals. Also, it adds every single time a checkbox is clicked on or off, I need it to take off the .25 cents if it's unclicked. It has 4 checkboxes, please help!! Thanks

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Noob needs help with Check Boxes!

    well what event are you calling this code in??? I would think you need an "Else" statement to evaluate it if its not checked, then subtracting the 25 cents...

  3. #3
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Noob needs help with Check Boxes!

    What you are doing is adding the text value .25 to the string variable. You should, if anything, be converting the string to a number first, then to a string after the math is finished.

    Also, checkboxes are a weird way to do this, unless I suppose you have four options that each cost a quarter..

    Anyways, assuming textbox1 has the value in it to add to from the beginning, here's one method.

    VB Code:
    1. Dim Cost as Decimal = Convert.ToDecimal(textbox1.text)
    2. Cost = Cost + 0.25D 'Or Cost += 0.25D to save typing.
    3. textbox1.text = Cost.ToString

    You can also concatenate all of the above to one statement:

    VB Code:
    1. Textbox1.text = (Convert.ToDecimal(textbox1.text) + 0.25D).ToString

    That way your math is done independantly of the string, you have to be careful when working with different data types, and the proper (and safest way) is always going to be to explicitly (that means with a statement) convert them instead of implicitly (that means what it sounds, the compiler makes an assumption about what you're doing and tries to add it somehow) One way to ensure you don't get data type errors is to at the top of your form's code put

    VB Code:
    1. Option Strict On

    It can also be set as the default if you play with the options a little bit.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  4. #4
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Noob needs help with Check Boxes!

    Also, as gigemboy said, when you want to subtract the value, you need to put an else statement in after that. Remember you are handling the CheckChanged event, which means if it's changing, you're executing the code. (on or off)

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2004
    Posts
    22

    Re: Noob needs help with Check Boxes!

    Thanks for all of your replies, if I could get someone to please add me to msn messenger or xfire it would be greatly appreciated. I am still having a conflict, my msn passport is da.niche@!!NO-SPAM!!gmail.com and my Xfire username is DaNiche Thanks.

  6. #6
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Noob needs help with Check Boxes!

    Why not post your questions in here? Someone else may have the same problem later, and the answer is best put out in the open so it can be reused.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  7. #7
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Noob needs help with Check Boxes!

    At least tell us what event you are calling this code in.. that could be the main problem...

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Noob needs help with Check Boxes!

    You may want to change this a bit if you aren't actually display a currency value in the TextBox but you get the idea:
    VB Code:
    1. Private Sub CheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged, _
    2.                                                                                                             CheckBox2.CheckedChanged, _
    3.                                                                                                             CheckBox3.CheckedChanged, _
    4.                                                                                                             CheckBox4.CheckedChanged
    5.         Dim currentValue As Double
    6.  
    7.         'Make sure that the TextBox contains a valid value.
    8.         If Double.TryParse(Me.TextBox1.Text, Globalization.NumberStyles.Currency, Nothing, currentValue) Then
    9.             If DirectCast(sender, CheckBox).Checked Then
    10.                 'Add $0.25
    11.                 currentValue += 0.25R
    12.             Else
    13.                 'Subtract $0.25
    14.                 currentValue -= 0.25R
    15.             End If
    16.  
    17.             'Convert the value to currency and display.
    18.             Me.TextBox1.Text = currentValue.ToString("c")
    19.         End If
    20.     End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2004
    Posts
    22

    Re: Noob needs help with Check Boxes!

    Alright, I have my code to call it whenever the focus is put on one of the checkboxes inside of a group box. I tried this and it's doing the exact same thing.

    If chkBox1.Checked = True Then
    Cost = Cost + 0.25
    ElseIf chkBox1.Checked = False Then
    Cost = Cost - 0.25
    End If

    etc....

    The option strict just caused more problems then what I originally had, and I made the Cost dimension as double.

  10. #10
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Noob needs help with Check Boxes!

    Quote Originally Posted by jmcilhinney
    You may want to change this a bit if you aren't actually display a currency value in the TextBox but you get the idea:
    VB Code:
    1. Private Sub CheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged, _
    2.                                                                                                             CheckBox2.CheckedChanged, _
    3.                                                                                                             CheckBox3.CheckedChanged, _
    4.                                                                                                             CheckBox4.CheckedChanged
    5.         Dim currentValue As Double
    6.  
    7.         'Make sure that the TextBox contains a valid value.
    8.         If Double.TryParse(Me.TextBox1.Text, Globalization.NumberStyles.Currency, Nothing, currentValue) Then
    9.             If DirectCast(sender, CheckBox).Checked Then
    10.                 'Add $0.25
    11.                 currentValue += 0.25R
    12.             Else
    13.                 'Subtract $0.25
    14.                 currentValue -= 0.25R
    15.             End If
    16.  
    17.             'Convert the value to currency and display.
    18.             Me.TextBox1.Text = currentValue.ToString("c")
    19.         End If
    20.     End Sub

    ....ok that's cool.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Noob needs help with Check Boxes!

    Note that, as my code does, you only have to check the state of the CheckBox that raised the event because you know that all the others are unchanged. You just add or subtract 0.25 based on the new state of the CheckBox that raised the event. I'd say that the problem you're facing is that you are adding 0.25 for each one that's checked each time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Noob needs help with Check Boxes!

    Quote Originally Posted by Niche
    Alright, I have my code to call it whenever the focus is put on one of the checkboxes inside of a group box. I tried this and it's doing the exact same thing.

    If chkBox1.Checked = True Then
    Cost = Cost + 0.25
    ElseIf chkBox1.Checked = False Then
    Cost = Cost - 0.25
    End If

    etc....

    The option strict just caused more problems then what I originally had, and I made the Cost dimension as double.
    Jmcilhinney's code is really good for what you're doing, I think.. but a couple of things to mention. One, Handling the GotFocus event is probably a bad way to go about it. If your user uses the keyboard, the whole code will fall apart (since it can get focus without clicking, and only checks when it gets focus)

    Also, if Option strict on is causing problems, then you are performing alot of implicit conversions - which could lead to even bigger problems down the line.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Oct 2004
    Posts
    22

    Re: Noob needs help with Check Boxes!

    Well there's a reason why I need implicit conversions and basic beginner line code, and if someone could PM me I could explain.

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Noob needs help with Check Boxes!

    No-one NEEDS implicit conversions as far as I've ever seen. You probably just don't realise where and when you should be converting and casting explicitly. Turning Option Strict On will help teach you that, as well as make your code more efficient and less error-prone. It takes more effort and time in the short term but will save you effort and time in the long run. Why don't you give an example of where you feel you need an implicit conversion and we'll see if we can explain why you don't.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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