Add the number count (Visible) in a listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • patopo1
    New Member
    • Oct 2012
    • 4

    Add the number count (Visible) in a listbox

    Hi, I have google this many days without luck, I hope you could help me:)

    Well , i add the items to the listbox normally and I get this:

    $53.23
    $34.99
    $74.33
    $93.63

    Thats how it is show in the listbox, but I want to show like this:

    01 - $53.23
    02 - $34.99
    03 - $74.33
    04 - $93.63
    ...
    ...
    I have tried this many ways without luck, I dont know if it is a property or a specific code that I need so i hope you understand my question
    Thanks

    VISUAL BASIC 6
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    How do you add the values to the Listbox?
    Value after value with a counter like:
    Code:
    Private Sub Command1_Click()
    Dim i As Integer
    For i = 1 To 20
       If i < 10 Then
          List1.AddItem "0" & i & " - " & i * 10 & "$"
       Else
          List1.AddItem i & " - " & i * 10 & "$"
       End If
    Next
    End Sub
    If not, but it's entered from a text file as block of data:
    Load it in a first hidden listbox.
    Read the lines in the first listbox and put it in a second visible listbox:
    Code:
    Private Sub Command2_Click()
    Dim i As Integer
    Dim j As Integer
    List1.Clear
    For i = 1 To 20
       List1.AddItem i * 10 & "$"
    Next
    For j = 0 To List1.ListCount - 1
       If j < 9 Then
          List2.AddItem "0" & j + 1 & " - " & List1.List(j)
       Else
          List2.AddItem j + 1 & " - " & List1.List(j)
       End If
    Next
    End Sub

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      You might want to investigate multi-column listboxes, too.

      Comment

      • patopo1
        New Member
        • Oct 2012
        • 4

        #4
        Maybe you could help me to get the code into mine, This is mine:
        Code:
        Private Sub cmdCalculate_Click()
          Dim X As String
          Dim Initial As Integer
          Dim interest As Integer
          Dim years As Integer
          Dim balance As Integer
          
          lstBalance.Clear
         
          Initial = Val(txtInitialInvestment.Text)
          interest = Val(txtAnnualInterest.Text)
          years = Val(txtYearsToSave.Text)
          
          X = Initial / 100 * (interest + 100)
          X = Format(X, "currency")
          lstBalance.AddItem X
          
          For years = 2 To years
            X = X / 100 * (interest + 100)
            X = Format(X, "currency")
           lstBalance.AddItem X
          Next years
          
          lstBalance.SetFocus
          txtInitialInvestment.SetFocus
        End Sub
        
        Private Sub cmdExit_Click()
          Unload Me
        End Sub
        
        Private Sub txtAnnualInterest_GotFocus()
          txtAnnualInterest.SelStart = 0
          txtAnnualInterest.SelLength = Len(txtAnnualInterest.Text)
        End Sub
        
        Private Sub txtInitialInvestment_GotFocus()
          txtInitialInvestment.SelStart = 0
          txtInitialInvestment.SelLength = Len(txtInitialInvestment.Text)
        End Sub
        
        Private Sub txtYearsToSave_GotFocus()
          txtYearsToSave.SelStart = 0
          txtYearsToSave.SelLength = Len(txtYearsToSave.Text)
        End Sub
        I am going to try with multi-column and see if that work thanks

        Comment

        • patopo1
          New Member
          • Oct 2012
          • 4

          #5
          Ok now i have this:

          Code:
          X = Initial / 100 * (interest + 100)
            X = Format(X, "currency")
            i = lstBalance.ListCount
            lstBalance.AddItem "0" & i & " - " & X
            
            For years = 2 To years
              X = X / 100 * (interest + 100)
              X = Format(X, "currency")
              i = lstBalance.ListCount
              If i > 0 And i < 10 Then
                lstBalance.AddItem "0" & i & " - " & X
              Else
                lstBalance.AddItem i & " - " & X
              End If
            Next years
          It work almost like I want, the only problem is that the first item in the listbox starts with a "00" and i want to start with a "01"
          Any suggestions? thanks

          Comment

          • Marcussk
            New Member
            • Jul 2012
            • 4

            #6
            Change the row:
            lstBalance.AddI tem "0" & i & " - " & X
            into:
            lstBalance.AddI tem "0" & (i + 1) & " - " & X
            or something around this way...(declare variable which always has value i + 1 and use that instead

            Comment

            • Guido Geurs
              Recognized Expert Contributor
              • Oct 2009
              • 767

              #7
              You set the i = lstBalance.List Count !
              Where when you add the first item, the count=0 !! (empty list)
              so:
              i=0
              lstBalance.AddI tem "0" & i & " - " & X
              gives: 00
              The index for the next add must be: listcount +1

              Code:
                 If i > 0 And i < 10 Then 
                    lstBalance.AddItem "0" & i+1 & " - " & X 
                  Else 
                    lstBalance.AddItem i+1 & " - " & X 
                  End If

              Comment

              • patopo1
                New Member
                • Oct 2012
                • 4

                #8
                It works greatly
                Thanks!!:D

                Comment

                • IraComm
                  New Member
                  • Oct 2012
                  • 14

                  #9
                  Assuming the data is already in the listbox:

                  Code:
                  Private Sub Command1_Click()
                  Dim f as interger
                  Dim a$
                  
                  For f = 0 To List1.ListCount - 1
                      a$ = List1.List(f)
                      
                      a$ = Format(f + 1, "00") & " " & a$
                      
                      List1.List(f) = a$
                  Next
                      
                  End Sub

                  Comment

                  Working...