|
-
Feb 4th, 2009, 11:41 AM
#1
Thread Starter
New Member
Learning help please
Hi all,
I am trying to teach myself the basics of programming before I take a course involved in it. I have been working through some sample pieces and i was wondering if someone could help me out with a little bit of code.
basically the problem i have is using check boxes. I want to select a certain amount of ice cream toppings and I have each check box done separately.
basically in the code for each text box i have this
Code:
Private Sub chkSprinkles_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSprinkles.CheckedChanged
If chkSprinkles.Checked Then
totalToppings += 1
End If
totalToppings += 0
End Sub
i figured doing this for each would allow me to raise a running total (totalToppings)
since each topping costs one dollar i have the totalToppings incrementing when each one is ticked. I want to then display this total in a text box.
I assumed it was something like this. would this not display the total in the text box
Code:
Private Sub toppingPrice_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles toppingPrice.TextChanged
toppingPrice.Text = totalToppings
End Sub
thanks alot
-
Feb 4th, 2009, 11:44 AM
#2
-
Feb 4th, 2009, 11:51 AM
#3
Thread Starter
New Member
Re: Learning help please
I am lost as to what has happened to my post. Let me know
Thanks
-
Feb 4th, 2009, 11:55 AM
#4
Re: Learning help please
It is right here. If it wasn't, you couldn't have posted in it.
The CodeBank (where you originally posted it) isn't for questions. It is for sharing code with other members.
You said the toppings cost a $1.00 each, but I don't see you adding a $1.00 to anything.
-
Feb 4th, 2009, 11:58 AM
#5
Re: Learning help please
The Text_Changed event fires when the text gets changed and not when some variable changes.
You can add the 'toppingPrice.Text = totalToppings' in the CheckedChanged event of each checkbox.
-
Feb 4th, 2009, 12:04 PM
#6
Thread Starter
New Member
Re: Learning help please
At the moment I just want the client to be able to press on a bunch of check boxes. and for each time one is clicked i want totalToppings to increase by 1.
This will allow me to say i have 5 6 7 8 etc amount of toppings.
Then i will convert it to say $5 for 5 toppings.
how do i make the total display inside the text box
-
Feb 4th, 2009, 12:22 PM
#7
Re: Learning help please
Code:
Private Sub chkSprinkles_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSprinkles.CheckedChanged
If chkSprinkles.Checked Then
totalToppings += 1
toppingPrice.Text = totalToppings.ToString
End If
End Sub
-
Feb 4th, 2009, 12:24 PM
#8
Thread Starter
New Member
Re: Learning help please
Thanks but let me clear up,
totalToppings DOES hold the amount of boxes checked off.
So if i called totalToppings it should display the amount of boxes that are checked off.
thanks sorry for all the troubles
-
Feb 4th, 2009, 12:33 PM
#9
Re: Learning help please
You can have one event handler to handle for all the checkBoxs’ CheckedChanged event and add or subtract one according to the checked state of the control. Here is an example:
vb Code:
Dim totalToppings As Integer
Private Sub CheckBoxs_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
Dim cb As CheckBox = DirectCast(sender, CheckBox)
If cb.Checked Then
Me.totalToppings += 1
Else
Me.totalToppings -= 1
End If
Me.toppingPrice.Text = totalToppings.ToString("C")
End Sub
Last edited by VBDT; Feb 4th, 2009 at 12:45 PM.
-
Feb 4th, 2009, 12:40 PM
#10
Re: Learning help please
Aah, got it...yep, the above soluton is the right way. If you want to do it for each checkbox separately:
Code:
Private Sub chkSprinkles_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSprinkles.CheckedChanged
If chkSprinkles.Checked Then
totalToppings += 1
toppingPrice.Text = totalToppings.ToString
Else
totalToppings -= 1
toppingPrice.Text = totalToppings.ToString
End If
End Sub
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
|