Results 1 to 19 of 19

Thread: [RESOLVED] [2008] How to number results in a list box?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    20

    Resolved [RESOLVED] [2008] How to number results in a list box?

    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

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2008] How to number results in a list box?

    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.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    20

    Re: [2008] How to number results in a list box?

    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

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [2008] How to number results in a list box?

    Are you looking for something like this?
    vb.net Code:
    1. ListBox.Items.Add ("Result #1")
    2. ListBox.Items.Add ("Result #2")
    3. ListBox.Items.Add ("Result #3")
    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

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] How to number results in a list box?

    Probably more like this:
    Code:
    For i as integer = 1 to 3
       ListBox1.Items.Add(String.Format("{0}: Result #{0}", i))
    Next
    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 -

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    20

    Re: [2008] How to number results in a list box?

    Ok here is my code and I just need the results of the calculation to be numbered.

    vb Code:
    1. Private Sub btnCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCal.Click
    2.         'Declare variable
    3.         Dim iv As Decimal
    4.         Dim result As Decimal
    5.         Dim revenue As Decimal
    6.         Dim expenses As Decimal
    7.         Dim intYear As Integer = 0
    8.  
    9.         'Convert input correctly
    10.         iv = CDec(txtInvestment.Text)
    11.         expenses = CDec(txtExp.Text)
    12.         revenue = CDec(txtRev.Text)
    13.  
    14.         'Calculate and display results in listbox
    15.         iv = (revenue - expenses) - iv
    16.         lstResults.Items.Add(iv)
    17.         result = iv
    18.         Do Until intYear = 9
    19.             result = result + (revenue - expenses)
    20.             intYear += 1
    21.             lstResults.Items.Add(result)
    22.         Loop
    23.     End Sub

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    20

    Re: [2008] How to number results in a list box?

    Does anyone know?

  8. #8
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2008] How to number results in a list box?

    Stanav gave you the answer in post #5, just change what you add to to the listbox to include a number.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2008] How to number results in a list box?

    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.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    20

    Re: [2008] How to number results in a list box?

    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
    Attached Images Attached Images   

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2008] How to number results in a list box?

    Hey,

    So, using what has already been given to you above, does the following not give you what you want:

    Code:
            Do Until intYear = 9
                result = result + (revenue - expenses)
                lstResults.Items.Add(String.Format("{0}: {1}", intYear, result))
                intYear += 1
            Loop
    Hope that helps!!

    Gary

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    20

    Re: [2008] How to number results in a list box?

    Hey,
    Thanks but no it gives me this:
    Sorry I'm new to VB.
    Attached Images Attached Images  

  13. #13
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2008] How to number results in a list box?

    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

  14. #14
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2008] How to number results in a list box?

    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
    Hope that helps!!

    Gary

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    20

    Re: [2008] How to number results in a list box?

    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

  16. #16
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] How to number results in a list box?

    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 -

  17. #17
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2008] How to number results in a list box?

    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

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    20

    Re: [2008] How to number results in a list box?

    Ok great. Everything works. Thanks a lot guys I really appreciate it.

  19. #19
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2008] How to number results in a list box?

    Cool, remember to mark your thread as resolved if your question has been answered.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width