By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,983 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,983 IT Pros & Developers. It's quick & easy.

calculation for total sum

P: 9
ive been working on this code for a long time... bt doesn't work... i want it to total up the sum of the number, there are adding and deletion of numbers, bt the sum doesn't appear rite... here's my screwed up code

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command7_Click()
  2. Text4.Text = Total
  3. End Sub
  4. Private Sub ctrlAddButton_Click()
  5. Total = 0
  6.  
  7.     ctrlListBox1.AddItem ctrlTextBox1.Text
  8.     Total = ctrlTextBox1.Text + Total
  9.     If (ctrlDeleteButton.Enabled = False) Then
  10.       ctrlDeleteButton.Enabled = True
  11.  
  12.     End If
  13.  
  14. End Sub
  15. Private Sub ctrlDeleteButton_Click()
  16.     If (Not (ctrlListBox1.ListCount = 0)) Then
  17.         ctrlListBox1.RemoveItem (ctrlListBox1.ListCount - 1)
  18.         Total = ctrlTextBox1.Text - Total
  19.         If (ctrlListBox1.ListCount = 0) Then
  20.                 ctrlDeleteButton.Enabled = False
  21.         End If
  22.  
  23.     End If
  24. End Sub
  25. Private Sub Form_Load()
  26.     If (ctrlListBox1.ListCount = 0) Then
  27.         ctrlDeleteButton.Enabled = False
  28.     End If
  29. End Sub
Apr 30 '07 #1
Share this Question
Share on Google+
3 Replies


P: 9
this is after i modified, my total sum always appears as 0

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command7_Click()
  2. Total = 0
  3. If (ctrlAddButton) Then
  4. Total = CInt(ctrlTextBox1) + Total
  5. End If
  6. If (ctrlDeleteButton) Then
  7. Total = CInt(ctrlTextBox1) - Total
  8. End If
  9. Text4.Text = Total
  10. End Sub
  11. Private Sub ctrlAddButton_Click()
  12.  
  13.   ctrlListBox1.AddItem ctrlTextBox1.Text
  14.   If (ctrlDeleteButton.Enabled = False) Then
  15.   ctrlDeleteButton.Enabled = True
  16.   End If
  17.  
  18. End Sub
  19. Private Sub ctrlDeleteButton_Click()
  20.     If (Not (ctrlListBox1.ListCount = 0)) Then
  21.         ctrlListBox1.RemoveItem (ctrlListBox1.ListCount - 1)
  22.         If (ctrlListBox1.ListCount = 0) Then
  23.                 ctrlDeleteButton.Enabled = False
  24.         End If
  25.  
  26.     End If
  27. End Sub
  28. Private Sub Form_Load()
  29.     If (ctrlListBox1.ListCount = 0) Then
  30.         ctrlDeleteButton.Enabled = False
  31.     End If
  32. End Sub
Apr 30 '07 #2

P: 9
this is my latest .... now when i delete, i delete the last inserted number , how to change it instead from deleting from the last inserted number, bt it deletes from the list?

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command7_Click()
  2. Text4.Text = Total
  3. End Sub
  4. Private Sub ctrlAddButton_Click()
  5.  
  6.   ctrlListBox1.AddItem ctrlTextBox1.Text
  7.     If (ctrlDeleteButton.Enabled = False) Then
  8.         ctrlDeleteButton.Enabled = True
  9.     End If
  10.     If (ctrlAddButton) Then
  11.         Total = CInt(ctrlTextBox1) + Total
  12.     End If
  13.  
  14. End Sub
  15. Private Sub ctrlDeleteButton_Click()
  16.     If (Not (ctrlListBox1.ListCount = 0)) Then
  17.         ctrlListBox1.RemoveItem (ctrlListBox1.ListCount - 1)
  18.         If (ctrlListBox1.ListCount = 0) Then
  19.                 ctrlDeleteButton.Enabled = False
  20.         End If
  21.         If (ctrlDeleteButton) Then
  22.         Total = Total - CInt(ctrlTextBox1)
  23.         End If
  24.     End If
  25. End Sub
  26. Private Sub Form_Load()
  27.     If (ctrlListBox1.ListCount = 0) Then
  28.         ctrlDeleteButton.Enabled = False
  29.     End If
  30. End Sub
  31.  
Apr 30 '07 #3

Expert 5K+
P: 8,434
...how to change it instead from deleting from the last inserted number, bt it deletes from the list?
Can you try to ask this question in a different way? I can't tell exactly what you mean.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command7_Click()
  2.   Text4.Text = Total
  3. End Sub
  4.  
  5. Private Sub ctrlAddButton_Click()
  6.   ctrlListBox1.AddItem ctrlTextBox1.Text
  7.   If (ctrlDeleteButton.Enabled = False) Then
  8.     ctrlDeleteButton.Enabled = True
  9.   End If
  10.   If (ctrlAddButton) Then
  11.     Total = CInt(ctrlTextBox1) + Total
  12.   End If
  13. End Sub
  14.  
  15. Private Sub ctrlDeleteButton_Click()
  16.   If (Not (ctrlListBox1.ListCount = 0)) Then
  17.     ctrlListBox1.RemoveItem (ctrlListBox1.ListCount - 1)
  18.     If (ctrlListBox1.ListCount = 0) Then
  19.       ctrlDeleteButton.Enabled = False
  20.     End If
  21.     If (ctrlDeleteButton) Then
  22.       Total = Total - CInt(ctrlTextBox1)
  23.     End If
  24.   End If
  25. End Sub
  26.  
  27. Private Sub Form_Load()
  28.   If (ctrlListBox1.ListCount = 0) Then
  29.     ctrlDeleteButton.Enabled = False
  30.   End If
  31. End Sub
Two questions:
  1. Where Is the variable Total defined?
  2. What is the purpose of testing If CtrlDeleteButton (see highlighted line in code)? If this is supposed to test whether the delete button was clicked, you already know that because you're in the click handler routine.
Hm... I suppose the same applies to testing If (ctrlAddButton), further back.
Apr 30 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.