Results 1 to 10 of 10

Thread: Learning help please

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    5

    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

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Learning help please

    Moved From The CodeBank

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    5

    Re: Learning help please

    I am lost as to what has happened to my post. Let me know
    Thanks

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  5. #5
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    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.
    VB 2005, Win Xp Pro sp2

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    5

    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

  7. #7
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    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
    VB 2005, Win Xp Pro sp2

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    5

    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

  9. #9
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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:
    1. Dim totalToppings As Integer
    2.  
    3. Private Sub CheckBoxs_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    4. Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
    5.  
    6.     Dim cb As CheckBox = DirectCast(sender, CheckBox)
    7.     If cb.Checked Then
    8.        Me.totalToppings += 1
    9.     Else
    10.        Me.totalToppings -= 1
    11.     End If
    12.     Me.toppingPrice.Text = totalToppings.ToString("C")
    13. End Sub

  10. #10
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    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
    VB 2005, Win Xp Pro sp2

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