Results 1 to 9 of 9

Thread: How to Total numbers on a textbox.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2008
    Posts
    12

    Exclamation How to Total numbers on a textbox.

    Yep, As in the other post I need to know if its just as simple as I think it is..

    Its basicly this code but for a textbox not a list.
    Code:
    listprice.AddItem "Sub Total:    " & subTotal
    listprice.ListIndex = listprice.ListCount - 1 '<<< this will highlight new item
    thanks

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: How to Total numbers on a textbox.

    You mean to highlight the last line? If so:
    Code:
    Dim start As Long
    
    start = Len(Text1.Text)
    
    Text1.Text = Text1.Text & vbCrLf & "something new"
    
    start = start + Len(vbCrLf)
    
    Text1.SelStart = start
    Text1.SelLength = Len(Text1.Text) - start
    
    Text1.SetFocus

  3. #3
    New Member
    Join Date
    Aug 2008
    Posts
    2

    Re: How to Total numbers on a textbox.

    Hello, i also need this code, and we are looking for a way to make a subtotal of the numbers in a text box - nothing to do with highlighting

  4. #4
    Lively Member
    Join Date
    Jan 2007
    Posts
    71

    Re: How to Total numbers on a textbox.

    I'd like to hear more about the application. Doesn't seem like a text box is your best bet...?

  5. #5
    New Member
    Join Date
    Aug 2008
    Posts
    2

    Re: How to Total numbers on a textbox.

    Okay so the program we need to make (a cash register thingie) requires you to write a bunch of items onto a datafile (using buttons in our case) then read them from the datafile (we have them going into two text boxes, one for text, one for cost) and produce a subtotal of the cost of all the items for that table/datafile. We tried doing the total with a list box but this gave us problems as far as putting the items from the datafile into the list box went (only the last item was showing =S). So yeh. We need a way to make a subtotal of the numbers in a text box. Or another way if it works xD

  6. #6
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: How to Total numbers on a textbox.

    I recommend a list box as the best way to present the data and then a label can be used to show the total. I doubt that you want users to be changing that total. Here's some sample code in which I build the file using random data:
    Code:
    Const ListSize = 20
    
    Private Sub Command1_Click()
    ' Retrieve Data and Total It
    Dim Total As Single, MyNumbers As String
    Open "MyData" For Input As #1
    For I = 1 To ListSize
        Line Input #1, MyNumbers
        List1.AddItem MyNumbers
        Total = Total + CSng(MyNumbers)
    Next
    Close
    Label1.Caption = "Total = " & Format$(Total, "#,###.00")
    End Sub
    
    Private Sub Form_Load()
    Open "MyData" For Output As #1
    ' Build a Sample Data File
    For I = 1 To ListSize
        Print #1, Format$((Rnd * 100) + 1, "###.00")
    Next
    Close
    Label1.Caption = vbNullString
    End Sub
    If you wish to keep adding numbers to the file, use a text box for data entry and open it for Append to write to the file.
    Last edited by Code Doc; Aug 5th, 2008 at 06:17 PM.
    Doctor Ed

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2008
    Posts
    12

    Re: How to Total numbers on a textbox.

    Thanks. I shall use that once I learn how to print to a listbox.
    :P

    And haha demonic I've got more posts than you!!!

  8. #8
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: How to Total numbers on a textbox.

    I hope you guys realize that you're doing someones homework for them. Read the first line in post 5.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  9. #9
    Lively Member
    Join Date
    Jan 2007
    Posts
    71

    Re: How to Total numbers on a textbox.

    Good point. But man, what a horrible assignment for learning VB. Can't they come up with anything that's even remotely like a real-world situation?

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