display total(sum) of listbox in a label, can some one help!!
-
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:
-
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:
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
-
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
thanks mate the numbers are in a listbox not a textbox