|
-
Nov 2nd, 2005, 11:47 PM
#1
Thread Starter
Junior Member
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
-
Nov 2nd, 2005, 11:59 PM
#2
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...
-
Nov 3rd, 2005, 12:00 AM
#3
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:
Dim Cost as Decimal = Convert.ToDecimal(textbox1.text)
Cost = Cost + 0.25D 'Or Cost += 0.25D to save typing.
textbox1.text = Cost.ToString
You can also concatenate all of the above to one statement:
VB Code:
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
It can also be set as the default if you play with the options a little bit.
Bill
-
Nov 3rd, 2005, 12:04 AM
#4
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
-
Nov 3rd, 2005, 12:10 AM
#5
Thread Starter
Junior Member
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.
-
Nov 3rd, 2005, 12:13 AM
#6
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
-
Nov 3rd, 2005, 12:16 AM
#7
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...
-
Nov 3rd, 2005, 12:19 AM
#8
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:
Private Sub CheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged, _
CheckBox2.CheckedChanged, _
CheckBox3.CheckedChanged, _
CheckBox4.CheckedChanged
Dim currentValue As Double
'Make sure that the TextBox contains a valid value.
If Double.TryParse(Me.TextBox1.Text, Globalization.NumberStyles.Currency, Nothing, currentValue) Then
If DirectCast(sender, CheckBox).Checked Then
'Add $0.25
currentValue += 0.25R
Else
'Subtract $0.25
currentValue -= 0.25R
End If
'Convert the value to currency and display.
Me.TextBox1.Text = currentValue.ToString("c")
End If
End Sub
-
Nov 3rd, 2005, 12:19 AM
#9
Thread Starter
Junior Member
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.
-
Nov 3rd, 2005, 12:23 AM
#10
Re: Noob needs help with Check Boxes!
 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:
Private Sub CheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged, _
CheckBox2.CheckedChanged, _
CheckBox3.CheckedChanged, _
CheckBox4.CheckedChanged
Dim currentValue As Double
'Make sure that the TextBox contains a valid value.
If Double.TryParse(Me.TextBox1.Text, Globalization.NumberStyles.Currency, Nothing, currentValue) Then
If DirectCast(sender, CheckBox).Checked Then
'Add $0.25
currentValue += 0.25R
Else
'Subtract $0.25
currentValue -= 0.25R
End If
'Convert the value to currency and display.
Me.TextBox1.Text = currentValue.ToString("c")
End If
End Sub
....ok that's cool.
Bill
-
Nov 3rd, 2005, 12:27 AM
#11
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.
-
Nov 3rd, 2005, 12:30 AM
#12
Re: Noob needs help with Check Boxes!
 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
-
Nov 3rd, 2005, 12:38 AM
#13
Thread Starter
Junior Member
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.
-
Nov 3rd, 2005, 02:43 AM
#14
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.
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
|