Listbox problems

sweet-t7

Member
Joined
Jan 14, 2005
Messages
5
Programming Experience
Beginner
Ok, I'm totally new at this. I know how to select items in one listbox and put them into another one... but I can't figure out how to put user-entered text into a listbox. I want the user to enter text in txtBox1, click btnAdd, and the text will appear in lstBox1. What's wrong with this code...??

PrivateSub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

lstBox1.Items.Add(txtBox1.Text)

EndSub

 
Hmm well that should work fine. This maybe a stupid question but are you typing something into the textbox before clicking btnadd?

Also are you getting any errors?

TPM
 
hahaha - yes, I'm typing things into the textbox :)

No, I'm not getting any errors... that's part of why I was confused. At least errors give you an idea of what might be the problem.
 
I got it figured out... thanks for checking this for me. It ended up being a problem totally un-related a few lines earlier in the code. Those are so annoying...
 
Another problem

When I add items to the listbox (prices), I need to add all the prices in the listbox together to find Subtotal, and display it in a label. But for some reason my Subtotal is only showing the price of the most recent item entered... instead of adding up the items in the listbox. Here is my code for that portion - can anyone help?

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim ItemPrice As Double
Dim Subtotal As Double
' Add prices to receipt list
lstReceipt.Items.Add(FormatCurrency(txtPrice.Text))
ItemPrice = CDbl(txtPrice.Text)
' Below is the formula that is not working....
Subtotal = Subtotal + ItemPrice
lblSubtotal.Text = FormatCurrency(Subtotal)
End Sub
 
it's because your diming subtotal each time, which is resetting it. Either set it as a global (dim it outside the sub), or set it's value.

Also something to note to make your life a little easyer instead of : Subtotal = Subtotal + ItemPrice Use:Subtotal += ItemPrice (you can do this for - etc as well)

TPM
 
Back
Top