Hello,
I want to list the results in my list box in a numbering format, something like this:
1: Result #1
2: Result #2
3: Result #3
How do you create this type of format in a list box. Any help would be greatly appreciated. Thanks
Printable View
Hello,
I want to list the results in my list box in a numbering format, something like this:
1: Result #1
2: Result #2
3: Result #3
How do you create this type of format in a list box. Any help would be greatly appreciated. Thanks
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
Are you looking for something like this?
vb.net Code:
ListBox.Items.Add ("Result #1") ListBox.Items.Add ("Result #2") ListBox.Items.Add ("Result #3")
Probably more like this:
Code:For i as integer = 1 to 3
ListBox1.Items.Add(String.Format("{0}: Result #{0}", i))
Next
Ok here is my code and I just need the results of the calculation to be numbered.
vb 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 = 0 '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(iv) result = iv Do Until intYear = 9 result = result + (revenue - expenses) intYear += 1 lstResults.Items.Add(result) Loop End Sub
Does anyone know?
Stanav gave you the answer in post #5, just change what you add to to the listbox to include a number.
Also note that if the Sorted property of the listbox is True, then this will fail.
No, actually, now that I think about it, it won't fail using that code. It would fail under other circumstances, but not with the code you posted.
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
Hey,
So, using what has already been given to you above, does the following not give you what you want:
Hope that helps!!Code:Do Until intYear = 9
result = result + (revenue - expenses)
lstResults.Items.Add(String.Format("{0}: {1}", intYear, result))
intYear += 1
Loop
Gary
Hey,
Thanks but no it gives me this:
Sorry I'm new to VB.
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.
Gary
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:
Hope that helps!!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
Gary
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
Exactly, if you use what stanav has given in addition to what i gave in post 14, it should work the way you want it.
Gary
Ok great. Everything works. Thanks a lot guys I really appreciate it.
Cool, remember to mark your thread as resolved if your question has been answered.
Gary