Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. display total(sum) of listbox in a label, can some one help!!

display total(sum) of listbox in a label, can some one help!!

Scheduled Pinned Locked Moved Visual Basic
helptutorialannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Simon Smith 12345
    wrote on last edited by
    #1

    can someone help please i have a button used for adding numbers from a textbox to a listbox, at the bottom of my form i have a label that shows the total(sum) of the numbers entered in the listbox for example if 3 and 8 were in the listbox i would want the label at the botton to show 11, and if another number was entered in the listbox i would want the label to update i know it may be easy but i am new to VB, please could someone help many thanks :omg:

    D 1 Reply Last reply
    0
    • S Simon Smith 12345

      can someone help please i have a button used for adding numbers from a textbox to a listbox, at the bottom of my form i have a label that shows the total(sum) of the numbers entered in the listbox for example if 3 and 8 were in the listbox i would want the label at the botton to show 11, and if another number was entered in the listbox i would want the label to update i know it may be easy but i am new to VB, please could someone help many thanks :omg:

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      OK. You and the other guy keep saying 'listbox'. Is it a listbox or a TextBox? The thing to do would be to add code to the Changed event of the textboxes. This sample uses one function to handle the Changed events of TWO Textboxes. IF YOU USE THIS CODE EXACTLY, YOUR INSTRUCTOR WILL MORE THAN LIKELY KNOW YOU DIDN'T WRITE IT!

      Private Sub tbChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbNumber1.TextChanged, tbNumber2.TextChanged
          Dim Result As Single
          Dim Operand1, Operand2 As Single
      
          ' Try and convert the text in the first textbox to a Single.
          Try
              Operand1 = CSng(tbNumber1.Text)
          Catch ex As Exception
              Operand1 = 0.0
          End Try
      
          ' Try and convert the text in the second textbox to a Single.
          Try
              Operand2 = CSng(tbNumber2.Text)
          Catch ex As Exception
              Operand2 = 0.0
          End Try
      
          Result = Operand1 + Operand2
          lblResult.Text = Result.ToString
      End Sub
      

      The form contains two textboxes, tbNumber1 and tbNumber2, and a label control called lblResult. If you type in either textbox, this function will convert both textboxes to Single precision numbers, if possible, add them, then show the result in the label control. Now, you could easily change this so the code only runs when you click on a button instead of relying on the Change event of a TextBox. :-D But that, I'll leave up to you. RageInTheMachine9532

      S 1 Reply Last reply
      0
      • D Dave Kreskowiak

        OK. You and the other guy keep saying 'listbox'. Is it a listbox or a TextBox? The thing to do would be to add code to the Changed event of the textboxes. This sample uses one function to handle the Changed events of TWO Textboxes. IF YOU USE THIS CODE EXACTLY, YOUR INSTRUCTOR WILL MORE THAN LIKELY KNOW YOU DIDN'T WRITE IT!

        Private Sub tbChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbNumber1.TextChanged, tbNumber2.TextChanged
            Dim Result As Single
            Dim Operand1, Operand2 As Single
        
            ' Try and convert the text in the first textbox to a Single.
            Try
                Operand1 = CSng(tbNumber1.Text)
            Catch ex As Exception
                Operand1 = 0.0
            End Try
        
            ' Try and convert the text in the second textbox to a Single.
            Try
                Operand2 = CSng(tbNumber2.Text)
            Catch ex As Exception
                Operand2 = 0.0
            End Try
        
            Result = Operand1 + Operand2
            lblResult.Text = Result.ToString
        End Sub
        

        The form contains two textboxes, tbNumber1 and tbNumber2, and a label control called lblResult. If you type in either textbox, this function will convert both textboxes to Single precision numbers, if possible, add them, then show the result in the label control. Now, you could easily change this so the code only runs when you click on a button instead of relying on the Change event of a TextBox. :-D But that, I'll leave up to you. RageInTheMachine9532

        S Offline
        S Offline
        Simon Smith 12345
        wrote on last edited by
        #3

        thanks mate the numbers are in a listbox not a textbox

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups