How are you loading the listbox? If you are databinding, I don't think it can be done, but if you are loading directly, it can be done to some extent, though not if you have the Sort property of the listbox set.
Well I'm doing a loop and adding the results directly to the listbox and no I don't have the Sort property set. I can post you the code if you want.
Thank you
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
Hello,
Thanks for all the help but I'm not getting it right, I am attaching a picture so you can better understand.
The first picture is what I want and the second picture is what I am getting. I tried the code in post #5 and its not working correctly. Thanks again for all your help
Did you use the code as I posted? Did you notice that I moved the intYear += 1 down a line?
I take it the 1 should indicate the first year, therefore I would initialize intYear to 1, rather than 0. In doing that though, you may need to change the extent of the while loop.
I have just noticed as well that using what I have suggested is always going to miss a number out of the first listbox item as you make an addition to the listbox outside of the while loop, try this:
Code:
Private Sub btnCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCal.Click
'Declare variable
Dim iv As Decimal
Dim result As Decimal
Dim revenue As Decimal
Dim expenses As Decimal
Dim intYear As Integer = 1
'Convert input correctly
iv = CDec(txtInvestment.Text)
expenses = CDec(txtExp.Text)
revenue = CDec(txtRev.Text)
'Calculate and display results in listbox
iv = (revenue - expenses) - iv
lstResults.Items.Add(String.Format("{0}: {1}", intYear, iv))
result = iv
Do Until intYear = 9
result = result + (revenue - expenses)
intYear += 1
lstResults.Items.Add(String.Format("{0}: {1}", intYear, result))
Loop
End Sub
Yeah I copy pasted the code. Now I initialized intYear = 1
It gives me the same thing except there is no 0: 800
Instead its 1: 800.
By the way, where did the $$ signs go suddenly
That's just plain formatting problem... If you want to display a value in a specific way, you have to tell your program how you want the value to be displayed. Try this:
Code:
Do Until intYear = 9
result = result + (revenue - expenses)
lstResults.Items.Add(String.Format("{0}: {1}", intYear, result.ToString("$#.00")))
intYear += 1
Loop
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it. - Abraham Lincoln -